#include<iostream>
#include<sstream>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cfloat>
#include<functional>
#include<map>
#include<string>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<deque>
#include<set>
#include<bitset>
#include<list>
#include<numeric>
#include<complex>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<long long, long long> ll_ll;
typedef pair<double, double> d_d;
typedef vector<int> Vint;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for(i = 0;i < n;++i)
#define rep1(i, n) for(i = 1;i < n;++i)
#define rep2d(i, j, n) for(i = 0;i < n;++i)for(j = i + 1;j < n;++j)
#define per(i, n) for(i = n - 1;i > -1;--i)
#define int(x) int x; scanf("%d",&x)
#define int2(x, y) int x, y; scanf("%d%d",&x, &y)
#define int3(x, y, z) int x, y, z; scanf("%d%d%d",&x, &y, &z)
#define int4(v, x, y, z) int v, x, y, z; scanf("%d%d%d%d", &v, &x, &y, &z)
#define int5(v, w, x, y, z) int v, w, x, y, z; scanf("%d%d%d%d%d", &v, &w, &x, &y, &z)
#define ll2(x, y) ll x, y; cin >> x >> y;
#define scn(n, a) rep(i, n)cin >> a[i]
#define sc2n(n, a, b) rep(i, n)cin >> a[i] >> b[i]
#define pri(x) cout << x << "\n"
#define pri2(x, y) cout << x << " " << y << "\n"
#define pri3(x, y, z) cout << x << " " << y << " " << z << "\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define endl "\n"
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << x << "\n"
#define debug2(x, y) cout << " --- " << x << " " << y << "\n"
#define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\n"
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\n", x)


struct rmqsegtree{
  vector< pair<int, int> > t; //<data, at>
  pair<int, int> re;
  int n;
  void dflt(void){ re.first = INT_MAX; re.second = -1; return; }
  void use(void){
    int i; dflt();
    for(i = 0;;++i)if((1 << i) >= n)break;
    ++i; t.resize(1 << i); t[0].first = 1 << (i - 1); t[0].second = 1 << i;
    for(i = 1;i < t[0].second;++i)t[i] = re;
    for(i = 0;i < n;++i)t[i + t[0].first].second = i;
    return;
  }
  void build(vector<int> a){  //???????¨?
    for(n = 1;;n <<= 1)if(n >= a.size())break;
    t.resize(n << 1); dflt();
    t[0].first = n; t[0].second = t[0].first << 1; n = a.size();
    for(int i = 1;i < t[0].second;++i)t[i] = re;
    for(int i = 0;i < n;++i){
      t[t[0].first + i].first = a[i]; t[t[0].first + i].second = i;
    }
    for(int i = t[0].first - 1;i >= 1;--i)
      t[i] = (t[i << 1] <= t[(i << 1) + 1]) ? t[i << 1] : t[(i << 1) + 1];
    return;
  }
  void update(int at, int data){
    t[t[0].first + at].first = data;
    for(int x = t[0].first + at;x > 1;){
      x >>= 1;
      if(t[x << 1].first <= t[(x << 1) + 1].first)t[x] = t[x << 1];
      else t[x] = t[(x << 1) + 1];
    }
    return;
  }
  pair<int, int> query(int l, int r, int k, int kl, int kr){
    //printf("%d %d %d %d %d\n", l, r, k, kl, kr);
    if(l <= t[k].second && t[k].second < r)return t[k];
    if(k >= t.size())return re;
    if(kl >= r || kr <= l)return re;
    if(l <= kl && kr <= r)return t[k];
    pair<int, int> a = query(l, r, k << 1, kl, (kl + kr) >> 1);
    pair<int, int> b = query(l, r, (k << 1) + 1, (kl + kr) >> 1, kr);
    return (a.first < b.first) ? a : b;
  }
  pair<int, int> find(int l, int r){ return query(l, r, 1, 0, t[0].first); }
  void deb(void){
    int x = 1;
    printf("t[0] = (%d, %d)\n", t[0].first, t[0].second);
    for(int i = 1;i < t[0].second;++i){
      printf("%3d", t[i].first);
      if(i == (1 << x) - 1){ puts(""); ++x; }
    }
    x = 1;
    printf("t[0] = (%d, %d)\n", t[0].first, t[0].second);
    for(int i = 1;i < t[0].second;++i){
      printf("%3d", t[i].second);
      if(i == (1 << x) - 1){ puts(""); ++x; }
    }
    return;
  }
};

struct edge{
  int to, cost;
};

struct tree{
  vector< vector<edge> > t;
  int v;
  void nexttree(int x){ //x-indexed
    scanf("%d", &v);  t.resize(v);
    int from, to, cost = 1;
    for(int i = 1;i < v;++i){
      scanf("%d %d", &from, &to);
      t[from - x].push_back((edge){to - x, cost});
      t[to - x].push_back((edge){from - x, cost});
    }
    return;
  }
  void nexttree2(int x){
    scanf("%d", &v); t.resize(v);
    int k, to, cost = 1;
    for(int i = 0;i < v;++i){
      scanf("%d", &k);
      for(int j = 0;j < k;++j){
        scanf("%d", &to);
        t[i - x].push_back((edge){to - x, cost});
        t[to - x].push_back((edge){i - x, cost});
      }
    }
    return;
  }
  rmqsegtree S;
  vector<int> vst, dpth, id;
  void lcarmqdfs(int now, int dep){
    id[now] = vst.size();
    vst.push_back(now); dpth.push_back(dep);
    for(int i = 0;i < t[now].size();++i)if(id[t[now][i].to] == -1){
      lcarmqdfs(t[now][i].to, dep + 1);
      vst.push_back(now); dpth.push_back(dep);
    }
    return;
  }
  void lcarmq(void){
    if(vst.size() > v)return;
    id.resize(v); for(int i = 0;i < v;++i)id[i] = -1;
    lcarmqdfs(0, 0);
    S.build(vst);
    return;
  }
  int lca(int a, int b){
    if(id[a] > id[b])swap(a, b);
    return vst[S.find(id[a], id[b] + 1).second];
  }
};

signed main(void){
  int i, j, k;
  for(int testcase = 0;testcase >= 0;testcase++){
    tree T; T.nexttree2(0);
    T.lcarmq();

    int(q);
    for(;q--;){
      int2(a, b);
      int x = T.lca(a, b);
      pri(x);
    }

/*/

//*/ break;
  }
  return 0;
}

Measured memory_bytes: 16204