// dpoeschl import java.util.*; public class DParencedence { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int cases = scan.nextInt(); scan.nextLine(); for(int c = 1; c <= cases; c++) { System.out.println("Case " + c + ":"); process(scan.nextLine()); } } public static void process(String line) { String[] parts = line.split(" "); char[] ops = new char[Integer.parseInt(parts[0])]; for(int i = 0; i < ops.length; i++) ops[i] = parts[2 * i + 2].charAt(0); long[] nums = new long[ops.length+1]; for(int i = 0; i < nums.length; i++) nums[i] = Long.parseLong(parts[2 * i + 1]); long p1 = solve(nums, ops, true, true); long p2 = solve(nums, ops, false, true); int winner = p1 == -p2 ? 0 : (p1 > -p2 ? 1 : 2); System.out.println(winner == 0 ? "Tie" : ("Player " + winner + " wins")); } public static long solve(long[] nums, char[] ops, boolean maximize, boolean print) { int bestOpIndex = -1; long bestScore = maximize ? Long.MIN_VALUE : Long.MAX_VALUE; if(nums.length == 2) { bestOpIndex = 0; bestScore = calc(nums[0], ops[0], nums[1]); } else { for(int i = 0; i < ops.length; i++) { long[] nums2 = new long[nums.length-1]; for(int j = 0; j < i; j++) nums2[j] = nums[j]; nums2[i] = calc(nums[i], ops[i], nums[i+1]); for(int j = i+1; j < nums.length-1; j++) nums2[j] = nums[j+1]; char[] ops2 = new char[ops.length-1]; for(int j = 0; j < i; j++) ops2[j] = ops[j]; for(int j = i; j < ops.length-1; j++) ops2[j] = ops[j+1]; long answer = solve(nums2, ops2, !maximize, false); if(maximize ? (answer > bestScore) : (answer < bestScore)) { bestOpIndex = i; bestScore = answer; } } } if(print) System.out.println("Player " + (maximize ? 1 : 2) + " (" + nums[bestOpIndex] + ops[bestOpIndex] + nums[bestOpIndex+1] +") leads to " + bestScore); return bestScore; } public static long calc(long a, char op, long b) { switch(op) { case '+': return a + b; case '-': return a - b; case '*': return a * b; default: throw new RuntimeException(); } } }