using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using static System.Console;
using static System.Math;

class Program
{
    static void Main()
    {
        SetOut(new System.IO.StreamWriter(OpenStandardOutput()) { AutoFlush = false });
        var n = FastIO.Int();
        var g = new List<int>[n];
        for (int i = 0; i < n; i++) g[i] = new List<int>();
        for (int i = 0; i < n; i++)
        {
            var k = FastIO.Int();
            for (int j = 0; j < k; j++)
            {
                var c = FastIO.Int();
                g[i].Add(c);
                g[c].Add(i);
            }
        }
        var hld = new HLD(g);
        hld.Build(0);
        var q = FastIO.Int();
        for (int i = 0; i < q; i++)
        {
            var u = FastIO.Int();
            var v = FastIO.Int();
            WriteLine(hld.LCA(u, v));
        }
        Out.Flush();
    }
}

public class HLD
{
    List<int>[] tree;
    int[] size, l, par, head;

    public HLD(List<int>[] g)
    {
        var len = g.Length;
        size = new int[len];
        l = new int[len];
        par = new int[len];
        head = new int[len];
        tree = g;
    }

    public void Build(int root)
    {
        DFSS(root, -1);
        var k = 0;
        DFSD(root, -1, ref k);
    }

    void DFSS(int v, int p)
    {
        par[v] = p;
        for (int i = 0; i < tree[v].Count; i++)
        {
            var u = tree[v][i];
            if (u == p) continue;
            DFSS(u, v);
            size[v] += size[u];
            var h = tree[v][0];
            if (size[h] < size[u])
            {
                tree[v][0] = u;
                tree[v][i] = h;
            }
        }
        size[v]++;
    }

    void DFSD(int v, int p, ref int k)
    {
        l[v] = k++;
        foreach (var u in tree[v])
        {
            if (u == p) continue;
            head[u] = u == tree[v][0] ? head[v] : u;
            DFSD(u, v, ref k);
        }
    }

    void Swap<T>(ref T l, ref T r)
    {
        var t = l;
        l = r;
        r = t;
    }

    public int LCA(int u, int v)
    {
        while (true)
        {
            if (l[u] > l[v]) Swap(ref u, ref v);
            if (head[u] == head[v]) return u;
            v = par[head[v]];
        }
    }
}

public static class FastIO
{
    static System.IO.Stream str = System.Console.OpenStandardInput();
    const int size = 1024;
    static byte[] buffer = new byte[size];
    static int ptr;
    static int len;

    static byte Read()
    {
        if (ptr == len)
        {
            len = str.Read(buffer, 0, size);
            if (len == 0) return 0;
            ptr = 0;
        }
        return buffer[ptr++];
    }

    public static int Int()
    {
        var c = Read();
        while (c < 0x21)
        {
            c = Read();
        }
        var n = false;
        if (c == '-')
        {
            n = true;
            c = Read();
        }
        var ret = 0;
        while (c > 0x20)
        {
            ret = ret * 10 + c - '0';
            c = Read();
        }
        return n ? -ret : ret;
    }

    public static long Long()
    {
        var c = Read();
        while (c < 0x21)
        {
            c = Read();
        }
        var n = false;
        if (c == '-')
        {
            n = true;
            c = Read();
        }
        var ret = 0L;
        while (c > 0x20)
        {
            ret = ret * 10 + c - '0';
            c = Read();
        }
        return n ? -ret : ret;
    }
}


Measured memory_bytes: 41976