#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <cmath>

using namespace std;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ld, int> S;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int csnum = 1;
    int n; cin >> n;
    while (n > 0) {
        map<string, int> str2idx;
        vector<string> idx2str;
        vector<vector<piii> > adj;
        int nv = 0;
        for (int i = 0; i < n; i++) {
            int v1, v2; string c1, c2; char c;
            cin >> v1 >> c1 >> c >> v2 >> c2;
            if (str2idx.count(c1) == 0) {
                str2idx[c1] = nv++;
                idx2str.push_back(c1);
                adj.push_back(vector<piii>());
            }
            if (str2idx.count(c2) == 0) {
                str2idx[c2] = nv++;
                idx2str.push_back(c2);
                adj.push_back(vector<piii>());
            }
            adj[str2idx[c1]].push_back(piii(str2idx[c2], pii(v1, v2)));
            adj[str2idx[c2]].push_back(piii(str2idx[c1], pii(v2, v1)));
        }
        int val; string cc; cin >> val >> cc;
        int s = str2idx[cc];
        int best = -1;
        int bestidx = -1;
        ld bestdiff = 1000000000000.L;
        vector<bool> visited(nv, false);
        queue<S> q;
        q.push(S(val, s));
        while (!q.empty()) {
            S curr = q.front(); q.pop();
            if (visited[curr.second]) continue;
            visited[curr.second] = true;

            //cerr << "visit: " << idx2str[curr.second] << " " << curr.first << endl;

            for (int i = 0; i < adj[curr.second].size(); i++) {
                piii blah = adj[curr.second][i];
                if (blah.first != s) {
                    ld nxt = curr.first * blah.second.second / blah.second.first;
                    ld nxtround = ceil(nxt-1e-9);
                    ld diff = abs(nxtround/nxt*val-val);
                    if (diff < bestdiff && (int)nxtround <= 100000) {
                        bestdiff = diff;
                        best = (int)nxtround;
                        bestidx = blah.first;
                    }
                    q.push(S(nxt, blah.first));
                }
            }
        }
        cout << "Case " << csnum << ": " << best << " " << idx2str[bestidx] << endl;

        cin >> n;
        csnum++;
    }
}