using System;
using System.Collections.Generic;
using static Assistant.Input;
using static Assistant.Debug;
using System.Linq;
using Assistant;
using System.Text;

namespace AOJ_GRL_5_C
{
    class Program
    {
        static void Main(string[] args)
        {
            var n = ReadInt;
            List<int>[] child = new List<int>[n].Select(x => new List<int>()).ToArray();
            for (int i = 0; i < n; i++)
            {
                int k = ReadInt;
                for (int j = 0; j < k; j++)
                {
                    child[i].Add(ReadInt);
                }
            }

            List<LCA.Edge>[] edges = new List<LCA.Edge>[n];
            for (int i = 0; i < n; i++)
            {
                edges[i] = new List<LCA.Edge>();
                foreach (var c in child[i])
                {
                    edges[i].Add(new LCA.Edge() { to = c });
                }
            }

            LCA lca = new LCA(edges);

            int q = ReadInt;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < q; i++)
            {
                int u = ReadInt;
                int v = ReadInt;

                int ans = lca.Query(u, v);
                sb.AppendLine(ans.ToString());
            }

            Console.Write(sb);
        }

    }

    /* LCA(G, root): 木 G に対する根を root として Lowest Common Ancestor を求めるクラス
    query(u,v): u と v の LCA を求める。計算量 O(logn)
    前処理: O(nlogn)時間, O(nlogn)空間
    */
    class LCA
    {
        public struct Edge
        {
            public int to;
        }

        int[,] parent;  // parent[k][u]:= u の 2^k 先の親
        int[] dist;     // root からの距離

        // 初期化
        public LCA(List<Edge>[] G, int root = 0)
        {
            int V = G.Length;
            int K = 1;
            while ((1 << K) < V) K++;
            parent = new int[K, V];
            for (int i = 0; i < V; i++)
            {
                parent[K - 1, i] = -1;
            }
            dist = new int[V].Select(x => -1).ToArray();
            Dfs(G, root, -1, 0);
            for (int k = 0; k + 1 < K; k++)
            {
                for (int v = 0; v < V; v++)
                {
                    if (parent[k, v] < 0)
                    {
                        parent[k + 1, v] = -1;
                    }
                    else
                    {
                        parent[k + 1, v] = parent[k, parent[k, v]];
                    }
                }
            }
            //DrawMapforDebug(parent);
        }

        // 根からの距離と1つ先の頂点を求める
        void Dfs(List<Edge>[] G, int v, int p, int d)
        {
            parent[0, v] = p;
            dist[v] = d;
            foreach (var e in G[v])
            {
                if (e.to != p) Dfs(G, e.to, v, d + 1);
            }
        }

        public int Query(int u, int v)
        {
            if (dist[u] < dist[v])
            {
                int temp = v;
                v = u; u = temp;// u の方が深いとする
            }
            int K = parent.GetLength(0);

            // LCA までの距離を同じにする
            for (int k = 0; k < K; k++)
            {
                if ((((dist[u] - dist[v]) >> k) & 1) > 0)
                {
                    u = parent[k, u];
                }
            }

            // 二分探索で LCA を求める⇒重要
            if (u == v) return u;
            for (int k = K - 1; k >= 0; k--)
            {
                if (parent[k, u] != parent[k, v])
                {
                    u = parent[k, u];
                    v = parent[k, v];
                }
            }
            return parent[0, u];
        }

        public int Distance(int u, int v)
        {
            return dist[u] + dist[v] - 2 * dist[Query(u, v)];
        }

        public bool IsOnPath(int u, int v, int a)
        {
            return Distance(u, a) + Distance(a, v) == Distance(u, v);
        }

    }
}

namespace Assistant
{
    static class Input
    {
        static List<string> line = new List<string>();
        static int index = 0;
        static String ReadNext()
        {
            if (line.Count <= index) line.AddRange(Console.ReadLine().Split());
            return line[index++];
        }
        public static int ReadInt => int.Parse(ReadNext());
        public static long ReadLong => long.Parse(ReadNext());
        public static int[] ReadInts => Console.ReadLine().Split().Select(int.Parse).ToArray();
        public static long[] NextLongs => Console.ReadLine().Split().Select(long.Parse).ToArray();
    }

    static class Debug
    {
        //drawmap
        //drawarray
        /// <summary>
        /// 二次元配列の可視化メソッド
        /// </summary>
        /// <param name="map"></param>
        static public void DrawMapforDebug(int[,] map, int mode = 1)
        {
            int W = map.GetLength(0);
            int H = map.GetLength(1);

            int[,] map2 = new int[W + 1, H + 1];
            for (int i = 0; i < W + 1; i++)
            {
                for (int j = 0; j < H + 1; j++)
                {
                    if (i == 0 && j == 0) map2[i, j] = 0;
                    else if (i == 0) map2[i, j] = j - 1;
                    else if (j == 0) map2[i, j] = i - 1;
                    else map2[i, j] = map[i - 1, j - 1];
                }
            }

            for (int i = 0; i < W + 1; i++)
            {
                for (int j = 0; j < H + 1; j++)
                {
                    if (mode == 0) Console.Write(map2[i, j] % 10);
                    if (mode == 1) Console.Write(map2[i, j] + " ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
}



Measured memory_bytes: 80908