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"
using namespace std;
#define REP(i, n) for(ll i = 0;i < n;i++)
#define ll long long
#define MOD 1000000007
using vi = vector<ll>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける

ll n,m;
int e;
vvi data;

//DFS
vector<vector<pair<int, int>>> g;
const int N = 1e5;
int used[N];
int col[N];

int dfs(int i, int c,int num) {
  if(used[i]) return 0;
  used[i] = 1;
  
  for(auto to : g[i]) {
    if (to.first==c){
      e=1;
      data[to.second].emplace_back(num);
      break;
    }else{
      e=dfs(to.first, c,num);
      if (e==1){
        data[to.second].emplace_back(num);
        break;
      }
    }
  }

  return e;
}




int main() {
  cin>>n;
  g.resize(n);
  for(int i = 0; i < n-1; i++) {
    int a, b;
    cin >> a >> b ;
    a--; b--;
    g[a].emplace_back(b, i);
    g[b].emplace_back(a, i);
  }

  cin>>m;
  vi u(m),v(m);
  REP(i,m){
    cin>>u[i]>>v[i];
    u[i]--;
    v[i]--;
  }

  
  //
  data.resize(50*20*2);
  REP(i,m){
    e=0;
    REP(j,50){used[j]=0;}
    e=dfs(u[i], v[i],i);
  }

  /*
  cout<<endl;
  REP(i,50){
    cout<<i<<" ";
    REP(j,data[i].size()){
      cout<<data[i][j]<<" ";
    }
    cout<<endl;
      
    
  }
  */

  ll ans=0LL;
  //1乗のやつを計算
  vi numm1(20);
  vvi numm2;
  numm2.resize(50*n);

  REP(i,n-1){
    REP(j,data[i].size()){
      numm1[data[i][j]]++;
      numm2[data[i][j]].emplace_back(i);
    }
  }

  /*
  REP(i,20){
    if (numm1[i]!=0){
      ans+=1LL<<((n-1)-numm1[i]);
    }
  }
  */

  //2乗のやつを計算
  //vvi ddata(20, vi(50,-1));//mi番目の通る道


  vi tabl(n-1,0);
  REP(i,1<<m){
    vi v(m);
    ll flag=0;
    REP(j,m){
      v[j]=(i>>j)%2;
      flag+=v[j];
    }
    flag%=2;

    
    REP(j,m){
      if (v[j]==1){
        for(int k=0;k<numm2[j].size();k++){
          tabl[numm2[j][k]]=1;
        }
      }
      int sm=0;
      for(int k=0;k<n-1;k++){
        sm+=tabl[k];
        tabl[k]=0;
      }
      ans+=(1LL<<(n-1-sm))*(flag*2LL-1LL);
    }

  }
/*

  REP(i,50){
    REP(j,data[i].size()){
      for(int k=j+1;k<data[i].size();k++){
        tabl[data[i][j]*20+data[i][k]]++;
        tabl[data[i][k]*20+data[i][j]]++;
      }
    }
  }


  ll cnt=0;
  ll cnt1=0;
  REP(i,20){
    for(int j=i+1;j<20;j++){
      if ((numm1[i]!=0)&(numm1[j]!=0)){
        
        
        cnt=tabl[i*20+j];
        cnt1=numm1[i]+numm1[j]-cnt;
        ans-=(1LL<<(n-1-cnt1))*( (1LL<<(numm1[i]-cnt))+(1LL<<(numm1[j]-cnt)) -2LL);
      }
    }
  }
*/
  //cout<<ans<<endl;


  //ans=(1LL<<(n-1))-ans;

  cout<<-ans<<endl;
  return 0;
}

/*
3
1 2
2 3
2
1 3
2 3
*/