using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LowestCommonAncestor
{
    class LowestCommonAncestor
    {
        Dictionary<int, List<int>> G;
        int N, logN;
        int[,] parent;
        int[] depth;

        public LowestCommonAncestor(Dictionary<int, List<int>> G, int V)
        {
            this.G = G;
            N = V;
            logN = (int)Math.Log(N, 2) + 1;
            parent = new int[N, logN];
            depth = new int[N];

            for (int i = 0; i < N; i++)
                for (int j = 0; j < logN; j++)
                    parent[i, j] = -1;
        }

        public void Init(int v, int p, int d)
        {
            parent[v, 0] = p;
            depth[v] = d;

            foreach (var u in G[v])
            {
                if (u != p) Init(u, v, d + 1);
            }
        }

        public void Build()
        {
            for (int d = 1; (1 << d) < N; d++)
            {
                for (int i = 0; i < N; i++)
                {
                    if (parent[i, d - 1] != -1)
                    {
                        parent[i, d] = parent[parent[i, d - 1], d - 1];
                    }
                }
            }
        }

        public int Query(int u, int v)
        {
            if (depth[u] < depth[v])
            {
                int temp = u;
                u = v;
                v = temp;
            }

            for (int i = logN - 1; i >= 0; i--)
            {
                if (depth[u] - (1 << i) >= depth[v])
                {
                    u = parent[u, i];
                }
            }

            if (u == v) return u;

            for (int i = logN - 1; i >= 0; i--)
            {
                if (parent[u, i] != -1 && parent[u, i] != parent[v, i])
                {
                    u = parent[u, i];
                    v = parent[v, i];
                }
            }
            return parent[u, 0];
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            int V = int.Parse(Console.ReadLine());

            Dictionary<int, List<int>> G = new Dictionary<int, List<int>>();
            for (int i = 0; i < V; i++) G.Add(i, new List<int>());

            for (int i = 0; i < V; i++)
            {
                string[] input = Console.ReadLine().Split(' ');
                int k = int.Parse(input[0]);

                for (int j = 1; j <= k; j++)
                {
                    int c = int.Parse(input[j]);
                    G[i].Add(c);
                    G[c].Add(i);
                }
            }

            LowestCommonAncestor lca = new LowestCommonAncestor(G, V);
            lca.Init(0, -1, 0);
            lca.Build();

            StringBuilder sb = new StringBuilder();
            int q = int.Parse(Console.ReadLine());

            for (int i = 0; i < q; i++)
            {
                string[] input = Console.ReadLine().Split(' ');
                int u = int.Parse(input[0]);
                int v = int.Parse(input[1]);

                int p = lca.Query(u, v);
                sb.AppendLine(p.ToString());
            }
            Console.Write(sb);
        }
    }
}

Measured memory_bytes: 54032