import java.util.*;

public class Jack
{
	public static final int MAX = 100;
	public static final int MAXPASS = 9;
	public static String [] names = new String[MAX];
	public static int numNames;
	public static Ratio [][][] table = new Ratio[MAXPASS][MAX][MAX];

	public static String printRatio(long num, int exp)
	{
		String s = num+"";
		String ans;
		if (exp >= 5) {
			ans = "0.";
			for(int i=0; i<exp-5; i++)
				ans += "0";
			ans += s;
		}
		else if (exp >= 1) {
			ans = s.substring(0, 5-exp) + "." + s.substring(5-exp, s.length());
		}
		else if (exp == 0)
			ans = s;
		else {
			ans = s;
			for(; exp <0; exp++)
				ans += "0";
		}
		return ans;
	}

	public static long gcd(long a, long b)
	{
		if (b == 0)
			return a;
		else
			return gcd(b, a%b);
	}

	public static int nameId(String s)
	{
		int i=0;
		for(;i<numNames; i++) {
			if (names[i].equals(s))
				return i;
		}
		names[numNames++] = s;
		return numNames-1;
	}

	public static void printTable(int t, int numNames)
	{
		System.out.println("t = " + t);
		for(int i=0; i<numNames; i++) {
			for(int j=0; j<numNames; j++) {
				System.out.print(table[t][i][j].num + "/" + table[t][i][j].den + "(" + table[t][i][j].count + ") ");
			}
			System.out.println();
		}
		System.out.println();
	}

	public static void main(String [] args)
	{
		Scanner in = new Scanner(System.in);
		int ncases, n;
		int want, have;

		ncases = in.nextInt();
		for(int t=0; t<MAXPASS; t++)
			for(int i=0; i<MAX; i++)
				for(int j=0; j<MAX; j++)
					table[t][i][j] = new Ratio();

		for(int icase=1; icase<=ncases; icase++) {
			numNames = 0;
			want = nameId(in.next());
			have = nameId(in.next());
			n = in.nextInt();

			for(int t=0; t<MAXPASS; t++)
				for(int i=0; i<MAX; i++)
					for(int j=0; j<MAX; j++)
						table[t][i][j].reset();
			for(int i=0; i<n; i++) {
				long den, num, div;
				int name1, name2; 
				den = in.nextInt();
				name1 = nameId(in.next());
				num = in.nextInt();
				name2 = nameId(in.next());
				div = gcd(num, den);
				num /= div;
				den /= div;
				if (table[0][name2][name1].count == 0) {
					table[0][name2][name1].num = num;
					table[0][name2][name1].den = den;
					table[0][name2][name1].count = 1;
				}
				else if (num*table[0][name2][name1].den < den*table[0][name2][name1].num) {
					table[0][name2][name1].num = num;
					table[0][name2][name1].den = den;
					table[0][name2][name1].count = 1;
				}
				else if (num*table[0][name2][name1].den == den*table[0][name2][name1].num) {
					table[0][name2][name1].count++;
				}
			}

//printTable(0, numNames);
			for(int t=1; t<MAXPASS; t++) {
				for(int i=0; i<numNames; i++) {
					for(int j=0; j<numNames; j++) {
						for(int k=0; k<numNames; k++) {
							long num = table[t-1][i][k].num*table[0][k][j].num;
							long den = table[t-1][i][k].den*table[0][k][j].den;
							if (num == 0)
								continue;
							long div = gcd(num, den);
							num /= div;
							den /= div;
							long count = table[t-1][i][k].count*table[0][k][j].count;
							if (table[t][i][j].count == 0) {
								table[t][i][j].num = num;
								table[t][i][j].den = den;
								table[t][i][j].count = count;
							}
							else if (num!= 0 && num*table[t][i][j].den < den*table[t][i][j].num) {
								table[t][i][j].num = num;
								table[t][i][j].den = den;
								table[t][i][j].count = count;
							}
							else if (num!= 0 && num*table[t][i][j].den == den*table[t][i][j].num) {
								table[t][i][j].count += count;
							}
						}
					}
				}
//printTable(t, numNames);
			}	
			long bestNum = 0, bestDen = 0;
			long bestCount=0;
			for(int t=0; t<MAXPASS; t++) {
				long num = table[t][1][0].num;
				long den = table[t][1][0].den;
				long count = table[t][1][0].count;
//System.out.println("checking = " +num + "," + den + "," + count);
				if (bestNum == 0) {
					bestNum = num;
					bestDen = den;
					bestCount = count;
				}
				else if (num != 0 && num*bestDen < den*bestNum) {
					bestNum = num;
					bestDen = den;
					bestCount = count;
				}
				else if (num != 0 && num*bestDen == den*bestNum) {
					bestCount += count;
				}
			}

//System.out.println(bestNum + "," + bestDen + "," + bestCount);
//System.out.println(bestRatio);

			double bestRatio = ((double) bestNum)/bestDen;
			int exp = 4-(int)Math.log10(bestRatio);
			if (bestRatio < 1 && Math.log10(bestRatio) != 4-exp)
				exp++;
			long temp = (long)(bestRatio*Math.pow(10,exp)+0.5);
//			bestRatio = temp;
//			bestRatio /= Math.pow(10, exp);
//			System.out.print("Case " + icase + ": ");
//			printRatio(temp, exp);
//			System.out.println(" " + bestCount);
			System.out.println("Case " + icase + ": " + printRatio(temp, exp) + " " + bestCount);
//			System.out.printf("Case %d: %7f %d\n", icase, bestRatio, bestCount);
		}
	}
}

class Ratio
{
	long num, den;
	long count;

	Ratio()
	{
		reset();
	}

	void reset()
	{
		num = den = count = 0;
	}
}
