floyd
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <cstring>
#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[33][33];
int main()
{
int cas = 1, m;
while (scanf("%d", &m) != EOF)
{
int i, j, k, u, v, w;
for (i = 1; i <= 20; ++i)
{
fill(mp[i] + 1, mp[i] + 21, INF);
}
for (i = 0; i < m; ++i)
{
scanf("%d", &v);
mp[1][v] = 1;
}
for (i = 2; i <= 19; ++i)
{
scanf("%d", &m);
for (j = 0; j < m; ++j)
{
scanf("%d", &v);
mp[i][v] = 1;
mp[v][i] = 1;
}
}
int n = 20;
for (k = 1; k <= n; ++k)
for (i = 1; i <= n; ++i)
for (j = 1; j <= n; ++j)
{
if (mp[i][j] > mp[i][k] + mp[k][j])
mp[i][j] = mp[i][k] + mp[k][j];
}
scanf("%d", &m);
printf("Test Set #%d\n", cas++);
for (i = 0; i < m; ++i)
{
scanf("%d%d", &u, &v);
printf("%d to %d: %d\n", u, v, mp[u][v]);
}
printf("\n");
}
return 0;
}