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::collections::VecDeque;

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());

    let n: usize = sc.read();
    let mut graph = vec![vec![]; n];
    let mut edges = vec![];
    for i in 0..(n - 1) {
        let a = sc.read::<usize>() - 1;
        let b = sc.read::<usize>() - 1;
        graph[a].push((b, i));
        graph[b].push((a, i));
        edges.push((a, b));
    }

    let m: usize = sc.read();
    let mut rules = vec![];
    for _ in 0..m {
        let a = sc.read::<usize>() - 1;
        let b = sc.read::<usize>() - 1;
        let edges = mark(&graph, a, b);
        rules.push(edges);
    }

    let mut edge_rule = vec![vec![]; n - 1];
    for rule_id in 0..m {
        for &edge_id in rules[rule_id].iter() {
            edge_rule[edge_id].push(rule_id);
        }
    }

    let mut dp = vec![vec![0; 1 << m]; n];
    dp[0][0] = 1;
    for edge_id in 0..(n - 1) {
        let mut edge_mask = 0;
        for &rule in edge_rule[edge_id].iter() {
            edge_mask |= 1 << rule;
        }
        //        println!("mask={}", edge_mask);

        for cur_mask in 0..(1 << m) {
            dp[edge_id + 1][cur_mask] += dp[edge_id][cur_mask];
            dp[edge_id + 1][cur_mask | edge_mask] += dp[edge_id][cur_mask];
        }
    }

    println!("{}", dp[n - 1][(1 << m) - 1]);
    //    eprintln!("{:?}", dp);
}

fn dfs(
    v: usize,
    p: usize,
    graph: &Vec<Vec<(usize, usize)>>,
    dp: &mut Vec<Vec<usize>>,
    edge_rule: &Vec<Vec<usize>>,
    m: usize,
) {
    let n = graph.len();
    for &(next, _) in graph[v].iter() {
        if next == p {
            continue;
        }

        dfs(next, v, graph, dp, edge_rule, m);
    }

    for &(next, id) in graph[v].iter() {
        if next == p {
            continue;
        }
        let rules = &edge_rule[id];
        let mut rule_mask = 0;
        for &rule in rules.iter() {
            rule_mask |= 1 << rule;
        }

        for mask in 0..(1 << m) {
            let black_next_mask = mask | rule_mask;
            let prev = dp[next][mask];
            dp[v][mask] *= prev;
            dp[v][black_next_mask] *= prev;
        }
    }
}

fn mark(graph: &Vec<Vec<(usize, usize)>>, from: usize, to: usize) -> Vec<usize> {
    let mut edges = vec![];
    let n = graph.len();
    let mut dist: Vec<i64> = vec![1 << 50; n];
    let mut q = VecDeque::new();
    q.push_back(from);
    dist[from] = 0;
    while let Some(v) = q.pop_front() {
        for &(next, _) in graph[v].iter() {
            if dist[next] > dist[v] + 1 {
                dist[next] = dist[v] + 1;
                q.push_back(next);
            }
        }
    }

    let mut q = VecDeque::new();
    q.push_back(to);
    while let Some(v) = q.pop_front() {
        for &(next, id) in graph[v].iter() {
            if dist[v] > dist[next] {
                edges.push(id);
                q.push_back(next);
            }
        }
    }
    edges
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> IO<R, W> {
        IO(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: std::ops::Deref<Target = str>>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}