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 C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>

//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
//#pragma GCC optimize ("unroll-loops")

using namespace std;
using namespace __gnu_pbds;

#define all(x) x.begin(), x.end()
#define mp make_pair
#define X first
#define Y second

template<typename T> // order_of_key(), *find_by_order()
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
mt19937_64 gen(time(nullptr));

ll const mod = 1e9 + 7;

namespace {
    ll mul(ll a, ll b) {
        ll val = a * b - (ll) ((ld) a * b / mod) * mod;
        if (val < 0)
            val += mod;
        if (val >= mod)
            val -= mod;
        return val;
    }

    ll poww(ll a, ll b) {
        ll val = 1;
        a %= mod;
        while (b > 0) {
            if (b % 2)
                val = mul(a, val);
            a = mul(a, a);
            b >>= 1;
        }
        return val % mod;
    }

    ll inv(ll a) {
        return poww(a, mod - 2);
    }
}

ll const maxn = 55;

vector<int> g[maxn];

map<pair<int, int>, int> mapa;

bitset<51> kek[maxn];
int p[maxn];
bool used[maxn];

void dfs(ll u) {
    used[u] = 1;
    for (ll v : g[u]) {
        if (used[v])
            continue;
        p[v] = u;
        dfs(v);
    }
}

int const maxk = 1 << 21;
int rofl[maxk];

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    int id = 0;
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
        if (a > b)
            swap(a, b);
        mapa[{a, b}] = id++;
    }
    int m;
    cin >> m;

    for (int j = 0; j < m; j++) {
        memset(p, 0, sizeof(p));
        memset(used, 0, sizeof(used));
        int a, b;
        cin >> a >> b;
        dfs(a);
        vector<int> path;
        while (b != a) {
            path.push_back(b);
            b = p[b];
        }
        path.push_back(a);
        for (int i = 0; i < path.size() - 1; i++) {
            int x = path[i], y = path[i + 1];
            if (x > y)
                swap(x, y);
            kek[j][mapa[{x, y}]] = 1;
        }
    }
    for (int mask = 0; mask < (1 << m); mask++) {
        bitset<51> roflan;
        for (int j = 0; j < m; j++) {
            if ((1 << j) & mask) {
                roflan |= kek[j];
            }
        }
        rofl[mask] = roflan.count();
    }
    ll ans = 0;

    for (int mask = 0; mask < (1 << m); mask++) {
        int cnt = rofl[mask];
        int sz = __builtin_popcount(mask);
        if (sz % 2) {
            ans -= (1LL << (n - 1 - cnt));
        } else {
            ans += (1LL << (n - 1 - cnt));
        }
    }

    cout << ans << endl;

    return 0;
}

/*
4
1 5 10 50
3
2
8
50


*/