// ?????¬???????????¬??????

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;

#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define int long long int

template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

// ?§?????????¨????????¨???????????????????¨???????????§???????
template <typename T>
struct Edge {
    int from, to;
    T cost;
    Edge(int s, T d) : to(s), cost(d) {}
    Edge(int f, int s, T d) : from(f), to(s), cost(d) {}

    bool operator<(const Edge &e) const {
        return cost < e.cost;
    }
    bool operator>(const Edge &e) const {
        return cost > e.cost;
    }
};

// HL ????§£
// ???????????? v ???????????? c_i ?????????????????????????????¨????????¨?????¨???????????°???
// ?????§???????????? 1 ?????? c_h ??¨?????????????????¨????????? (v, c_h) ??? "heavy" ????????§????????¨?????????
// ????????\???????????? "light" ????????§????????¨?????????

// ?????????????????? "heavy" ??¨ "light" ?????????????????¨?????¨??? "heavy" ??????????????????
// (????????????????????¨ chain) ???????§£ ("heavy" ??§???????????£???????????????????????¨???????????¨??????) ??§?????????
// ????????? HL ????§£??¨?????????

// chain ???????´????????????¨?????¨???????????? O(log N) ??§?????????
// ????????????"heavy" ?????????????????????????????? v ??¨ "light" ????????§?????°???????????? v ?????????????????????????????????
// ??¨?????¨?????§????????? subsize(v) / 2 ??\?????¨?????£??????????????¨???????°??????????
// "light" ??????????????????????????¨?????¨???????????????????????\???????????????????????±?????? O(log N) ??¨?????????

template <typename T>
using Graph = vector< vector< Edge<T> > >;

struct HLD {
    int N;
    const Graph<int> G;
    // ???????????¨????????????
    // ??????????????±????????????????????????????????????????????¨????????¨?????¨????????????
    // ???????????????????????????????????? "heavy" ??????
    vector<int> depth, parent, subsize, heavy;
    // ???chain ????????????
    // chain ?????????????´????????°?????´??????????????????? 1 ????¬??????????????´????chain ?????§?????????????????????
    vector<int> head, last, prev, next, chain, idx;
    // chain ????????± (?????? vector ????????????????´??????????????????? chain ????±???????)
    vector< vector<int> > chains;

    HLD(const Graph<int> &H, int r=-1) :
        N(H.size()), G(H), depth(N, -1), parent(N, 0), subsize(N, 0), heavy(N, -1),
        head(N), last(N), prev(N, -1), next(N, -1), chain(N, -1), idx(N, 0)
    {
        if(r != -1) decompose(r);
    }

    // root ????????¨??????????§£
    void decompose(const int root) {
        stack<int> st; st.push(root);
        parent[root] = -1;
        depth[root] = 0;        

        while(st.size()) {
            int cur_v = st.top(); st.pop();
            // ?????????????????????????¨???????
            if(cur_v >= 0) {
                st.push(~cur_v);
                for(auto e : G[cur_v]) {
                    // ??????????????§???????????°????????´??°
                    if(depth[e.to] != -1) continue;
                    depth[e.to] = depth[cur_v] + 1;
                    parent[e.to] = cur_v;
                    st.push(e.to);
                }
            }
            // ??°?????????
            else {
                int ma = 0;
                cur_v = ~cur_v;
                subsize[cur_v] = 1;
                for(auto e : G[cur_v]) {
                    if(parent[cur_v] == e.to) continue;
                    subsize[cur_v] += subsize[e.to];
                    if(ma < subsize[e.to]) {
                        // cur_v ??¨??¨?????¨????????????????????§??????????????¶
                        // (????????? "heavy" ??????)
                        ma = subsize[e.to];
                        next[cur_v] = e.to;
                    }
                }
            }
        }

        st.push(root);
        while(st.size()) {
            int cur_v = st.top(); st.pop();
            for(auto e : G[cur_v]) {
                if(parent[cur_v] != e.to) st.push(e.to);
            }

            if(chain[cur_v] != -1) continue;
            chains.push_back(vector<int>());
            vector<int> &path = chains.back();

            for(int v=cur_v; v!=-1; v=next[v]) path.push_back(v);

            for(size_t i=0; i<path.size(); i++) {
                int v = path[i];
                head[v] = path.front(), last[v] = path.back();
                prev[v] = (i != 0 ? path[i-1] : -1);
                next[v] = (i+1 != path.size() ? path[i+1] : -1);
                chain[v] = (int)chains.size() - 1;
                idx[v] = i;
            }
        }
    }

    // ?????? v ????????????????????? chain ????±?????????????
    // ???????????? chain ????????§????????????????????????????????? pair ??§??????
    pair<int, int> get_index(int v) {
        return make_pair(chain[v], idx[v]);
    }

    // v ????±??????? chain ?????????????????? (head[v] ???????????? -1 ?????????)
    int climb(int v) {
        return parent[ head[v] ];
    }

    // ?????? u ??¨ v ???????°???±????\????
    int lca(int u, int v) {
        while(chain[u] != chain[v]) {
            if(depth[head[u]] < depth[head[v]]) v = climb(v);
            else u = climb(u);
        }
        return depth[u] < depth[v] ? u : v;
    }
};

signed main() {
    int N; cin >> N;
    Graph<int> G(N);
    rep(i,0,N) {
        int K; cin >> K;
        while(K--) {
            int child; cin >> child;
            G[i].push_back(Edge<int>(child, 1));
            G[child].push_back(Edge<int>(i, 1));
        }
    }

    HLD hl(G, 0);
    int Q; cin >> Q;

    /*
    rep(i,0,N) {
        pii res = hl.get_index(i);
        printf("vertex %lld: chain = %lld, index = %lld\n", i+1, res.first, res.second);
    }
    */

    rep(i,0,Q) {
        int u, v; cin >> u >> v;
        cout << hl.lca(u, v) << endl;
    }
    return 0;
}

Measured memory_bytes: 32260