#define INCLUDE_EULER_TOUR_HPP
#include <stack>
#include <vector>

struct EulerTour {
  using tree_type = std::vector<std::vector<int>>;
  tree_type T;
  std::vector<int> begin, end;
  std::vector<int> id, depth;

  EulerTour(int N) : T(N), begin(N), end(N) {}

  void add_edge(int s, int t) {
    T[s].push_back(t);
    T[t].push_back(s);
  }

  void build(int root) {
    id.reserve(T.size() * 3);
    depth.reserve(T.size() * 3);
    traverse(root);
    id.shrink_to_fit();
    depth.shrink_to_fit();
  }

  void traverse(int u, int p=-1, int d=0) {
    struct Frame {
      int u, p, d;
      int i = 0;
      Frame(int u, int p, int d) : u(u), p(p), d(d) {}
    };
    std::stack<Frame> s;

    s.emplace(u, p, d);
    while (!s.empty()) {
      auto& f = s.top();
      if (f.i == 0) begin[f.u] = id.size();
      end[f.u] = id.size();
      id.push_back(f.u);
      depth.push_back(f.d);

      bool call = false;
      while (f.i < T[f.u].size()) {
        int v = T[f.u][f.i];
        ++f.i;
        if (v != f.p) {
          s.emplace(v, f.u, f.d + 1);
          call = true;
          break;
        }
      }
      if (!call) s.pop();
    }
  }
};
#define INCLUDE_RMQ_HPP
#include <algorithm>

template <typename T>
struct RMQ {
  using Index = int;
  using BitIndex = int;

  const std::vector<T>& A;
  const int N, S, L;

  std::vector<int> lg;
  std::vector<BitIndex> small;
  std::vector<std::vector<Index>> large;

  bool f(Index a, Index b) const {
    if (A[a] != A[b]) return A[a] < A[b];
    return a < b;
  }
  RMQ(const std::vector<T>& A) : RMQ(A, A.size()) {}
  Index query(Index l, Index r) const {
    const int a = l / S, b = r / S;
    if (a == b) return query_small(l, r);

    Index ans = F(query_small(l, a * S + (S - 1)), query_small(b * S, r));
    if (a + 1 <= b - 1) ans = F(ans, query_large(a + 1, b - 1));
    return ans;
  }

private:
  Index F(Index a, Index b) const { return f(a, b) ? a : b; }

  RMQ(const std::vector<T>& A, int N) : RMQ(A, N, std::__lg(N) + 1) {}
  RMQ(const std::vector<T>& A, int N, int S) : RMQ(A, N, S, (N + S - 1) / S) {}
  RMQ(const std::vector<T>& A, int N, int S, int L) : RMQ(A, N, S, L, std::__lg(L) + 1) {}
  RMQ(const std::vector<T>& A, int N, int S, int L, int LG_L)
      : A(A), N(N), S(S), L(L), lg(1 << S), small(N), large(L, std::vector<Index>(LG_L)) {
    for (int i = 1, sz = lg.size(); i < sz; i++) {
      lg[i] = std::__lg(i);
    }
    for (int li = 0; li < L; li++) {
      int i = li * S;
      large[li][0] = i;
      for (int si = 0; si < S && i + si < N; si++) {
        large[li][0] = F(large[li][0], i + si);
      }
    }
    for (int x = 0; x < LG_L - 1; x++) {
      for (int li = 0; li + (1 << x) < L; li++) {
        large[li][x + 1] = F(large[li][x], large[li + (1 << x)][x]);
      }
    }
    std::vector<Index> st;
    for (int li = 0; li < L; li++) {
      int i = li * S;
      st.clear();
      for (int si = 0; si < S && i + si < N; si++) {
        while (!st.empty() && f(i + si, st.back())) st.pop_back();
        small[i + si] |= 1 << si;
        if (!st.empty()) small[i + si] |= small[st.back()];
        st.push_back(i + si);
      }
    }
  }
  int ntz(int x) const { return lg[x & -x]; }
  int bitmask(int l, int r) const { return ((1 << (r - l + 1)) - 1) << l; }
  Index query_small(Index l, Index r) const { return (l - l % S) + ntz(small[r] & bitmask(l % S, r % S)); }
  Index query_large(Index l, Index r) const {
    int x = lg[r - l + 1];
    return F(large[l][x], large[r - (1 << x) + 1][x]);
  }
};
#include <utility>

struct LCA {
  EulerTour euler_tour;
  const RMQ<int> rmq;

  LCA(EulerTour&& euler_tour) : euler_tour(euler_tour), rmq(euler_tour.depth) {}

  int query(int u, int v) const {
    int l = euler_tour.begin[u];
    int r = euler_tour.begin[v];
    if (r < l) std::swap(r, l);
    int x = rmq.query(l, r);
    return euler_tour.id[x];
  }
};

#include <cstdio>
#define mygc(c) (c) = getchar_unlocked()
#define mypc(c) putchar_unlocked(c)
template<typename T=int>inline T rd(){T x=0,m=0,k;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||'9'<k)break;x=x*10+k-'0';}return x;}
template<typename T=int>inline void wr(T x,char c='\n'){int s=0,m=0;char b[32];if(x<0)m=1,x=-x;for(;x;x/=10)b[s++]=x%10;if(!s)b[s++]=0;if(m)mypc('-');for(;s--;)mypc(b[s]+'0');mypc(c);}


int main() {
  int N = rd();
  EulerTour et(N);
  for (int i = 0; i < N; i++) {
    int k = rd();
    et.T[i].reserve(k);
    for (int j = 0; j < k; j++) {
      et.T[i].push_back(rd());
    }
  }
  et.build(0);
  LCA lca(std::move(et));

  int Q = rd();
  for (int i = 0; i < Q; i++) {
    int u = rd(), v = rd();
    wr(lca.query(u, v));
  }
  return 0;
}

Measured memory_bytes: 20712