using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
static class Program{
	const int mod=(int)1e9+7;
	static List<int>[] li;
	static bool[] b;
	static int[] h;
	static void Main(){
		Sc sc=new Sc();
		int n=sc.I;
		li=new List<int>[n+1];
		b=new bool[n+1];
		h=new int[n+1];
		for(int i=1;i<=n;i++){li[i]=new List<int>();}
		for(int i=1;i<=n;i++){
			int[] e=sc.Ia;
			for(int j = 1;j<=e[0];j++) {
				li[i].Add(e[j]+1);
				li[e[j]+1].Add(i);
			}
		}
		var da=new Alca(li,true);
		int q=sc.I;
		StringBuilder sb=new StringBuilder();
		for(int i = 0;i<q;i++) {
			int[] e=sc.Ia;
			sb.Append((da.Lca(e[0]+1,e[1]+1)-1)+"\n");
		}
		Console.Write(sb);
	}
}
public class Alca{
	private List<int>[] li;
	private readonly int[] at,ix,de,l;
	private readonly int[][] g;
	private int f=0;
	public Alca(List<int>[] li,bool bo){
		int n=li.Length,z=(n<<1)-1,m=z;
		this.li=li;
		at=new int[z];
		ix=new int[n];
		de=new int[n];
		if(bo){Fu1(1,1);}
		else{
			for(int i = 1;i<n;i++) {
				if(de[i]==0){Fu1(i,1);f++;}
			}
		}
		m=(int)Log(m,2)+1;
		g=new int[m][];
		g[0]=new int[z];
		l=new int[z+1];
		for(int i=0;i<z;i++){
			g[0][i]=i;
			l[i+1]=l[i>>1]+1;
		}
		for(int i=1,k=1;i<m;i++,k<<=1){
			int p=z+1-(k<<1);
			g[i]=new int[p];
			for(int j=0;j<p;j++){g[i][j]=de[at[g[i-1][j]]]<de[at[g[i-1][j+k]]]?g[i-1][j]:g[i-1][j+k];}
		}
	}
	public int Lca(int a,int b){return Get(ix[a],ix[b]);}
	public int Di(int a,int b){
		int p=Get(ix[a],ix[b]);
		return p!=0?de[a]+de[b]-(de[p]<<1):-1;
	}
	private int Get(int a,int b){
		if(a>b){a^=b;b^=a;a^=b;}
		int p=l[b-a];
		return at[de[at[g[p][a]]]<de[at[g[p][b+1-(1<<p)]]]?g[p][a]:g[p][b+1-(1<<p)]];
	}
	private void Fu1(int a,int g){
		at[f]=a;
		de[a]=g;
		ix[a]=f;
		f++;
		for(int i=0;i<li[a].Count;i++){
			if(de[li[a][i]]==0){
				Fu1(li[a][i],g+1);
				at[f]=a;
				f++;
			}
		}
	}
}

public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),int.Parse);}
	public int[] Ia3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),long.Parse);}
	public long[] La3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),long.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}


Measured memory_bytes: 56144