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: Wrong Answer)

#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
vector<int> a[110], b[111];
int mem[1<<21][110], mp[55][55], mskk[55];
ll mod=1e9+7;
vector<int> v;
void dfs(int x, int par){
    v.push_back(x);
    for(int i=0; i<a[x].size(); ++i){
        if(a[x][i]==par)
            continue;
        dfs(a[x][i], x);
    }
}
void dfs2(int x, int par){
    v.push_back(x);
    for(int i=0; i<a[x].size(); ++i){
        if(a[x][i]==par)
            continue;
        dfs2(a[x][i], x);
        v.push_back(x);
    }
}
int m;
int pos[55];
ll dp(int x, int msk){
    if(x+1==v.size()){
        return msk==(1<<m)-1;
    }
    if(mem[msk][x]!=-1){
        return mem[msk][x];
    }
    ll res=0;
    res=dp(x+1, msk);
    if(mp[v[x]][v[x+1]]!=-1 && x<pos[v[x+1]])
        res+=dp(x+1, msk|mskk[mp[v[x]][v[x+1]]]);
    res%=mod;
    mem[msk][x]=res;
    return res;
}
int x[55], y[55], l[55][55];
int main(){
    int n;
    while(cin>>n){
        ll ans=0;
        memset(mp, -1, sizeof mp);
        for(int i=0; i<n-1; ++i){
            scanf("%d%d",&x[i],&y[i]);
            a[x[i]].push_back(y[i]);
            a[y[i]].push_back(x[i]);
            mp[x[i]][y[i]]=mp[y[i]][x[i]]=i;
        }
        int q1[22], q2[22];
        cin>>m;
        memset(l, -1, sizeof l);
        for(int i=0; i<m; ++i){
            scanf("%d%d",q1+i, q2+i);
            l[q1[i]][q2[i]]=l[q2[i]][q1[i]]=i;
        }
        vector<int>v1, v2;
        for(int i=0; i<n-1; ++i){
            int x1=x[i], x2=y[i];
            dfs(x1, x2);
            v1=v;v.clear();
            dfs(x2, x1);
            v2=v;v.clear();
            for(int j=0; j<v1.size(); ++j){
                for(int k=0; k<v2.size(); ++k){
                    if(l[v1[j]][v2[k]]>-1){
                        mskk[i]|=(1<<l[v1[j]][v2[k]]);
                    }
                }
            }
        }
        memset(mem, -1, sizeof mem);
        dfs2(1,-1);
        for(int i=(int)(v.size())-1; i>=0; --i){
            pos[v[i]]=i;
        }
        assert(v.size()<110);
        ans=dp(0, 0);
        cout<<ans<<endl;
    }
  return 0;
}