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

#define int long long
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define pb push_back
#define fi first
#define se second
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}


struct LinkCutTree{;
    struct Node{
        Node *lp,*rp,*pp;
        Node():lp(0),rp(0),pp(0){}
        bool isRoot(){
            return !pp||(pp->lp!=this&&pp->rp!=this);
        }
        void rotR(){
            Node *q=pp,*r=q->pp;
            if((q->lp=rp))rp->pp=q;
            rp=q;q->pp=this;
            if((pp=r)){
                if(r->lp==q)r->lp=this;
                if(r->rp==q)r->rp=this;
            }
        }

        void rotL(){
            Node *q=pp,*r=q->pp;
            if((q->rp=lp))lp->pp=q;
            lp=q;q->pp=this;
            if((pp=r)){
                if(r->lp==q)r->lp=this;
                if(r->rp==q)r->rp=this;
            }
        }

        void splay(){
            while(!isRoot()){
                Node *q=pp;
                if(q->isRoot()){
                    if(q->lp==this)rotR();
                    else rotL();
                }
                else{
                    Node *r=q->pp;
                    if(r->lp==q){
                        if(q->lp==this){q->rotR();rotR();}
                        else {rotL();rotR();}
                    }
                    else{
                        if(q->rp==this){q->rotL();rotL();}
                        else{rotR();rotL();}
                    }
                }
            }
        }

        void expose(){
            Node *rp=NULL;
            for(Node *p=this;p;p=p->pp){
                p->splay();
                p->rp=rp;
                rp=p;
            }
            splay();
        }
    };

    Node *nodes;
    LinkCutTree(int n){nodes=new Node[n];}

    //???(v->v??????)?????????
    void cut(int v){
        nodes[v].expose();
        Node *p=nodes[v].lp;
        nodes[v].lp=NULL;
        p->pp=NULL;
    }

    //???(v->u)?????????
    void link(int v,int u){
        nodes[v].expose();
        nodes[u].expose();
        nodes[v].pp=&nodes[u];
        nodes[u].rp=&nodes[v];
    }

    int lca(int a,int b){
        nodes[a].expose();
        nodes[b].expose();

        bool sameTree=false;
        Node *ret=&nodes[a];
        for(Node *x=&nodes[a];x;x=x->pp){
            if(x->pp&&x->isRoot())ret=x->pp;
            if(x==&nodes[b])sameTree=true;
        }
        return sameTree?(ret-nodes):-1;
    }


    //verify??????
    int findRoot(int v){
        Node *p=&nodes[v];
        p->expose();
        while(p->lp)p=p->lp;
        p->splay();
        return p-nodes;
    }
};


signed main(){
    int N;scanf("%lld",&N);
    LinkCutTree lct(N);
    rep(i,N){
        int k;scanf("%lld",&k);
        while(k--){
            int c;scanf("%lld",&c);
            lct.link(c,i);
        }
    }
    int q;scanf("%lld",&q);
    while(q--){
        int a,b;
        scanf("%lld%lld",&a,&b);
        printf("%lld\n",lct.lca(a,b));
    }
    return 0;
}

Measured memory_bytes: 5524