from collections import deque

n = eval(input())



C = [None]*n

P = [None]*n

for i in range(n):

    ipt = list(map(int, input().split()))

    C[i] = ipt[1:]

    for c in C[i]:

        P[c] = i

LEV = (n+1).bit_length()

parent = [[None]*(LEV + 1) for i in range(n)]

for i in range(n):

    parent[i][0] = P[i]

for k in range(LEV):

    for i in range(n):

        if parent[i][k] is None:

            parent[i][k+1] = None

        else:

            parent[i][k+1] = parent[parent[i][k]][k]

depth = [None]*n

deq = deque()

deq.append(0)

depth[0] = 0

while deq:

    v = deq.popleft()

    for c in C[v]:

        deq.append(c)

        depth[c] = depth[v] + 1



q = eval(input())

for t in range(q):

    u, v = list(map(int, input().split()))

    if not depth[u] < depth[v]:

        u, v = v, u

    for k in range(LEV+1):

        if ((depth[v] - depth[u]) >> k) & 1:

            v = parent[v][k]

    if u == v:

        print(u)

        continue



    for k in range(LEV, -1, -1):

        if parent[u][k] != parent[v][k]:

            u = parent[u][k]

            v = parent[v][k]

    print(parent[u][0])
import sys

sys.setrecursionlimit(10**6)



readline = sys.stdin.readline

write = sys.stdout.write



N = int(readline())

G = [None]*N

for v in range(N):

    k, *G[v] = list(map(int, readline().split()))



S = []



FS = [0]*N

depth = [0]*N



*it, = list(map(iter, G))

P = [-1]*N



stk = [0]

depth[0] = 0

FS[0] = 0

while stk:

    v = stk[-1]

    p = P[v]

    S.append(v)



    w = next(it[v], -1)

    while w != -1:

        if w != p:

            P[w] = v

            depth[w] = len(stk)

            FS[w] = len(S)

            stk.append(w)

            break

        w = next(it[v], -1)

    else:

        stk.pop()



L = len(S)

lg = [0]*(L + 1)

for i in range(2, L+1):

    lg[i] = lg[i >> 1] + 1

st = [None]*(lg[L] + 1)

st0 = st[0] = S

b = 1

for i in range(lg[L]):

    st0 = st[i+1] = [p if depth[p] <= depth[q] else q for p, q in zip(st0, st0[b:])]

    b <<= 1



def query(u, v):

    x = FS[u]; y = FS[v]

    if x > y:

        x, y = y, x

    l = lg[y - x + 1]

    px = st[l][x]; py = st[l][y - (1 << l) + 1]

    return px if depth[px] <= depth[py] else py



Q = int(readline())

ans = []

for q in range(Q):

    u, v = list(map(int, readline().split()))

    ans.append(query(u, v))

write("\n".join(map(str, ans)))

write("\n")

from collections import deque

n = eval(input())
C = [None] * n
P = [None] * n
for i in range(n):
    ipt = list(map(int, input().split()))
    C[i] = ipt[1:]
    for c in C[i]:
        P[c] = i
LEV = (n + 1).bit_length()
parent = [[None] * (LEV + 1) for i in range(n)]
for i in range(n):
    parent[i][0] = P[i]
for k in range(LEV):
    for i in range(n):
        if parent[i][k] is None:
            parent[i][k + 1] = None
        else:
            parent[i][k + 1] = parent[parent[i][k]][k]
depth = [None] * n
deq = deque()
deq.append(0)
depth[0] = 0
while deq:
    v = deq.popleft()
    for c in C[v]:
        deq.append(c)
        depth[c] = depth[v] + 1
q = eval(input())
for t in range(q):
    u, v = list(map(int, input().split()))
    if not depth[u] < depth[v]:
        u, v = v, u
    for k in range(LEV + 1):
        if ((depth[v] - depth[u]) >> k) & 1:
            v = parent[v][k]
    if u == v:
        print(u)
        continue
    for k in range(LEV, -1, -1):
        if parent[u][k] != parent[v][k]:
            u = parent[u][k]
            v = parent[v][k]
    print(parent[u][0])

import sys

sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
write = sys.stdout.write
N = int(readline())
G = [None] * N
for v in range(N):
    k, *G[v] = list(map(int, readline().split()))
S = []
FS = [0] * N
depth = [0] * N
(*it,) = list(map(iter, G))
P = [-1] * N
stk = [0]
depth[0] = 0
FS[0] = 0
while stk:
    v = stk[-1]
    p = P[v]
    S.append(v)
    w = next(it[v], -1)
    while w != -1:
        if w != p:
            P[w] = v
            depth[w] = len(stk)
            FS[w] = len(S)
            stk.append(w)
            break
        w = next(it[v], -1)
    else:
        stk.pop()
L = len(S)
lg = [0] * (L + 1)
for i in range(2, L + 1):
    lg[i] = lg[i >> 1] + 1
st = [None] * (lg[L] + 1)
st0 = st[0] = S
b = 1
for i in range(lg[L]):
    st0 = st[i + 1] = [p if depth[p] <= depth[q] else q for p, q in zip(st0, st0[b:])]
    b <<= 1


def query(u, v):
    x = FS[u]
    y = FS[v]
    if x > y:
        x, y = y, x
    l = lg[y - x + 1]
    px = st[l][x]
    py = st[l][y - (1 << l) + 1]
    return px if depth[px] <= depth[py] else py


Q = int(readline())
ans = []
for q in range(Q):
    u, v = list(map(int, readline().split()))
    ans.append(query(u, v))
write("\n".join(map(str, ans)))
write("\n")

-from collections import deque +import sys -n = eval(input()) -C = [None] * n -P = [None] * n -for i in range(n): -    ipt = list(map(int, input().split())) -    C[i] = ipt[1:] -    for c in C[i]: -        P[c] = i -LEV = (n + 1).bit_length() -parent = [[None] * (LEV + 1) for i in range(n)] -for i in range(n): -    parent[i][0] = P[i] -for k in range(LEV): -    for i in range(n): -        if parent[i][k] is None: -            parent[i][k + 1] = None -        else: -            parent[i][k + 1] = parent[parent[i][k]][k] -depth = [None] * n -deq = deque() -deq.append(0) +sys.setrecursionlimit(10**6) +readline = sys.stdin.readline +write = sys.stdout.write +N = int(readline()) +G = [None] * N +for v in range(N): +    k, *G[v] = list(map(int, readline().split())) +S = [] +FS = [0] * N +depth = [0] * N +(*it,) = list(map(iter, G)) +P = [-1] * N +stk = [0] -while deq: -    v = deq.popleft() -    for c in C[v]: -        deq.append(c) -        depth[c] = depth[v] + 1 -q = eval(input()) -for t in range(q): -    u, v = list(map(int, input().split())) -    if not depth[u] < depth[v]: -        u, v = v, u -    for k in range(LEV + 1): -        if ((depth[v] - depth[u]) >> k) & 1: -            v = parent[v][k] -    if u == v: -        print(u) -        continue -    for k in range(LEV, -1, -1): -        if parent[u][k] != parent[v][k]: -            u = parent[u][k] -            v = parent[v][k] -    print(parent[u][0]) +FS[0] = 0 +while stk: +    v = stk[-1] +    p = P[v] +    S.append(v) +    w = next(it[v], -1) +    while w != -1: +        if w != p: +            P[w] = v +            depth[w] = len(stk) +            FS[w] = len(S) +            stk.append(w) +            break +        w = next(it[v], -1) +    else: +        stk.pop() +L = len(S) +lg = [0] * (L + 1) +for i in range(2, L + 1): +    lg[i] = lg[i >> 1] + 1 +st = [None] * (lg[L] + 1) +st0 = st[0] = S +b = 1 +for i in range(lg[L]): +    st0 = st[i + 1] = [p if depth[p] <= depth[q] else q for p, q in zip(st0, st0[b:])] +    b <<= 1 + + +def query(u, v): +    x = FS[u] +    y = FS[v] +    if x > y: +        x, y = y, x +    l = lg[y - x + 1] +    px = st[l][x] +    py = st[l][y - (1 << l) + 1] +    return px if depth[px] <= depth[py] else py + + +Q = int(readline()) +ans = [] +for q in range(Q): +    u, v = list(map(int, readline().split())) +    ans.append(query(u, v)) +write("\n".join(map(str, ans))) +write("\n")