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: Time Limit Exceeded)

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

local function getor(x, y)
  if x == 0 then return y end
  if y == 0 then return x end
  local ret = 0
  local mul = 1
  while 0 < x or 0 < y do
    ret = ret + mul * (1 - (1 - x % 2) * (1 - y % 2))
    -- if 1 <= (x % 2) + (y % 2) then
    --   ret = ret + mul
    -- end
    x, y, mul = mfl(x / 2), mfl(y / 2), mul * 2
  end
  return ret
end

local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  for i = 1, stagenum do self.size[i] = self.cnt[stagenum + 1 - i] end
  self.stagenum = stagenum
  for i = 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local start_stage = 1
  while right - left + 1 < self.size[start_stage] do
    start_stage = start_stage + 1
  end
  local ret = self.emptyvalue
  local t1, t2, t3 = {start_stage}, {left}, {right}
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = self.size[stage]
    if (l - 1) % sz ~= 0 then
      local newr = mmi(r, mce((l - 1) / sz) * sz)
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, newr)
      l = newr + 1
    end
    if sz <= r + 1 - l then
      ret = self.func(ret, self.stage[stage][mce(l / sz)])
      l = l + sz
    end
    if l <= r then
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
  return ret
end
SegTree.setValue = function(self, idx, value, silent)
  self.stage[self.stagenum][idx] = value
  if not silent then
    for i = self.stagenum - 1, 1, -1 do
      local dst = mce(idx / 2)
      local rem = dst * 4 - 1 - idx
      self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
      idx = dst
    end
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

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 st = SegTree.new(2 * n - 1, function(a, b)
  return len[a] < len[b] and a or b end,
  n + 1
)

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)
  st:setValue(#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 stLine = SegTree.new(2 * n - 2, function(a, b) return a + b end, 0)

local m = io.read("*n")
local mtot = 2^m
local mbox = {}
for i = 1, mtot do
  mbox[i] = 0LL
end
mbox[1] = 1LL
local mu, mv = {}, {}
for i = 1, m do
  mu[i], mv[i] = io.read("*n", "*n")
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

for il = 1, n - 1 do
  local lx, ly = line[il][1], line[il][2]
  local p1, p2 = linepos[il][1], linepos[il][2]
  stLine:setValue(p1, 1)
  stLine:setValue(p2, -1)
  local mul = 1
  local actsum = 0
  for im = 1, m do
    local u, v = mu[im], mv[im]
    local pu, pv = posinv[u], posinv[v]
    if pv < pu then pu, pv = pv, pu end
    local parent = st:getRange(pu, pv)
    local activate = false
    local pp = posinv[parent]
    if pv < pp then
      activate = 0 ~= stLine:getRange(pv, pp - 1)
    elseif pp < pv then
      activate = 0 ~= stLine:getRange(pp, pv - 1)
    end
    if not activate then
      if pu < pp then
        activate = 0 ~= stLine:getRange(pu, pp - 1)
      elseif pp < pu then
        activate = 0 ~= stLine:getRange(pp, pu - 1)
      end
    end
    if activate then
      actsum = actsum + mul
    end
    mul = mul * 2
  end
  if 0 < actsum then
  end
  -- print("IL", il, actsum)
  for im = mtot, 1, -1 do
    local src = im - 1
    local dst = getor(src, actsum)
    -- print("a " .. src .. " " .. dst)
    mbox[dst + 1] = mbox[dst + 1] + mbox[src + 1]
  end
  stLine:setValue(p1, 0)
  stLine:setValue(p2, 0)
  -- print(table.concat(mbox, " "))
end
-- print(table.concat(mbox, " "))
local str = tostring(mbox[mtot]):gsub("LL", "")
print(str)