#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<functional>
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;

#define mind(a,b) (a>b?b:a)
#define maxd(a,b) (a>b?a:b)
#define absd(x) (x<0?-(x):x)
#define pow2(x) ((x)*(x))
#define rep(i,n) for(int i=0; i<n; ++i)
#define repr(i,n) for(int i=n-1; i>=0; --i)
#define repl(i,s,n) for(int i=s; i<=n; ++i)
#define replr(i,s,n) for(int i=n; i>=s; --i)
#define repf(i,s,n,j) for(int i=s; i<=n; i+=j)
#define repe(e,obj) for(auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X,Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i,n,val) for(int i=0; i<n; ++i) cout << val << " "; cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 10000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
//typedef pair<ll, ll> P;
typedef pair<P, int> PI;
typedef pair<int, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P> > pvqueue;


#define N 100003
#define LEV 21

class LCADoubling {
  int n, lev;
  int kprv[LEV][N], de[N];

  inline void construct_kprv() {
    for(int k=0; k<lev; ++k) {
      for(int i=0; i<n; ++i) {
        if(kprv[k][i] == -1) continue;
        kprv[k+1][i] = kprv[k][kprv[k][i]];
      }
    }
  }

public:
  LCADoubling(int n) : n(n) {
    int n0 = 1;
    lev = 0;
    while(n0 < n) n0 <<= 1, ++lev;
  }
  void construct(int *prv, int *depth) {
    for(int i=0; i<n; ++i) kprv[0][i] = prv[i], de[i] = depth[i];
    construct_kprv();
  }

  void construct_from_graph(vector<int> g[N], int s) {
    bool used[N];
    queue<int> que;
    for(int i=0; i<n; ++i) used[i] = false;

    used[s] = true;
    kprv[0][s] = -1; de[s] = 0;
    que.push(s);
    while(!que.empty()) {
      int v = que.front(); que.pop();
      for(int i=0; i<g[v].size(); ++i) {
        int w = g[v][i];
        if(used[w]) continue;
        used[w] = true;
        kprv[0][w] = v; de[w] = de[v] + 1;
        que.push(w);
      }
    }
    construct_kprv();
  }

  int query(int u, int v) {
    int dd = de[v] - de[u];
    if(dd < 0) {
      swap(u, v);
      dd = -dd;
    }

    // de[u] <= de[v]
    for(int k=0; k<=lev; ++k) {
      if(dd & 1) {
        v = kprv[k][v];
      }
      dd >>= 1;
    }

    // de[u] == de[v]
    if(u == v) return u;

    for(int k=lev-1; k>=0; --k) {
      int pu = kprv[k][u], pv = kprv[k][v];
      if(pu != pv) u = pu, v = pv;
    }
    return kprv[0][u];
  }
};

int n, q;
bool used[N];
vector<int> g[N];

int main() {
  cin >> n;
  for(int i=0; i<n; ++i) {
    int k; cin >> k;
    while(k--) {
      int c; cin >> c;
      g[i].push_back(c);
    }
  }

  LCADoubling lca(n);
  lca.construct_from_graph(g, 0);

  cin >> q;
  while(q--) {
    int u, v; cin >> u >> v;
    int w = lca.query(u, v);
    cout << w << endl;
  }
  return 0;
}


Measured memory_bytes: 16076