voidFloyd(){ for (int k = 1; k <= n; k ++) for (int i = 1; i <= n; i ++) for (int j = 1; j <= n; j ++){ map[i][j] = map[i][j] | (map[i][k] & map[k][j]); } }
voidFloyd(){ for (int k = 1; k <= n; k ++) for (int i = 1; i <= n; i ++) for (int j = 1; j <= n; j ++){ map[i][j] = map[i][j] | (map[i][k] & map[k][j]); } }
intmain(){ int u,v; char s[100]; while (cin >> n){ if (n == 0) break; memset(map,0,sizeof(map)); while (scanf("%d%d", &u, &v)){ if (u == 0 && v == 0) break; scanf("%s", s); //cout << s << endl; for (int i = 0; i < strlen(s); i ++){ map[u][v] |= (1<<(s[i] - 'a')); } } Floyd(); while (scanf("%d%d", &u, &v)){ if (u == 0 && v == 0) break; if (map[u][v]){ for (int i = 0; i <= 26; i ++){ if (map[u][v] & (1 << i)){ printf("%c", 'a' + i); } } puts(""); }else { puts("-"); } } puts(""); } return0; }