#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
typedef long double ld;

ld change[9][9]; //1 b = change[a][b] dollars of a
bool visited[9]; 
set<int> adj[9];
ld simple[9]; //simple[i] of i = 1 dollar of a
map<string,int> key;
int main(){
    ios::sync_with_stdio(0);
    int n;
    int ca=1;
    while (cin>>n){
        memset(change,0,sizeof change); memset(visited,0,sizeof visited); memset(simple,0,sizeof simple);
        key.clear();
        if (n==0) break;
        vector<tuple<int,string,int,string>> stuff; 
        set<string> names;
        for (int i=0;i<n;i++){
            int a,b; string x,y; char c; cin>>a>>x>>c>>b>>y; 
            stuff.push_back(make_tuple(a,x,b,y));
            names.insert(x); names.insert(y); 
        }
        vector<string> currencies; currencies.assign(names.begin(),names.end());
        for (int i=0;i<currencies.size();i++) key[currencies[i]]=i;
        for (int i=0;i<currencies.size();i++) adj[i].clear();
        for (tuple<int,string,int,string> t:stuff){
            int a=get<0>(t),x=key[get<1>(t)],b=get<2>(t),y=key[get<3>(t)];
            change[y][x]=ld(b)/ld(a); //a of x = b of y -> 1 of x = b/a of y
            change[x][y]=1.0/change[y][x];
            adj[x].insert(y); adj[y].insert(x);
        }
        
        int want; string what; cin>>want>>what; 
        //bfs, determine conversion
        int start=key[what];
        queue<int> q; q.push(start); simple[start]=1; visited[start]=1;
        while (!q.empty()){
            int now=q.front(); q.pop();
            for (int nex:adj[now]) if (!visited[nex]){
                simple[nex]=simple[now]*change[nex][now];
                visited[nex]=true;
                q.push(nex);
            }
        }
        //for (int i=0;i<currencies.size();i++) cout<<simple[i]<<' '; cout<<endl;
        //solve:
        pair<int,string> solution; ld minerr=1e7;
        for (int i=0;i<currencies.size();i++) if ((i!=start)&&(simple[i]>1e-6)){
            ld ch=simple[i]*ld(want);  
            ld pick=int(ch); 
            if (abs(ch-pick)>1e-6) pick++;
            if (pick<1e5+1e-6){
                ld error=pick/simple[i];
                if (error<minerr) {
                    solution={pick,currencies[i]}; minerr=error;
                }
            }
        }
        cout<<"Case "<<ca<<": "<<solution.first<<' '<<solution.second<<endl;
        ca++;
    }
    return 0;
}