#include <bits/stdc++.h>
#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it,X) for(auto it=(X).begin();it!=(X).end();it++)
#define numa(x,a) for(auto x: a)
#define ite iterator
#define mp make_pair
#define mt make_tuple
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define pb push_back
#define pf push_front
#define sec second
#define sz(x) ((int)(x).size())
#define ALL( c ) (c).begin(), (c).end()
#define gcd(a,b) __gcd(a,b)
#define endl "\n"
using namespace std;
template <int POS, class TUPLE> void deploy(std::ostream &os, const TUPLE &tuple){}
template <int POS, class TUPLE, class H, class ...Ts> void deploy(std::ostream &os, const TUPLE &t){ os << (POS == 0 ? "" : ", ") << get<POS>(t); deploy<POS + 1, TUPLE, Ts...>(os, t); }
template <class T> std::ostream& operator<<(std::ostream &os, std::vector<T> &v){ int remain = v.size(); os << "{"; for(auto e: v) os << e << (--remain == 0 ? "}" : ", "); return os; }
template <class T> std::ostream& operator<<(std::ostream &os, std::set<T> &v){ int remain = v.size(); os << "{"; for(auto e: v) os << e << (--remain == 0 ? "}" : ", "); return os; }
template <class T, class K> std::ostream& operator<<(std::ostream &os, std::map<T, K> &mp){ int remain = mp.size(); os << "{"; for(auto e: mp) os << "(" << e.first << " -> " << e.second << ")" << (--remain == 0 ? "}" : ", "); return os; }
#define DEBUG1(var0) { std::cerr << (#var0) << "=" << (var0) << endl; }
#define DEBUG2(var0, var1) { std::cerr << (#var0) << "=" << (var0) << ", ";DEBUG1(var1); }
#define DEBUG3(var0, var1, var2) { std::cerr << (#var0) << "=" << (var0) << ", ";DEBUG2(var1,var2); }
#define DEBUG4(var0, var1, var2, var3) { std::cerr << (#var0) << "=" << (var0) << ", ";DEBUG3(var1,var2,var3); }
typedef long long ll;

/*
//*********************************
//O(n)のLCA
#define MAX_V 100100
vector <int> edge[MAX_V];
int root;
int parent[MAX_V];
int depth[MAX_V];
int V;
void dfs(int v,int p,int d){
  parent[v] = p;
  depth[v] = d;
  for(int i = 0;i < sz(edge[v]);i++){
    if(edge[v][i] != p){
      dfs(edge[v][i],v,d+1);
    }
  }
}

void init(){
  //parentとdepthを初期化
  dfs(root,-1,0);
}

int LCA(int u,int v){//uとvのLCAを求める
  while(depth[u] > depth[v]){
    u = parent[u];
  }
  while(depth[v] > depth[u]){
    v = parent[v];
  }
  while(u != v){
    u = parent[u];
    v = parent[v];
  }
  return u;
}
//*********************************

*/

//*********************************
//O(log n)アルゴリズム
//O(nlogn)のメモリが必要
#define MAX_V 100100
#define MAX_LOG_V 32
vector <int> edge[MAX_V];
int root;
int parent[MAX_LOG_V][MAX_V];
int depth[MAX_V];
int V;

void dfs(int v,int p,int d){
  parent[0][v] = p;
  depth[v] = d;
  for(int i = 0;i < edge[v].size();i++){
    if(edge[v][i] != p){
      dfs(edge[v][i],v,d+1);
    }
  }
}

void init(){
  //parent[0]とrootを初期化
  dfs(root,-1,0);
  //parentを初期化する
  for(int k = 0;k+1<MAX_LOG_V;k++){
    for(int v = 0; v < V;v++){
      if(parent[k][v] < 0){
        parent[k+1][v] = -1;
      }else{
        parent[k+1][v] = parent[k][parent[k][v]];
      }
    }
  }
}

int LCA(int u,int v){
  if(depth[u] > depth[v]){
    swap(u,v);
  }
  
  for(int k = 0; k < MAX_LOG_V;k++){
    if((depth[v]-depth[u]) >> k & 1){
      v = parent[k][v];
    }
  }
  if(u == v){
    return u;
  }
  for(int k = MAX_LOG_V-1;k >= 0;k--){
    if(parent[k][u] != parent[k][v]){
      u = parent[k][u];
      v = parent[k][v];
    }
  }
  return parent[0][u];
}

//*********************************


int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> V;
  rep(i,V){
    int x;
    cin >> x;
    rep(j,x){
      int y;
      cin >> y;
      edge[i].pb(y);
    }
  }
  root = 0;
  init();
  int Q;
  cin >> Q;
  rep(i,Q){
    int x,y;
    cin >> x >> y;
    cout << LCA(x,y) << endl;
  }
  return 0;
}

Measured memory_bytes: 24264