#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include<set>
#include <cmath>
#include<sstream>
#include<queue>
#include<vector>
#include<stdio.h>
#include<stack>
#include<deque>
#include<bitset>
#include<string>
#include<cstdio>
#include<map>
#include<iterator>
#include<iomanip>
#define prfs(x) printf("%s\n",x.c_str())
#define prfi(x) printf("%d\n",x);
#define sfi(X) scanf("%d",&X);
#define lop(i,n) for (int i = 0 ; i < n ;i++)
#define blop(i,n) for(int i = n-1 ; i>=0;i--)
#define M_P make_pair
#define All(X) (X).begin(),(X).end()
#define SZ(n) (int)(n).size()
#define voi vector<int>
#define vos vector<string>
#define vob vector<bool>
#define vovi vector<vector<int > >
#define vob vector<bool>
#define ll long long
using namespace std;
vector<int> AdjList[333];
int parent[333];
bool vis[333];
int s, t;
void printPath(int u,int p) {
if (u == p)
{ printf("%d", p);
return; }
printPath(parent[u],p);
printf(" %d",u);
}
bool BFS(int n,int tar) {
lop(i, 330)vis[i] = 0,parent[i] = i;
queue<int> q;
q.push(n);
while (!q.empty()) {
int u = q.front(); q.pop();
if (u == tar)return 1;
vis[u] = 1;
sort(All(AdjList[u - 1]));
for (int i = 0; i < AdjList[u].size(); i++) {
int v = AdjList[u][i];
if (!vis[v]) {
parent[v] = u;
q.push(v);
vis[v] = 1;
}
}
}
return 0;
}
int main()
{
//freopen("src.txt", "r", stdin);
int n;
while (scanf("%d", &n) == 1) {
for (int i = 0; i < 305;i++)AdjList[i].clear();
string ss;
for (int j = 1; j <= n; j++) {
cin >> ss;
for (int i = 2; i < ss.size(); i++) {
string A = "";
for (; ss[i] != ',' && i < ss.size();i++) {
A += ss[i];
}
if (A.size())
AdjList[j].push_back(atoi(A.c_str()));
}
}
int m;scanf("%d", &m);
printf("-----\n");
for (int j = 0;j < m; j++) {
scanf("%d%d", &s, &t);
if (BFS(s, t)) {
printPath(t, s);
printf("\n");
}
else {
printf("connection impossible\n");
}
}
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbH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