1. 


/*
* 436 - Arbitrage (II)
* Studiengruppe: IFB2C
* Martin Pesl
* Robert Reichart
* Elvin Uzeirovic
*
* Run Time Submission Date
* 0.728 2009-06-24 14:00:57
*/

import java.io.*;
import java.util.*;


public class Main {
public static void main(String... args) throws IOException{
new Main();
}

private Main() throws IOException{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String line = input.readLine();
int casenr = 0;
while(!line.equals("0")){
casenr++;
int currencies = Integer.parseInt(line);
float[][] d = new float[currencies][currencies];
ArrayList<String> toIndex = new ArrayList<String>();

for(int i = 0; i < currencies; i++){
toIndex.add(input.readLine());
}
int x,y;

for(int i = Integer.parseInt(input.readLine()); i > 0; i--){
String[] splited = input.readLine().split(" ");
x = toIndex.indexOf(splited[0]);
y = toIndex.indexOf(splited[2]);
d[x][y] = Float.parseFloat(splited[1]);
}

input.readLine();

for(int k = 0; k < currencies; k++){
for(int i = 0; i < currencies; i++){
for(int j = 0; j < currencies; j++){
if(d[i][k] > 0 && d[k][j] > 0){
if(d[i][j] < (d[i][k] * d[k][j])){
d[i][j] = (d[i][k] * d[k][j]);
}
}
}
}
}

boolean arbitage = false;
for(int k = 0; k < currencies; k++){
if(d[k][k] > 1)
arbitage = true;
}
System.out.printf("Case %d: ", casenr);
if(arbitage){
System.out.println("Yes");
}else{
System.out.println("No");
}

line = input.readLine();
}
}
}