#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"

struct HeavyLightDecomposition {
private:
    void dfs_sz(int v) {
        for(int &u : g[v]) {
            if (u == par[v]) swap(u, g[v].back());
        }
        if (~par[v]) g[v].pop_back();
        for(int &u : g[v]) {
            par[u] = v;
            dep[u] = dep[v] + 1;
            dfs_sz(u);
            sub[v] += sub[u];
            if (sub[u] > sub[g[v][0]]) swap(u, g[v][0]);
        }
    }
    
    void dfs_hld(int v, int c, int &idx) {
        vid[v] = idx++;
        inv[vid[v]] = v;
        type[v] = c;
        for(int &u : g[v]) {
            if (u == par[v]) continue;
            head[u] = ((u == g[v][0]) ? head[v] : u);
            dfs_hld(u, c, idx);
        }
    }
    
public:
    vector<vector<int>> g;
    vector<int> par, sub, dep, vid, inv, type, head;
    
    HeavyLightDecomposition() {}
    HeavyLightDecomposition(int n) {
        g.resize(n);
        par.resize(n, -1);
        dep.resize(n, 0);
        sub.resize(n, 1);
        vid.resize(n, -1);
        inv.resize(n);
        type.resize(n);
        head.resize(n);
    }
    
    void add_edge(int u, int v) {
        g[u].push_back(v);
        g[v].push_back(u);
    }
    
    void build(vector<int> rs = {0}) {
        int c = 0, idx = 0;
        for(int r : rs) {
            dfs_sz(r);
            head[r] = r;
            dfs_hld(r, c++, idx);
        }
    }
    
    int lca(int u, int v) {
        while(true) {
            if (vid[u] > vid[v]) swap(u, v);
            if (head[u] == head[v]) return u;
            v = par[head[v]];
        }
    }
    
    int distance(int u, int v) {
        return dep[u] + dep[v] - 2 * dep[lca(u, v)];
    }
    
    template<class F>
    void for_each(int u, int v, const F &f) {
        while(true) {
            if (vid[u] > vid[v]) swap(u, v);
            f(max(vid[head[v]], vid[u]), vid[v] + 1);
            if (head[u] == head[v]) break;
            v = par[head[v]];
        }
    }
    
    template<class T, class Q, class F>
    T for_each(int u, int v, T ti, const Q &q, const F &f) {
        T l = ti, r = ti;
        while(true) {
            if (vid[u] > vid[v]) {
                swap(u, v);
                swap(l, r);
            }
            l = f(l, q(max(vid[head[v]], vid[u]), vid[v] + 1));
            if (head[u] == head[v]) break;
            v = par[head[v]];
        }
        return f(l, r);
    }
    
    template<class F>
    void for_each_edge(int u, int v, const F &f) {
        while(true) {
            if (vid[u] > vid[v]) swap(u, v);
            if (head[u] != head[v]) {
                f(vid[head[v]], vid[v] + 1);
                v = par[head[v]];
            }
            else {
                if (u != v) f(vid[u] + 1, vid[v] + 1);
                break;
            }
        }
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    ll n;
    cin >> n;
    HeavyLightDecomposition hld(n);
    rep(i, n) {
        ll k;
        cin >> k;
        rep(j, k) {
            ll c;
            cin >> c;
            hld.add_edge(i, c);
        }
    }
    hld.build();
    ll q;
    cin >> q;
    rep(i, q) {
        ll u, v;
        cin >> u >> v;
        cout << hld.lca(u, v) << endl;
    }
    return 0;
}



Measured memory_bytes: 15208