#include <bits/stdc++.h>
using namespace std;
#define FOR(I,X,Y) for(long long (I)=(X);(I)<(Y);(I)++)
#define REP(I,X,Y) for(long long (I)=(Y)-1;(I)>=(X);(I)--)
#define ALL(X) (X).begin(),(X).end()
#define pb push_back
#define COUNT(V,X) upper_bound((V).begin(),(V).end(),X)-lower_bound((V).begin(),(V).end(),X)
#define debug(x) cerr<<#x<<':'<<x<<endl;
#define DEBUG(v) cerr<<#v<<':';for(auto xXx:v)cerr<<xXx<<' ';cerr<<endl;
#define INF 1000000007
#define LINF 1000000000000000007
#define Yes(X) if(X){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
#define YES(X) if(X){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define ctoi(C) (C-'0')
typedef long long ll;
typedef long double ld;
long long dx[] = {1,0,-1,0};
long long dy[] = {0,1,0,-1};
long long dx8[] = {1,1,0,-1,-1,-1,0,1};
long long dy8[] = {0,1,1,1,0,-1,-1,-1};
long long dx9[] = {1,1,0,-1,-1,-1,0,1,0};

struct Graph{
  long long V;
  long long E;
  //u,v,weight,cap
  vector<tuple<long long,long long,long long,long long>> edges;
  void add(long long u,long long v){edges.push_back(make_tuple(u,v,1,1));}
  void add(long long u,long long v,long long weight){edges.push_back(make_tuple(u,v,weight,1));}
  void add(long long u,long long v,long long weight,long long capacity){edges.push_back(make_tuple(u,v,weight,capacity));}
};

struct HLD{
    Graph G;
    ll root;
    vector<ll> DFS;
    vector<ll> subtree;
    vector<ll> pre;

    ll dfs(ll n,const vector<vector<pair<ll,ll>>> &adj){
        DFS[n] = 0;
        ll ans = 1;
        for(auto p:adj[n]){
            if(DFS[p.first] == -1){
                ans += dfs(p.first,adj);
                pre[p.first] = n;
            }
        }
        return DFS[n] = ans;
    }

    void HLdfs(ll n,ll r,const vector<vector<pair<ll,ll>>> &adj){
        subtree[n] = r;
        ll MAX = 0;
        bool flag = true;
        for(auto p:adj[n]){
            if(DFS[p.first] < DFS[n]){
                MAX = max(DFS[p.first],MAX);
            }
        }
        for(auto p:adj[n]){
            if(MAX == DFS[p.first] && flag){
                HLdfs(p.first,r,adj);
                flag = false;
            }
            else if(DFS[p.first] < DFS[n]){
                HLdfs(p.first,p.first,adj);
            }
        }
    }

    void depthdfs(ll n,ll pre,ll depth,const vector<vector<pair<ll,ll>>> &adj){
        DFS[n] = depth;
        for(auto p:adj[n]){
            if(p.first != pre){
                depthdfs(p.first,n,depth+p.second,adj);
            }
        }
    }

    ll lca(ll u,ll v){
        while(subtree[u] != subtree[v]){
            if(DFS[subtree[u]] < DFS[subtree[v]]){
                v = pre[subtree[v]];
            }
            else{
                u = pre[subtree[u]];
            }
        }
        return (DFS[u] < DFS[v] ? u : v);
    }

    ll dist(ll u,ll v){
        ll LCA = lca(u,v);
        return DFS[u]+DFS[v]-2*DFS[LCA];
    }

    HLD(Graph G,ll root = 0) : G(G),root(root) {
        vector<vector<pair<ll,ll>>> adj;
        adj.resize(G.V);
        DFS.resize(G.V);
        pre.resize(G.V);
        subtree.resize(G.V);
        fill(ALL(DFS),-1);
        for(auto t:G.edges)adj[get<0>(t)].push_back(make_pair(get<1>(t),get<2>(t)));
        dfs(root,adj);
        HLdfs(root,root,adj);
        depthdfs(root,root,1,adj);
    }
};

signed main(){
    int n;
    cin >> n;
    Graph G;
    G.V = n;
    FOR(i,0,n){
        int k;
        cin >> k;
        FOR(j,0,k){
            int c;
            cin >> c;
            G.add(i,c);
            G.add(c,i);
        }
    }
    HLD hld(G);
    int q;
    cin >> q;
    FOR(i,0,q){
        int u,v;
        cin >> u >> v;
        cout << hld.lca(u,v) << endl;
    }
}


Measured memory_bytes: 38264