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 Rust (1.15.1) (judge verdict: Wrong Answer)

use std::io::{self, BufRead};
use std::collections::{HashMap, HashSet};
use std::hash::Hash;
use std::mem::swap;

fn lca(parents: &Vec<(usize, usize)>, x: usize, y: usize) -> usize {
  let mut u = x;
  let mut v = y;
  let depth_x = parents[x].1;
  let depth_y = parents[y].1;
  if depth_x < depth_y {
    for _ in 0..(depth_y - depth_x) {
      v = parents[v].0;
    }
  } else {
    for _ in 0..(depth_x - depth_y) {
      u = parents[u].0;
    }
  }
  while u != v {
    u = parents[u].0;
    v = parents[v].0;
  }
  u
}

fn insert_or_update<K, V>(tree: &mut HashMap<K, HashSet<V>>, k: &K, v: &V) where K: Eq + Hash + Copy, V: Eq + Hash + Copy {
  let contains = tree.contains_key(k);
  if contains {
    tree.get_mut(k).unwrap().insert(*v);
  } else {
    let mut ws = HashSet::new();
    ws.insert(*v);
    tree.insert(*k, ws);
  }
}

fn create_parents(tree: &HashMap<usize, HashSet<usize>>, n: usize) -> Vec<(usize, usize)> {
  let mut parents: Vec<(usize, usize)> = vec![(0, usize::max_value()); n];
  let mut buf = Vec::new();
  buf.push(0);
  parents[0].1 = 0;
  let mut depth = 1;
  while !buf.is_empty() {
    let mut next = Vec::new();
    for i in &buf {
      if let Some(js) = tree.get(&i) {
        for j in js {
          if parents[*j].1 == usize::max_value() {
            parents[*j] = (*i, depth);
            next.push(*j);
          }
        }
      }
    }
    swap(&mut buf, &mut next);
    depth += 1;
  }
  parents
}

fn main() {
  let stdin = io::stdin();
  let mut lines = stdin.lock().lines();
  let n: usize = lines.next().unwrap().unwrap().parse().unwrap();
  let mut tree: HashMap<usize, HashSet<usize>> = HashMap::new();

  for _ in 0..(n-1) {
    let vs: Vec<usize> = lines.next().unwrap().unwrap().split(' ').map(|x| x.parse().unwrap()).map(|x: usize| x - 1).collect();
    insert_or_update(&mut tree, &vs[0], &vs[1]);
    insert_or_update(&mut tree, &vs[1], &vs[0]);
  }

  let m: usize = lines.next().unwrap().unwrap().parse().unwrap();
  let mut constr: Vec<(usize, usize)> = Vec::new();
  for _ in 0..m {
    let vs: Vec<usize> = lines.next().unwrap().unwrap().split(' ').map(|x| x.parse().unwrap()).map(|x: usize| x - 1).collect();
    constr.push((vs[0], vs[1]));
  }

  let parents = create_parents(&tree, n);

  let lca_memo: Vec<_> = (0..m).map(|i| lca(&parents, constr[i].0, constr[i].1)).collect();
  let num_paths = 1 << m;
  let mut total: i32 = 0;
  let num_all = 1 << (n - 1);
  for paths in 1..num_paths {
    let cs: Vec<usize> = (0..m).filter(|i| paths & (1 << i) != 0).collect();
    let sign = if cs.len() % 2 == 0 { -1 } else { 1 };
    let mut edges = HashSet::new();
    for c in &cs {
      let (mut x, mut y) = constr[*c];
      let l = lca_memo[*c];
      while x != l {
        edges.insert(x);
        x = parents[x].0;
      }
      while y != l {
        edges.insert(y);
        y = parents[y].0;
      }
    }
    let size = n - 1 - edges.len();
    total += sign * (1 << size) as i32;
  }
  println!("{}", num_all - total);
}