//* Problem  : A - arbitrage
//* Contest  : UzhNU Local
//* Origin   : Ulm-1996
//* Type     : Solution
//* Date     : 2008.02.04
//* Author   : alt
//* Language : C++
//* Compiler : Microsoft Visual C++ 6.0
//* Algoritm : DFS

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

int n, m;

char dict[100][100];

double a[32][32];

double c[32];

int find(char *s)
{
	for (int i = 0; i < n; i++)
		if (!strcmp(dict[i], s)) return i;
	return 0;
}

char s1[100], s2[100], s[100];
double d;

int dfs(int v, double cc, int v0)
{
	if (cc < 0.000001) return 0;
	if (a[v][v0] * cc > 1) return 1;
	if (cc > c[v]) c[v] = cc; else return 0;
	for (int i = 0; i < n; i++)
		if (dfs(i, cc * a[v][i], v0)) return 1;
	return 0;
}

int main()
{
	int nt = 0;
	while (gets(s) && sscanf(s, "%d", &n) && n)
	{
		memset(a, 0, sizeof(a));
		nt++; printf("Case %d: ", nt);
		int i;
		for (i = 0; i < n; i++)
			gets(dict[i]);
			//scanf("%s", dict[i]);
		gets(s); sscanf(s, "%d", &m);
		for (i = 0; i < m; i++)
		{
			gets(s); sscanf(s, "%s %lf %s", s1, &d, s2);
			int c1 = find(s1), c2 = find(s2);
			a[c1][c2] = d;
		}
		int f = 0;
		for (i = 0; i < n; i++)
		{
			 memset(c, 0, sizeof(c));
			 if (dfs(i, 1, i)) {f = 1; break;}
		}
		if (f) printf("Yes\n"); else printf("No\n");
		gets(s);
	}
	return 0;
	} 