Problem:
Score : 600 points

Problem Statement

We have a tree with N vertices numbered 1 to N.
The i-th edge in this tree connects Vertex a_i and Vertex b_i.

Consider painting each of these edges white or black. There are 2^{N-1} such ways to paint the edges. Among them, how many satisfy all of the following M restrictions?

The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black.

Constraints

2 \leq N \leq 50

1 \leq a_i,b_i \leq N

The graph given in input is a tree.

1 \leq M \leq \min(20,\frac{N(N-1)}{2})

1 \leq u_i < v_i \leq N

If i \not= j, either u_i \not=u_j or v_i\not=v_j

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M

Output

Print the number of ways to paint the edges that satisfy all of the M conditions.

Sample Input 1

3
1 2
2 3
1
1 3

Sample Output 1

3

The tree in this input is shown below:

All of the M restrictions will be satisfied if Edge 1 and 2 are respectively painted (white, black), (black, white), or (black, black), so the answer is 3.

Sample Input 2

2
1 2
1
1 2

Sample Output 2

1

The tree in this input is shown below:

All of the M restrictions will be satisfied only if Edge 1 is painted black, so the answer is 1.

Sample Input 3

5
1 2
3 2
3 4
5 3
3
1 3
2 4
2 5

Sample Output 3

9

The tree in this input is shown below:

Sample Input 4

8
1 2
2 3
4 3
2 5
6 3
6 7
8 6
5
2 7
3 5
1 6
2 8
7 8

Sample Output 4

62

The tree in this input is shown below:

Solution in PyPy3 (2.4.0) (judge verdict: Runtime Error)

# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import math
from functools import lru_cache

#############
# Constants #
#############

MOD = 10**9 +7
INF = float('inf')

#############
# Functions #
#############

def swap(x,y):
  return y,x

def count_ones_by_bin(n):
  bin_n = bin(n)[2:]
  count = 0
  for i in bin_n:
    count += int(i)
  return count

kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#############
# Main Code #
#############

N = int(input())
LOG_N = math.floor(math.log(N)/math.log(2))

#{node:[depth,ancestor]}
dic = {}
for i in range(N-1):
  a,b = list(map(int,input().split()))
  if i == 0:
    dic[a] = [0,-1]
  if a in dic:
    dic[b] = [dic[a][0]+1,a]
  else:
    dic[a] = [dic[b][0]+1,b]

#table[k][i]: i's 2^k-th ancestor
table = [[0 for i in range(N+1)] for j in range(LOG_N+1)] 

for i in range(1,N+1):
  table[0][i] = dic[i][1]

for k in range(LOG_N):
  for i in range(1,N+1):
    if (table[k][i] == -1):
      table[k+1][i] = -1
    else:
      table[k+1][i] = table[k][table[k][i]]

def lca(u,v):
  if dic[u][0] > dic[v][0]:
    u,v = swap(u,v)
  for i in range(LOG_N+1)[::-1]:
    if ((dic[v][0] - dic[u][0]) >> i) & 1:
      v = table[i][v]
  if u == v:
    return u
  for i in range(LOG_N+1)[::-1]:
    if table[i][u] != table[i][v]:
      u = table[i][u]
      v = table[i][v]
  return table[0][u]

#path to root
@lru_cache(maxsize=None)
def path_to_root(i):
  if i == 1:
    return 0
  return path_to_root(table[0][i])+2**i

M = int(input())
l = []
for i in range(M):
  l.append(tuple(map(int,input().split())))


accum = 2**(N-1)
for i in range(1,2**M):
  choice = []
  for j in range(M):
    if (i >> j) & 1:
      choice.append(l[j])
  cover = 0
  for item in choice:
    u = item[0]
    v = item[1]
    u_ = path_to_root(u) - path_to_root(lca(u,v))
    v_ = path_to_root(v) - path_to_root(lca(u,v))
    cover = cover | u_ | v_
  
  conditions = len(choice)
  paths = count_ones_by_bin(cover)
  
  accum += 2 ** (N-1-paths) * (((M-conditions)%2)*2-1)


    
print(accum)