/*
http://acm.pku.edu.cn/JudgeOnline/problem?id=2240
求正权回路
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<set>
#include<queue>
#include<map>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<functional>
using namespace std;
const int INF=(1<<31)-1;
const double INF_D=1e8;
const double EPS=1e-8;
const int N=50;
double path[N][N];
int n,m;
bool spfa(int s)
{
double d[N];
for(int i=1;i<=n;i++)d[i]=0;
d[s]=1000.0;
bool v[N];
memset(v,false,sizeof(v));
v[s]=true;
queue<int>q;
q.push(s);
while(!q.empty())
{
int t=q.front();
q.pop();
v[t]=false;
if(d[s]>1000.0)return true;
for(int i=1;i<=n;i++)
{
if(d[i]<d[t]*path[t][i])
{
d[i]=d[t]*path[t][i];
if(!v[i])
{
v[i]=true;
q.push(i);
}
}
}
}
return d[s]>1000.0;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T=1;
while(cin>>n,n)
{
string name1,name2;
double r;
map<string,int> name;
int i,j;
for(i=1;i<=n;i++)
{
cin>>name1;
name[name1]=i;
}
cin>>m;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
path[i][j]=0;
while(m--)
{
cin>>name1>>r>>name2;
path[name[name1]][name[name2]]=r;
}
printf("Case %d: %s/n",T++,spfa(1)?"Yes":"No");
}
//system("pause");
return 0;
}