// Arup Guha
// 4/10/2021
// Solution to 2021 Code Jam Round 1A Problem B: Prime Time

import java.util.*;

public class Solution {

	public static int n;
	public static long[] primes;
	public static long[] freq;
	public static int[] powers;
	public static long sum;
	
	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();
		
		// Process all cases.
		for (int loop=1; loop<=nC; loop++) {
		
			// Store primes and frequencies here.
			n = stdin.nextInt();
			primes = new long[n];
			freq = new long[n];
			sum = 0;
			
			// Read input.
			for (int i=0; i<n; i++) {
				primes[i] = stdin.nextLong();
				freq[i] = stdin.nextLong();
				sum += (primes[i]*freq[i]);
			}
			
			// The smallest possible product of the product set is start.
			long start = Math.max(1, sum - 3500);
			
			// Default answer.
			long res = 0;
			
			// So val represents our desired product.
			for (long val=sum; val>=start; val--) {
				
				powers = new int[n];
				long tmp = val;
				long sumTerms = 0;
				
				// Flag to see if this set is possible.
				boolean flag = true;
				
				// Prime factorize val, but only using the primes we have cards for.
				for (int i=0; i<n; i++) {
					
					// See how many times this prime divides in to our number.
					while (tmp%primes[i] == 0) {
						powers[i]++;
						tmp /= primes[i];
						sumTerms += primes[i];
					}
					
					// We don't have enough of this card to make this product.
					if (powers[i] > freq[i]) {
						flag = false;
						break;
					}
				}
				
				// Go to next number.
				if (!flag) continue;
				
				// Divided everything out so we were able to achieve val with the cards we have AND
				// the sum of the rest of the cards equals this product.
				if (tmp == 1 && val == sum-sumTerms) {
					res = val;
					break;
				}
			}				

			// Ta da!
			System.out.println("Case #"+loop+": "+res);		
		}
	}
}