将最短路条件改成mp[i][j] = mp[i][j] | (mp[i][k] & mp[k][j]);
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double PI = acos(-1.0);
template <class T> inline T MAX(T a, T b){if (a > b) return a;return b;}
template <class T> inline T MIN(T a, T b){if (a < b) return a;return b;}
const int N = 111;
const int M = 11111;
const LL MOD = 1000000007LL;
const int dir[4][2] = {1, 0, -1, 0, 0, -1, 0, 1};
const int INF = 0x3f3f3f3f;
int mp[222][222];
int main()
{
int n;
while (scanf("%d", &n) != EOF && n)
{
int i, j, k, u, v, bit;
memset(mp, 0, sizeof(mp));
char str[100];
while (1)
{
scanf("%d%d", &u, &v);
if (u == 0 && v == 0) break;
scanf("%s", str);
int l = strlen(str);
bit = 0;
for (j = 0; j < l; ++j)
{
bit |= (1 << (str[j] - 'a'));
}
mp[u][v] = bit;
}
for (k = 1; k <= n; ++k)
for (i = 1; i <= n; ++i)
for (j = 1; j <= n; ++j)
{
mp[i][j] = mp[i][j] | (mp[i][k] & mp[k][j]);
}
while (1)
{
scanf("%d%d", &u, &v);
if (u == 0 && v == 0) break;
if (mp[u][v] == 0)
{
printf("-\n");
}
else
{
for (i = 0; i < 26; ++i)
if (mp[u][v] & (1 << i)) printf("%c",i + 'a');
printf("\n");
}
}
printf("\n");
}
return 0;
}