#include <bits/stdc++.h>

using namespace std;

#define int long long
#define double long double
#define FOR(i, a, b) for(ll i = (a); i < (b); ++i)
#define FORR(i, a, b) for(ll i = (a); i > (b); --i)
#define REP(i, n) for(ll i = 0; i < (n); ++i)
#define REPR(i, n) for(ll i = n; i >= 0; i--)
#define FOREACH(x, a) for(auto &(x) : (a))
#define VECCIN(x)                                                              \
    for(auto &youso_ : (x)) cin >> youso_
#define bitcnt(x) __builtin_popcount(x)
#define lbit(x) __builtin_ffsll(x)
#define rbit(x) __builtin_clzll(x)
#define SZ(x) ((ll)(x).size())
#define fi first
#define se second
#define All(a) (a).begin(), (a).end()
#define rAll(a) (a).rbegin(), (a).rend()

template <typename T = long long> inline T IN() {
    T x;
    cin >> x;
    return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&... tail) {
    cin >> head;
    CIN(move(tail)...);
}
#define CCIN(...)                                                              \
    char __VA_ARGS__;                                                          \
    CIN(__VA_ARGS__)
#define DCIN(...)                                                              \
    double __VA_ARGS__;                                                        \
    CIN(__VA_ARGS__)
#define LCIN(...)                                                              \
    ll __VA_ARGS__;                                                            \
    CIN(__VA_ARGS__)
#define SCIN(...)                                                              \
    string __VA_ARGS__;                                                        \
    CIN(__VA_ARGS__)
#define Yes(a) cout << (a ? "Yes" : "No") << "\n"
#define YES(a) cout << (a ? "YES" : "NO") << "\n"
#define Printv(v)                                                              \
    {                                                                          \
        FOREACH(x, v) { cout << x << " "; }                                    \
        cout << "\n";                                                          \
    }
template <typename T = string> inline void eputs(T s) {
    cout << s << "\n";
    exit(0);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
    std::fill((T *)array, (T *)(array + N), val);
}

template <typename T> using PQG = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using PQ = priority_queue<T>;

typedef long long ll;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<ll, ll> PL;
typedef vector<PL> VPL;
typedef vector<bool> VB;
typedef vector<double> VD;
typedef vector<string> VS;

const int INF = 1e9;
const int MOD = 1e9 + 7;
// const int MOD = 998244353;
const ll LINF = 1e18;
// const double PI = atan(1.0) * 4.0;
const ll dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
#define PI 3.141592653589793238

// rmq build:n query:logn
struct LCA {
    const int lg = 12;
    const int sz = 1 << lg;
    const int ms = sz - 1;
    int n;
    vector<int> P, D, E, A, B, T, ht;
    vector<vector<int>> G, dat;
    LCA(int n)
        : n(n), P(n, -1), D(n), E(n * 2, 0), A(n * 2, -1), B(n * 2 / lg + 1), T(sz, 0), G(n) {}

    void add_edge(int u, int v) {
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }

    void dfs(int v, int p, int d, int &k) {
        D[v] = k;
        A[k] = P[v] = p;
        E[k++] = d;
        for(int u : G[v])
            if(u != p) dfs(u, v, d + 1, k);
        A[k] = P[v];
        E[k++] = d - 1;
    }

    // if it need leftmost, then add: if(E[i]==E[j]) return i<j?i:j;
    inline int comp(int i, int j) { return E[i] < E[j] ? i : j; };
    inline int comp(int i, int j, int k) { return comp(comp(i, j), k); };

    void build(int r = 0) {
        int k = 0;
        dfs(r, -1, 1, k);

        B[0] = 1;
        for(int i = 1; i < n * 2; i++)
            B[i / lg] |= (E[i - 1] < E[i]) << (i % lg);

        for(int b = 0; b < sz; b++) {
            int e = 0, w = 1, &x = T[b];
            for(int i = 0; i < lg; i++) {
                if((b >> i) & 1)
                    e++;
                else
                    e--;
                if(e < w) e = w, x = i;
            }
        }

        int m = (n * 2 + lg - 1) / lg;
        int h = 1;
        while((1 << h) < m) h++;
        dat.assign(h, vector<int>(m, -1));
        ht.assign(m + 1, 0);
        for(int j = 2; j <= m; j++) ht[j] = ht[j >> 1] + 1;

        for(int j = 0; j < n * 2; j++) {
            if(dat[0][j / lg] < 0) dat[0][j / lg] = j;
            dat[0][j / lg] = comp(dat[0][j / lg], j);
        }

        for(int i = 1, p = 1; i < h; i++, p <<= 1)
            for(int j = 0; j < m; j++)
                dat[i][j] = comp(dat[i - 1][j], dat[i - 1][min(j + p, m - 1)]);
    }

    inline int cs(int a, int b) {
        int l = b - a;
        return comp(dat[ht[l]][a], dat[ht[l]][b - (1 << ht[l])]);
    }

    inline int es(int i, int l, int r) {
        int x = r - i * lg + 1, y = l - i * lg;
        int b = (((B[i] | (ms << x)) >> y) | (ms << (lg - y))) & ms;
        return l + T[b];
    }

    inline int ls(int i, int l) {
        int k = l - i * lg;
        int b = ((B[i] >> k) | (ms << (lg - k))) & ms;
        return l + T[b];
    }

    inline int rs(int j, int r) {
        int k = r - j * lg + 1;
        int b = (B[j] | (ms << k)) & ms;
        return j * lg + T[b];
    }

    inline int rmq(int l, int r) {
        int i = l / lg, j = r / lg;
        if(i == j) return es(i, l, r);
        if(i + 1 == j) return comp(ls(i, l), rs(j, r));
        return comp(ls(i, l), cs(i + 1, j), rs(j, r));
    }

    int lca(int l, int r) {
        if(l == r) return l;
        if(D[l] > D[r]) swap(l, r);
        int x = D[l], y = D[r];
        int m = rmq(x, y);
        return m == x ? l : A[m];
    }
};

signed main() {
    LCIN(n);
    LCA lca(n);
    REP(i, n) {
        LCIN(k);
        REP(j, k) {
            LCIN(c);
            lca.add_edge(i, c);
        }
    }
    lca.build();
    LCIN(q);
    REP(i, q) {
        LCIN(u, v);
        cout << lca.lca(u, v) << "\n";
    }
}


Measured memory_bytes: 19764