// Slower C++ solution:
#include <iostream>

#include <vector>

#include <algorithm>

#define all(c) (c).begin(),(c).end()



#define maxv 100000



using namespace std;



int n,len;

vector<int> tree[maxv], in, out, up[100];



void dfs(int u, int p, int &k){

  in[u] = k++;

  up[0][u] = p;

  for(int i = 1; i < len; i++)

    up[i][u] = up[i - 1][up[i - 1][u]];



  for(int i = 0; i < tree[u].size(); i++){

    if(tree[u][i] != p)dfs(tree[u][i],u,k);

  }

  out[u] = k++;

}



void init(int root){

  len = 1;

  while((1 << len) <= n)len++;

  for(int i=0;i<len;i++){

    up[i].resize(n);

    fill(all(up[i]),0);

  }

  in.resize(n);

  out.resize(n);

  fill(all(in),0);

  fill(all(out),0);

  int k=0;

  dfs(root,root,k);

}



bool isParent(int parent, int child){

  return in[parent] <= in[child] && out[child] <= out[parent];

}



int lca(int a, int b){

  if(isParent(a,b))return a;

  if(isParent(b,a))return b;

  for(int i = len - 1; i >= 0; i--){

    if(!isParent(up[i][a],b))a = up[i][a];

  }

  return up[0][a];

}





int main(void){



  cin >> n;

  for(int i=0;i<n;i++){

    int k,c;

    cin >> k;

    for(int j=0;j<k;j++){

      cin >> c;

      tree[i].push_back(c);

    }

  }



  init(0);



  int q;

  cin >> q;

  while(q--){

    int u,v;

    cin >> u >> v;

    cout << lca(u,v) << endl;

  }

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.4404105744886464x):
#include<iostream>

#include<vector>

#include<algorithm>



#define REP(i, n) for (int i=0;i<int(n);++i)

#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)

#define EACH(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

#define ALL(A) A.begin(), A.end()



using namespace std;



struct HeavyLight {

  int pathCount,n;

  vector<int>size,parent,in,out,path,pathSize,pathPos,pathRoot;

  vector<vector<int> > tree;



  HeavyLight(int n,vector<vector<int> > t)

    :pathCount(0),n(n),size(n),parent(n),in(n),out(n),

     path(n),pathSize(n),pathPos(n),pathRoot(n),tree(t){

    int time=0;

    dfs(0,-1,time);

    buildPaths(0,newPath(0));

  }



  void dfs(int u, int p, int &k){

    in[u]=k++, parent[u]=p, size[u]=1;

    EACH(v,tree[u])if(*v!=p)dfs(*v, u, k),size[u]+=size[*v];

    out[u]=k++;

  }



  int newPath(int u){ pathRoot[pathCount]=u; return pathCount++; }



  void buildPaths(int u, int pt){

    path[u]=pt, pathPos[u]=pathSize[pt]++;

    EACH(v,tree[u])if(*v != parent[u])

      buildPaths(*v, 2*size[*v] >= size[u] ? pt : newPath(*v));

  }



  bool isAncestor(int p, int ch){

    return in[p] <= in[ch] && out[ch] <= out[p];

  }



  int lca(int a,int b){

    for(int root; !isAncestor(root=pathRoot[path[a]],b);a=parent[root]);

    for(int root; !isAncestor(root=pathRoot[path[b]],a);b=parent[root]);

    return isAncestor(a,b)?a:b;

  }

};



//Usage: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_5_C

int main(void){

  ios::sync_with_stdio(false);



  int n;

  cin >> n;

  vector<vector<int> >tree(n);

  

  REP(i,n){

    int k;

    cin >> k;

    REP(j,k){

      int ch;

      cin >> ch;

      tree[i].push_back(ch);

    }

  }



  HeavyLight hl = HeavyLight(n,tree);



  int q;

  cin >> q;

  while(q--){

    int u,v;

    cin >> u >> v;

    cout << hl.lca(u,v) << "\n";

  }

  

  return 0;

}