#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define endl '\n'

map<string, int> ids1;
map<int, string> ids2;
vector<vector<long double> > currs (8, vector<long double>(8, 0));
int val;
string name;
long double minOver = 1000000000;
int minId;
int minNum;
set<int> visited;

void dfs(int id, long double currRatio) {
	for (int i = 0; i < 8; i++) {
		if (currs[id][i] != 0) {
			int num = ceil(val * currs[id][i] * currRatio - 1e-10);
			if (!visited.count(i)) {
				if (num <= 100000) {
					long double over = num * currs[i][id] / currRatio - val;
//					cout << num << " ";
//					cout << fixed << setprecision(15) << over << endl;
					if (over < minOver) {
						minOver = over;
						minId = i;
						minNum = num;
					}
				}
				visited.insert(i);
				dfs(i, currRatio * currs[id][i]);
			}
		}
	}
}

int main(){
	ios::sync_with_stdio(0);
	int caseNo = 1;
	while (true) {		
		int n; cin >> n;
		if (n == 0) break;
		int currId = 0;
		
		for (int i = 0; i < n; i++) {
			pair<string, int> id1;
			pair<int, string> id2;
			int val1; cin >> val1;
			string name1; cin >> name1;
			if (!ids1.count(name1)) {
				ids1[name1] = currId;
				ids2[currId] = name1;
				currId++;
			}
			string dummy; cin >> dummy;
			int val2; cin >> val2;
			string name2; cin >> name2;
			if (!ids1.count(name2)) {
				ids1[name2] = currId;
				ids2[currId] = name2;
				currId++;
			}
			
			currs[ids1[name1]][ids1[name2]] = (long double) val2 / val1;
			currs[ids1[name2]][ids1[name1]] = (long double) val1 / val2;
		}
		
		cin >> val;
		cin >> name;
		visited.insert(ids1[name]);
		dfs(ids1[name], 1.0);
		cout << "Case " << caseNo << ": " << minNum << " " << ids2[minId] << endl;
		caseNo++;
		visited = set<int>();
		currs = vector<vector<long double> > (8, vector<long double>(8, 0));
		ids1 = map<string, int>();
		ids2 = map<int, string>();
		minOver = 1000000000;
	}
	return 0;
}