/*
一开始题目没有看清楚,题目求的是最长路,而不是最短路
注释的代码是错的,题目要看清楚
*/
#include <iostream>//2566050 2010-07-02 12:58:09 Accepted 1217 31MS 296K 1755 B C++ 悔惜晟
#include <queue>
#include <cstdio>
#include <map>
#include <string>
#include <cstring>
using namespace std;
map<string, int > hash;
double cost[35][35];
bool hash1[35];
const int MAX = 0x3fffffff;
int n;
void Floyd()//还不是很理解的
{
for(int i = 1;i <= n; i++){
for(int j = 1;j <= n; j++){
for(int k = 1;k <= n; k++){
if(cost[j][i] * cost[i][k] > cost[j][k])
cost[j][k] = cost[j][i] * cost[i][k];
}
}
}
}
/*
struct node
{
double mon;
int h;
friend bool operator < (node a, node b)
{
return a.mon < b.mon;
}
};
void BFS(string ss)
{
priority_queue<node> Q;
node P, N;
for(int i = 1; i <= n; i++)
{
if(cost[hash[ss]][i] != MAX)
{
P.h = i;
P.mon = cost[hash[ss]][i];
Q.push(P);
}
}
while(!Q.empty())
{
P = Q.top();
Q.pop();
if(P.h == hash[ss])
{
if(P.mon > 1)
printf("Yes/n");
else
printf("No/n");
return ;
}
hash1[P.h] = true;
for(int i = 1; i <= n; i++)
{
if(!hash1[i] && cost[P.h][i] != MAX)
{
N.h = i;
N.mon = P.mon * cost[P.h][i];
Q.push(N);
}
}
}
}
*/
int main()
{
int m;
double dis;
char s[35], en[35];
string ss;
int t = 1;
//string ss, en;
while(scanf("%d", &n) != EOF && n)
{
for(int i = 1; i <= n; i++)
{
scanf("%s", s);
hash[s] = i;
}
ss = s;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
cost[i][j] = 1;
scanf("%d", &m);
while(m--)
{
scanf("%s %lf %s", s, &dis, en);
{
cost[hash[s]][hash[en]] = dis;
}
}
//BFS(ss);
Floyd();
if(cost[hash[ss]][hash[ss]] > 1)
printf("Case %d: Yes/n", t++);
else
printf("Case %d: No/n", t++);
}
}