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 LuaJIT (2.0.4) (judge verdict: Accepted)

local mfl, mce, mmi = math.floor, math.ceil, math.min
local bor = bit.bor

local n = io.read("*n")
local edge = {}
local asked = {}
local cpos = {}
local len = {}
for i = 1, n do
  edge[i] = {}
  asked[i] = false
  cpos[i] = 0
  len[i] = 0
end
len[n + 1] = 100
local line = {}
for i = 1, n - 1 do
  local x, y = io.read("*n", "*n")
  table.insert(edge[x], y)
  table.insert(edge[y], x)
  line[i] = {x, y}
end

local tasks = {1}
local posinv = {}
for i = 1, n do
  posinv[i] = 0
end
local eulers = {}
while 0 < #tasks do
  local src = tasks[#tasks]
  table.remove(tasks)
  asked[src] = true
  table.insert(eulers, src)
  if posinv[src] == 0 then
    posinv[src] = #eulers
  end
  while cpos[src] < #edge[src] do
    cpos[src] = cpos[src] + 1
    local dst = edge[src][cpos[src]]
    if not asked[dst] then
      len[dst] = len[src] + 1
      table.insert(tasks, src)
      table.insert(tasks, dst)
      break
    end
  end
end
local lca = {}
for i = 1, #eulers do
  lca[i] = {}
  local prv = eulers[i]
  lca[i][1] = prv
  for j = i + 1, #eulers do
    lca[i][j - i + 1] = len[prv] < len[eulers[j]] and prv or eulers[j]
    prv = lca[i][j - i + 1]
  end
end

local m = io.read("*n")
local mtot = 2^m

local posu, posv, posp = {}, {}, {}
for i = 1, m do
  local u, v = io.read("*n", "*n")
  local pu, pv = posinv[u], posinv[v]
  if pv < pu then pu, pv = pv, pu end
  local p = lca[pu][pv - pu + 1]
  posu[i], posv[i], posp[i] = pu, pv, posinv[p]
end
local linepos = {}
for il = 1, n - 1 do
  linepos[il] = {}
  local lx, ly = line[il][1], line[il][2]
  for j = 1, #eulers - 1 do
    if lx == eulers[j] and ly == eulers[j + 1] then
      table.insert(linepos[il], j)
    elseif ly == eulers[j] and lx == eulers[j + 1] then
      table.insert(linepos[il], j)
    end
  end
end
local lineval = {}
for i = 1, 2 * n - 2 do
  lineval[i] = 0
end
local linesum = {}
for i = 1, 2 * n - 2 do
  linesum[i] = {}
end
local function recalc_linesum()
  for i = 1, 2 * n - 2 do
    linesum[i][1] = lineval[i]
    for j = i + 1, 2 * n - 2 do
      linesum[i][j - i + 1] = linesum[i][j - i] + lineval[j]
    end
  end
end

local mbox1, mbox2 = {}, {}
for i = 1, mtot do
  mbox1[i], mbox2[i] = 0, 0
end
mbox1[1] = 1
local function addmbox(src, dst)
  mbox2[dst] = mbox2[dst] + mbox2[src]
  mbox1[dst] = mbox1[dst] + mbox1[src]
  if 1000000000 <= mbox1[dst] then
    mbox1[dst] = mbox1[dst] - 1000000000
    mbox2[dst] = mbox2[dst] + 1
  end
end

for il = 1, n - 1 do
  local lx, ly = line[il][1], line[il][2]
  local p1, p2 = linepos[il][1], linepos[il][2]
  lineval[p1] = 1
  lineval[p2] = -1
  recalc_linesum()
  local mul = 1
  local actsum = 0
  for im = 1, m do
    local pu, pv, pp = posu[im], posv[im], posp[im]
    if pv < pp then
      activate = 0 ~= linesum[pv][pp - pv]
    elseif pp < pv then
      activate = 0 ~= linesum[pp][pv - pp]
    end
    if not activate then
      if pu < pp then
        activate = 0 ~= linesum[pu][pp - pu]
      elseif pp < pu then
        activate = 0 ~= linesum[pp][pu - pp]
      end
    end
    if activate then
      actsum = actsum + mul
    end
    mul = mul * 2
  end
  for im = mtot, 1, -1 do
    local dst = bor(im - 1, actsum)
    addmbox(im, dst + 1)
  end
  lineval[p1] = 0
  lineval[p2] = 0
end
if mbox2[mtot] == 0 then
  print(mbox1[mtot])
else
  local z = 1000000000 + mbox1[mtot]
  local zstr = tostring(z)
  zstr = zstr:sub(2, #zstr)
  print(mbox2[mtot] .. zstr)
end