from collections import defaultdict, Counter



N = int(eval(input()))

ab = [[] for _ in range(N)]

for i in range(N - 1) :

  a, b = list(map(int, input().split()))

  ab[a-1].append((b-1, i))

  ab[b-1].append((a-1, i))



M = int(eval(input()))

uv = [[int(i) for i in input().split()] for _ in range(M)]



cnt = [set() for _ in range(M)]

def dfs(cur, pre, dest, i) :

  if cur == dest :

    return True

  

  for nex, j in ab[cur] :

    if nex == pre :

      continue

    if dfs(nex, cur, dest, i) :

      cnt[i].add(j)

      return True



  return False



for i, (u, v) in enumerate(uv) :

  dfs(u - 1, -1, v - 1, i)



ret = 1 << (N - 1)

for i in range(1, 1 << M) :

  sgn = 1

  s = set([])

  for j in range(M) :

    if i & (1 << j) :

      s |= cnt[j]

      sgn *= -1

  ret += sgn * (1 << (N - 1 - len(s)))



print(ret)
N = int(eval(input()))

ab = [[] for _ in range(N)]

for i in range(N-1) :

  a, b = list(map(int, input().split()))

  ab[a-1].append((b-1, i))

  ab[b-1].append((a-1, i))

M = int(eval(input()))

uv = [[int(i) for i in input().split()] for _ in range(M)]



dp = [0] * M

def dfs(cur, pre, t, i) :

  if cur == t : return True

  for nex, j in ab[cur] :

    if nex == pre : continue

    if dfs(nex, cur, t, i) :

      dp[i] |= 1<<j

      return True

  return False



for i, (u, v) in enumerate(uv) :

  dfs(u-1, -1, v-1, i)



ret = 1<<(N-1)

for i in range(1, 1<<M) :

  sgn = 1

  s = 0

  for j in range(M) :

    if i & (1<<j) :

      s |= dp[j]

      sgn *= -1

  ret += sgn * (1<<(N-1-bin(s).count('1')))



print(ret)
from collections import defaultdict, Counter

N = int(eval(input()))
ab = [[] for _ in range(N)]
for i in range(N - 1):
    a, b = list(map(int, input().split()))
    ab[a - 1].append((b - 1, i))
    ab[b - 1].append((a - 1, i))
M = int(eval(input()))
uv = [[int(i) for i in input().split()] for _ in range(M)]
cnt = [set() for _ in range(M)]


def dfs(cur, pre, dest, i):
    if cur == dest:
        return True
    for nex, j in ab[cur]:
        if nex == pre:
            continue
        if dfs(nex, cur, dest, i):
            cnt[i].add(j)
            return True
    return False


for i, (u, v) in enumerate(uv):
    dfs(u - 1, -1, v - 1, i)
ret = 1 << (N - 1)
for i in range(1, 1 << M):
    sgn = 1
    s = set([])
    for j in range(M):
        if i & (1 << j):
            s |= cnt[j]
            sgn *= -1
    ret += sgn * (1 << (N - 1 - len(s)))
print(ret)

N = int(eval(input()))
ab = [[] for _ in range(N)]
for i in range(N - 1):
    a, b = list(map(int, input().split()))
    ab[a - 1].append((b - 1, i))
    ab[b - 1].append((a - 1, i))
M = int(eval(input()))
uv = [[int(i) for i in input().split()] for _ in range(M)]
dp = [0] * M


def dfs(cur, pre, t, i):
    if cur == t:
        return True
    for nex, j in ab[cur]:
        if nex == pre:
            continue
        if dfs(nex, cur, t, i):
            dp[i] |= 1 << j
            return True
    return False


for i, (u, v) in enumerate(uv):
    dfs(u - 1, -1, v - 1, i)
ret = 1 << (N - 1)
for i in range(1, 1 << M):
    sgn = 1
    s = 0
    for j in range(M):
        if i & (1 << j):
            s |= dp[j]
            sgn *= -1
    ret += sgn * (1 << (N - 1 - bin(s).count("1")))
print(ret)

-from collections import defaultdict, Counter - -cnt = [set() for _ in range(M)] +dp = [0] * M -def dfs(cur, pre, dest, i): -    if cur == dest: +def dfs(cur, pre, t, i): +    if cur == t: -        if dfs(nex, cur, dest, i): -            cnt[i].add(j) +        if dfs(nex, cur, t, i): +            dp[i] |= 1 << j -    s = set([]) +    s = 0 -            s |= cnt[j] +            s |= dp[j] -    ret += sgn * (1 << (N - 1 - len(s))) +    ret += sgn * (1 << (N - 1 - bin(s).count("1")))