// Arup Guha
// 7/20/2013
// Solution to 2012 East Central Regional Problem G: Show Me the Money

import java.util.*;

public class g {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();
		int loop = 1;

		while (n != 0) {

			// Read in graph.
			pair[][] edges = new pair[n][2];
			for (int i=0; i<n; i++) {
				int aF = stdin.nextInt();
				String aS = stdin.next();
				edges[i][0] = new pair(aS, aF);
				stdin.next();
				int bF = stdin.nextInt();
				String bS = stdin.next();
				edges[i][1] = new pair(bS, bF);
			}

			// This is what we must convert.
			int val = stdin.nextInt();
			String cur = stdin.next();
			pair get = new pair(cur, val);

			// Solve and output.
			pair ans = solve(edges, get);
			System.out.println("Case "+loop+": "+ans.factor+" "+ans.cur);

			// Go to next case.
			n = stdin.nextInt();
			loop++;
		}
	}

	public static pair solve(pair[][] edges, pair target) {

		HashMap<String,Integer> vList = new HashMap<String,Integer>();
		ArrayList<String> lookup = new ArrayList<String>();

		// Create mapping of all vertices.
		int index = 0;
		for (int i=0; i<edges.length; i++) {
			for (int j=0; j<2; j++) {
				if (!vList.containsKey(edges[i][j].cur)) {
					vList.put(edges[i][j].cur, index);
					lookup.add(edges[i][j].cur);
					index++;
				}
			}
		}

		double[][] graph = new double[index][index];
		for (int i=0; i<graph.length; i++)
			graph[i][i] = 1;

		// Fill in graph.
		for (int i=0; i<edges.length; i++) {
			double factor = 1.0*edges[i][1].factor/edges[i][0].factor;
			int v1 = vList.get(edges[i][0].cur);
			int v2 = vList.get(edges[i][1].cur);
			graph[v1][v2] = factor;
			graph[v2][v1] = 1.0/factor;
		}

		// Slow, but for this small of a graph Floyd's is fine.
		for (int k=0; k<graph.length; k++) {
			for (int i=0; i<graph.length; i++) {
				for (int j=0; j<graph.length; j++) {
					if (graph[i][k] != 0 && graph[k][j] != 0)
						graph[i][j] = graph[i][k]*graph[k][j];
				}
			}
		}

		// Initialize variables to store best.
		boolean set = false;
		double bestVal = -1;
		int bestItem = -1, bestUnits = -1;

		int start = vList.get(target.cur);
		for (int i=0; i<graph.length; i++) {
			if (i == start) continue;

			// The -1e-6 is for floating point for exact matches...
			int units = (int)(Math.ceil(target.factor*graph[start][i]-1e-6));

			// This is invalid...
			if (units > 100000) continue;

			// See if we should update.
			double realVal = units*graph[i][start];
			if (!set || realVal > 0 && realVal < bestVal) {
				bestVal = realVal;
				bestItem = i;
				bestUnits = units;
				set = true;
			}

		}

		return new pair(lookup.get(bestItem), bestUnits);
	}

}

class pair {

	public String cur;
	public int factor;

	public pair(String s, int f) {
		cur = s;
		factor = f;
	}
}