#include <bits/stdc++.h>
 
const double pi = 3.141592653589793238462643383279;
using namespace std;
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
 
 
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a)*(a))
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
 
 
//repetition
//------------------------------------------
#define FOR(i,s,n) for(int i=s;i<(int)n;++i)
#define REP(i,n) FOR(i,0,n)
#define MOD 1000000007
 
 
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
 
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const double EPS = 1E-10;
 
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)


//ここから編集

//RMQ + LCA
template<typename T = int> struct SegmentTree{
private:
  // [val, index]
  vector<pair<T, int>> node;
  int n, sz;

public:
  void resize(vector<T>& v){
    sz = v.size();
    n = 1;
    while(n < sz) n*=2;
    node.resize(2*n);
    for(int i=0; i<sz; i++){
      node[i+n] = make_pair(v[i], i);
    }
    for(int i=n-1; i>=1; i--){
      node[i] = min(node[2*i], node[2*i+1]);
    }
  }

  void update(int k, T a){
    k += n;
    
    node[k] = make_pair(a, k);
    while(k >>= 1){
      node[k] = min(node[2*k], node[2*k+1]);
    }
  }

  // [a, b)について求める
  pair<T, int> query(int a, int b){

    pair<T, int> res1 = make_pair(numeric_limits<T>::max(), -1);
    pair<T, int> res2 = make_pair(numeric_limits<T>::max(), -1);

    a += n, b += n;
    while(a != b){
      if(a % 2) res1 = min(res1, node[a++]);
      if(b % 2) res2 = min(res2, node[--b]);
      a >>= 1, b >>= 1;
    }
    return min(res1, res2);
  }
};

struct LCA{
private:
  int V;
  vector<vector<int>> G;
  vector<int> ord, depth, id;
  SegmentTree<int> st;
  
  void dfs(int u, int p, int k){
    id[u] = (int)ord.size();
    ord.push_back(u);
    depth[u] = k;
    for(int v: G[u]){
      if(v != p){
        dfs(v, u, k+1);
        ord.push_back(u);
      }
    }
  }
public:

  LCA(int node_size) : V(node_size), G(V), depth(V), id(V, -1){}

  void add_edge(int from, int to){
    G[from].push_back(to);
    G[to].push_back(from);
  }

  void build(){
    ord.reserve(2*V-2);
    for(int i=0; i<V; i++){
      if(id[i] < 0){
        dfs(i, -1, 0);
      }
    }

    vector<int> stvec(2*V-2);
    for(int i=0; i<2*V-2; i++){
      stvec[i] = depth[ord[i]];
    }
    st.resize(stvec);
  }

  int lca(int u, int v){
    return ord[st.query(min(id[u], id[v]), max(id[u], id[v])+1).second];
  }

  int dist(int u, int v){
    int z = lca(u, v);
    return depth[u] + depth[v] - 2 * depth[z];
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  int n; cin >> n;
  LCA lca(n);
  REP(i,n){
    int k; cin >> k;
    REP(j,k){
      int a; cin >> a;
      lca.add_edge(i,a);
    }
  }
  lca.build();
  int q;
  cin >> q;
  while(q--){
    int a, b; cin >> a >> b;
    cout << lca.lca(a, b) << endl;
  }
  

  return 0;
}


Measured memory_bytes: 17784