1. 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, SS11
* Problem: 436 Arbitrage (II)
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=6&page=show_problem&problem=377
*
* @author Burgmair Stefan
* @author YYY
* @version 1.0, 02/05/2011
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 0.772
*/

public class Main
{
static int nmbrOfCase = 1;
public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
do{
do{
double [][] exchangeRates;
String [] currencys;
boolean arbitrageIsPossible = false;

int nmbrOfCurrencys = Integer.parseInt(reader.readLine());

if(nmbrOfCurrencys != 0){

currencys = new String [nmbrOfCurrencys];
for (int i = 0; i < nmbrOfCurrencys; i++){
currencys[i] = reader.readLine();
}
int nmbrOfExchangeRates = Integer.parseInt(reader.readLine());
exchangeRates = new double[nmbrOfCurrencys][nmbrOfCurrencys];
//changeDone = new boolean [nmbrOfExchangeRates];

for (int i = 0; i < nmbrOfExchangeRates; i++){
//printMap(exchangeRates);
String [] arr = reader.readLine().split(" ");
for (int j = 0; j < nmbrOfCurrencys; j++){
if (currencys[j].equals(arr[0]))
for (int k = 0; k < nmbrOfCurrencys; k++){
if (currencys[k].equals(arr[2])){
exchangeRates [j][k] = Double.parseDouble(arr[1]);
}
else if(exchangeRates [j][k] == 0)
exchangeRates [j][k] = -1;
}
}
}
//printMap(exchangeRates);

for (int x = 0; x < nmbrOfCurrencys; x++){
for (int a = 0; a < nmbrOfCurrencys; a++){
if (exchangeRates[a][x] != -1){
for (int e = 0; e < nmbrOfCurrencys; e++){
if(exchangeRates[x][e] != -1){
if(exchangeRates[a][e] == -1 || exchangeRates[a][e] < exchangeRates[a][x] * exchangeRates[x][e]){
exchangeRates[a][e] = exchangeRates[a][x] * exchangeRates[x][e];
}
}
}
}
}
}
//printMap(exchangeRates);
for (int i = 0; i < nmbrOfCurrencys; i++){
if(exchangeRates[i][i] > 1.0001)
arbitrageIsPossible = true;
}
if (reader.ready())
reader.readLine();
if(arbitrageIsPossible)
System.out.println("Case " + nmbrOfCase + ": Yes");
else
System.out.println("Case " + nmbrOfCase + ": No");
}

arbitrageIsPossible = false;

nmbrOfCase ++;
}while (reader.ready());
}while (reader.ready());
}

public static void printMap(double[][] exchangeRates)
{
for (int x = 0; x < exchangeRates.length; x ++)
{
for (int y = 0; y < exchangeRates.length; y ++)
{
System.out.print(exchangeRates[x][y]);
if (y + 1 < exchangeRates.length)
System.out.print(" ");
}
if (x + 1 < exchangeRates.length)
System.out.println();
}
System.out.println();
}
}