#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define rreps(i, n) for (int i = int(n); i >= 1; i--)
#define repc(i, n) for (int i = 0; i <= int(n); i++)
#define rrepc(i, n) for (int i = int(n); i >= 0; i--)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define repic(i, a, b) for (int i = int(a); i <= int(b); i++)
#define all(a) (a).begin(), (a).end()
#define bit32(x) (1 << (x))
#define bit64(x) (1ll << (x))
#define sz(v) ((int) v.size())

using namespace std;

using i64 = long long;
using f80 = long double;
using vi32 = vector<int>;
using vi64 = vector<i64>;
using vf80 = vector<f80>;
using vstr = vector<string>;

void yes() { cout << "Yes" << endl; exit(0); }
void no() { cout << "No" << endl; exit(0); }
template <typename T> class pqasc : public priority_queue<T, vector<T>, greater<T>> {};
template <typename T> class pqdesc : public priority_queue<T, vector<T>, less<T>> {};
template <typename T> void amax(T &x, T y) { x = max(x, y); }
template <typename T> void amin(T &x, T y) { x = min(x, y); }
template <typename T> T exp(T x, i64 n, T e = 1) { T r = e; while (n > 0) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; }
template <typename T> istream& operator>>(istream &is, vector<T> &v) { for (auto &x : v) is >> x; return is; }
template <typename T> ostream& operator<<(ostream &os, vector<T> &v) { rep(i, v.size()) { if (i) os << ' '; os << v[i]; } return os; }
void solve(); int main() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(16); solve(); return 0; }

const int INF = 1001001001;
const int dx[] = {0, -1, 0, 1};
const int dy[] = {1, 0, -1, 0};

struct LowestCommonAncestor {
  vector<vector<int>> g;
  int n;
  int l = 0;
  vector<vector<int>> par;
  vector<int> dep;
  LowestCommonAncestor(int n) : n(n), g(n), dep(n) {
    while ((1 << l) < n) l++;
    par.assign(n + 1, vector<int>(l, n));
  }
  void add_edge(int u, int v) {
    g[u].push_back(v);
    g[v].push_back(u);
  }
  void build(int root = 0) {
    auto dfs = [&](auto dfs, int u, int p, int d) -> void {
      if (p != -1) par[u][0] = p;
      dep[u] = d;
      for (auto v : g[u]) if (v != p) {
        dfs(dfs, v, u, d + 1);
      }
    };
    dfs(dfs, root, -1, 0);
    for (int i = 0; i < l - 1; i++) {
      for (int u = 0; u < n; u++) {
        par[u][i + 1] = par[par[u][i]][i];
      }
    }
  }
  int operator()(int u, int v) {
    if (dep[u] < dep[v]) swap(u, v);
    int gap = dep[u] - dep[v];
    for (int i = l - 1; i >= 0; i--) {
      if ((1 << i) & gap) u = par[u][i];
    }
    if (u == v) return u;
    for (int i = l - 1; i >= 0; i--) {
      int pv = par[u][i];
      int pu = par[v][i];
      if (pv != pu) u = pv, v = pu;
    }
    return par[u][0];
  }
  int dist(int u, int v) {
    int a = (*this)(u, v);
    return dep[u] + dep[v] - dep[a] * 2;
  }
};

void solve() {
  int n;
  cin >> n;
  LowestCommonAncestor lca(n);
  rep(i, n) {
    int k;
    cin >> k;
    rep(j, k) {
      int v;
      cin >> v;
      lca.add_edge(i, v);
    }
  }
  lca.build();
  int q;
  cin >> q;
  rep(i, q) {
    int a, b;
    cin >> a >> b;
    cout << lca(a, b) << endl;
  }
}



Measured memory_bytes: 21976