/*
* 入隊次數大於n說明一直在轉圈圈了,也就是找到了答案了。這裏SPFA要跑n次,
* 對於這題蒟蒻認爲不如Floyed寫的快,也可能沒發現好的做法把。
*/
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e2 + 10;
const int MAXM = 1e4 + 10;
struct E{
int to, nxt;
double w;
}edge[MAXM];
int tot;
int head[MAXN];
queue<int> q;
map<string, int> M;
double dis[MAXN];
bool inq[MAXN];
int inqtot[MAXN];
int n, m;
inline void clear(int s){
while(!q.empty()) q.pop();
memset(dis, 0, sizeof dis);
dis[s] = 1;
memset(inq, false, sizeof inq);
memset(inqtot, 0, sizeof inqtot);
}
inline void addedge(int u, int v, double w){
edge[tot].to = v;
edge[tot].w = w;
edge[tot].nxt = head[u];
head[u] = tot++;
}
inline bool SPFA(int s){
q.push(s);
inq[s] = true;
++inqtot[s];
while(!q.empty()){
int id = q.front();
q.pop();
inq[id] = false;
for(int i = head[id]; ~i; i = edge[i].nxt){
int to = edge[i].to;
double w = edge[i].w;
if(dis[id] * w > dis[to]){
dis[to] = dis[id] * w;
if(!inq[to]){
inq[to] = true;
++inqtot[to];
q.push(to);
if(inqtot[to] > n) return false;
}
}
}
}
return true;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//cout << setiosflags(ios::fixed) << setprecision(1); //保留小數點後1位
//cout << setprecision(1); //保留1位有效數字
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
int cas = 1;
while(cin >> n, n){
tot = 0;
memset(head, -1, sizeof head);
M.clear();
for(int i = 1; i <= n; ++i) {
string name;
cin >> name;
M[name] = i;
}
cin >> m;
for(int i = 1; i <= m; ++i){
string strs, stre;
double w;
cin >> strs >> w >> stre;
addedge(M[strs], M[stre], w);
}
bool findans = false;
for(int i = 1; i <= n; ++i){
clear(i); //別忘記了清空
if(!SPFA(i)){
findans = true;
break;
}
}
if(findans) cout << "Case " << cas++ << ": Yes" << endl;
else cout << "Case " << cas++ << ": No" << endl;
}
return 0;
}