#include <bits/stdc++.h>
using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

#define en "\n"

const double EPS = 1e-9;
const double PI  = acos(-1.0);
const int INF = 2147483647;
const long long LINF = 1LL<<60;
const long long MOD = 1000000007; // 998244353

#define CLR(a) memset((a), 0, sizeof(a))

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

struct Node{
    int id;
    int v;
    bool done = false;
    Node(int id, int v=1e9): id(id), v(v) {}
    Node(): id(0), v(1e9) {}
};

bool operator>(Node a, Node  b){
    return a.v > b.v;
}

struct Edge{
    int from, to, id;
    int cost;
    Edge(int from, int to, int cost=1, int id=-1): from(from), to(to), cost(cost), id(id) {}
};

struct Graph{
    int N;
    vector<vector<Edge>> adj;
    vector<Node> node;

    Graph(int N): N(N) {
        adj.assign(N, vector<Edge>());
        node.assign(N, Node());
        for(int i=0; i<N; ++i) node[i].id = i;
    }

    Graph() {}

    void add_edge(int i, int j, int cost=1, int id=-1){
        Edge e(i, j, cost, id);
        adj[i].emplace_back(e);
    }

    vector<Edge> get_adj(Node node){
        return adj[node.id];
    }

    void assign(int n){
        N = n;
        adj.assign(N, vector<Edge>());
        node.assign(N, Node());
        for(int i=0; i<N; ++i) node[i].id = i;
    }

    void reset(){
        for(int i=0; i<N; ++i){
            node[i].done = false;
            node[i].v = 1e9;
        }
    }
};

int N, LOGN;
Graph G;
VVI tab;
VI depth;
void doubling(){
    for(int i=0; i<N; i++) tab[i][0] = -1;

    for(int i=0; i<N; i++){
        for(Edge e : G.adj[i]) tab[e.to][0] = i;
    }

    for(int k=0; k<LOGN; k++)
        for(int i=0; i<N; i++){
            if(tab[i][k] == -1) tab[i][k+1] = -1;
            else tab[i][k+1] = tab[tab[i][k]][k];
        }
}

int LCA(int u, int v){
    int k = depth[v] - depth[u];
    for(int i=0; i<LOGN; i++){
        if(k & 1<<i) v = tab[v][i];
    }
    if(u == v) return u;
    for(int i=LOGN; i>=0; i--){
        if(tab[u][i] != tab[v][i]){
            u = tab[u][i];
            v = tab[v][i];
        }
    }
    return tab[u][0];
}

void dfs(int n, int d){
    depth[n] = d;
    for(Edge e : G.adj[n]){
        dfs(e.to, d+1);
    }
    return;
}

int main() {
	cin >> N;
    G.assign(N);
    LOGN = floor(log2(N)) + 1;
    tab.assign(N, VI(LOGN+1)); depth.resize(N);
    REP(i,N){
        int k; cin >> k;
        REP(j,k){
            int c; cin >> c;
            G.add_edge(i,c);
        }
    }
    dfs(0,0);
    doubling();
    int q; cin >> q;
    REP(i,q){
        int u,v; cin >> u >> v;
        if(depth[u] > depth[v]) swap(u,v);
        cout << LCA(u,v) << en;
    }
    return 0;
}


Measured memory_bytes: 23228