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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
#define rep(i,n) for (ll i=0;i<(n);++i)
#define rep2(i,a,b) for (ll i=(a);i<(b);++i)
#define debug(x) cout << #x << '=' << x << endl
#define all(v) (v).begin(),(v).end()
const ll MOD=1e9+7;
//const ll MOD=998244353;
const ll INF=1e9;
const ll IINF=1e18;
const double EPS=1e-8;
const double pi=acos(-1);

template<class T> inline bool chmin(T &a,T b){
    if (a>b){a=b; return true;} return false;
}
template<class T> inline bool chmax(T &a,T b){
    if (a<b){a=b; return true;} return false;
}

const int MAX_N=51;
vector<int> par(MAX_N);
vector<vector<int>> G(MAX_N);

void dfs(int v,int p){
    for(int u:G[v]){
        if (u==p) continue;
        par[u]=v;
        dfs(u,v);
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N; cin >> N;
    map<Pii,int> m;
    rep(i,N-1){
        int a,b; cin >> a >> b; --a,--b;
        m[{a,b}]=m[{b,a}]=i;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
    int M; cin >> M;
    vector<int> u(M),v(M);
    rep(i,M){
        cin >> u[i] >> v[i];
        --u[i],--v[i];
    }
    vector<ll> po(N,1);
    rep(i,N-1) po[i+1]=po[i]*2;
    ll ans=0;
    rep(bit,1<<M){
        vector<int> cnt(N-1,0);
        int flag=0;
        rep(i,M){
            if (bit&1<<i){
                ++flag;
                rep(i,N) par[i]=-1;
                dfs(u[i],-1);
                int t=v[i];
                while(t!=u[i]){
                    ++cnt[m[{t,par[t]}]];
                    t=par[t];
                }
            }
        }
        int c=0;
        rep(i,N-1) if (cnt[i]==0) ++c;
        if (flag%2==0) ans+=po[c];
        else ans-=po[c];
    }
    cout << ans << endl;
}