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)

#pragma GCC optimize("Ofast","unroll-loops")
//#pragma GCC push_options
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#include <xmmintrin.h>
#include <immintrin.h>
using namespace::std;
__attribute__((constructor))void init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include<ext/pb_ds/tag_and_trait.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
typedef mp::number<mp::cpp_dec_float<0>> cdouble;
typedef mp::cpp_int cint;
template<typename T>using pbds=__gnu_pbds::tree<T,__gnu_pbds::null_type,less<T>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>;
template<typename T>using pbds_map=__gnu_pbds::tree<T,T,less<T>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>;
template<typename T,typename E>using hash_map=__gnu_pbds::gp_hash_table<T,E>;
template<typename T>using pqueue =__gnu_pbds::priority_queue<T, less<T>,__gnu_pbds::rc_binomial_heap_tag>;
typedef long long lint;
#define INF (1LL<<60)
#define IINF (1<<30)
#define EPS (1e-10)
#define MOD 1000000007LL
typedef vector<lint> vec;
typedef vector<vector<lint>> mat;
typedef vector<string> svec;
typedef vector<vector<string>> smat;
template<typename T>inline void numout(T t){bool f=0;for(auto i:t){cout<<(f?" ":"")<<i<INF/2?i:"INF";f=1;}cout<<endl;}
template<typename T>inline void numout2(T t){for(auto i:t)numout(i);}
template<typename T>inline void output(T t){bool f=0;for(auto i:t){cout<<(f?" ":"")<<i;f=1;}cout<<endl;}
template<typename T>inline void output2(T t){for(auto i:t)output(i);}
template<typename T>inline void _output(T t){bool f=0;for(lint i=0;i<t.size();i++){cout<<f?"":" "<<t[i];f=1;}cout<<endl;}
template<typename T>inline void _output2(T t){for(lint i=0;i<t.size();i++)output(t[i]);}
#define rep(i,n) for(lint i=0;i<lint(n);++i)
#define repi(i,a,b) for(lint i=lint(a);i<(lint)(b);++i)
#define rrep(i,n) for(lint i=lint(n)-1;i>=0;--i)
#define rrepi(i,a,b) for(lint i=lint(b)-1;i>=lint(a);--i)
#define irep(i) for(lint i=0;;++i)
#define all(n) begin(n),end(n)
#define dist(a,b,c,d) sqrt(pow(a-c,2)+pow(b-d,2))
inline lint gcd(lint A,lint B){return B?gcd(B,A%B):A;}
inline lint lcm(lint A,lint B){return A/gcd(A,B)*B;}
// inline cint cgcd(cint A,cint B){return B?cgcd(B,A%B):A;}
// inline cint clcm(cint A,cint B){return A/cgcd(A,B)*B;}
inline bool chmin(auto& s,const auto& t){bool res=s>t;s=min(s,t);return res;}
inline bool chmax(auto& s,const auto& t){bool res=s<t;s=max(s,t);return res;}
const vector<lint> dx={1,0,-1,0,1,1,-1,-1};
const vector<lint> dy={0,1,0,-1,1,-1,1,-1};
#define SUM(v) accumulate(all(v),0LL)
auto call=[&](auto f,auto... args){return f(f,args...);};

class mint {
  using u64 = std::uint_fast64_t;
    public:
    u64 a;
    constexpr mint(const u64 x = 0)noexcept:a(x % MOD){}
    constexpr u64 &value()noexcept{return a;}
    constexpr const u64 &value() const noexcept {return a;}
    constexpr mint operator+(const mint rhs)const noexcept{return mint(*this) += rhs;}
    constexpr mint operator-(const mint rhs)const noexcept{return mint(*this)-=rhs;}
    constexpr mint operator*(const mint rhs) const noexcept {return mint(*this) *= rhs;}
    constexpr mint operator/(const mint rhs) const noexcept {return mint(*this) /= rhs;}
    constexpr mint &operator+=(const mint rhs) noexcept {
        a += rhs.a;
        if (a >= MOD)a -= MOD;
        return *this;
    }
    constexpr mint &operator-=(const mint rhs) noexcept {
        if (a<rhs.a)a += MOD;
        a -= rhs.a;
        return *this;
    }
    constexpr mint &operator*=(const mint rhs) noexcept {
        a = a * rhs.a % MOD;
        return *this;
    }
    constexpr mint operator++(int n) const noexcept {
        return mint(*this) +=1;
    }
    constexpr mint operator--(int n) const noexcept {
        return mint(*this) -=1;
    }
    constexpr mint &operator/=(mint rhs) noexcept {
        u64 exp=MOD-2;
        while (exp) {
            if (exp % 2) {
                *this *= rhs;
            }
            rhs *= rhs;
            exp /= 2;
        }
        return *this;
    }
    mint pow(long long n){
        long long x=a;
        mint ret = 1;
        while(n>0) {
            if(n&1)(ret*=x);
            (x*=x)%=MOD;
            n>>=1;
        }
        return ret;
    }
    mint inv()const{
        int _a=a,b=MOD,u=1,v=0,t;
        while(b>0) {
            t=_a/b;
            swap(_a-=t*b,b);
            swap(u-=t*v,v);
        }
        return mint(u);
    }
    mint comb(lint b){
        using lint=long long;
        static bool init=1;
        static lint fac[1000001],ifac[1000001];
        if(init){
            init=0;
            fac[0]=1;
            ifac[0]=1;
            auto mod_pow=[&](lint x,lint n){
                lint ans = 1;
                while(n != 0){
                    if(n&1)ans=ans*x%MOD;
                    x=x*x%MOD;
                    n=n>>1;
                }
                return ans;
            };
            for(int i=0;i<1000000;i++){
                fac[i+1]=fac[i]*(i+1)%MOD;
                ifac[i+1]=ifac[i]*mod_pow(i+1, MOD-2)%MOD;
            }
        }
        if(a==0&&b==0)return 1;
        if((lint)a<b||a<0)return 0;
        lint tmp=ifac[a-b]*ifac[b]%MOD;
        return tmp*fac[a]%MOD;
    }
    
    friend ostream& operator<<(ostream& lhs, const mint& rhs) noexcept {
        lhs << rhs.a;
        return lhs;
    }
    friend istream& operator>>(istream& lhs,mint& rhs) noexcept {
        lhs >> rhs.a;
        return lhs;
    }
};
inline mint fact(lint a){return a?fact(a-1)*a:1;}


template<typename T>struct SEG{
    lint n=1;
    vector<T> node;
    T init;
    SEG(lint size,T _init){
        init=_init;
        while(n<size)n*=2;
        node.resize(n*2-1,init);
    }
    void update(lint x,T val){
        x+=(n-1);
        node[x]=func(node[x],val);
        while(x>0){
            x=(x-1)/2;
            node[x]=func(node[x*2+1],node[x*2+2]);
        }
    }
    T get(lint a,lint b,lint k=0,lint l=0,lint r=-1){
        if(r<0)r=n;
        if(r<=a||b<=l)return init;
        if(a<=l&&r<=b)return node[k];
        else{
            T vl=get(a,b,k*2+1,l,(l+r)/2);
            T vr=get(a,b,k*2+2,(l+r)/2,r);
            return func(vl,vr);
        }
    }
    T func(T s,T t){
        return s+t;
    }
};

auto prime_factor(lint n) {
    map<decltype(n),lint>ret;
    for(auto i=2;i*i<=n;i++){
        while(n%i==0){
            ret[i]++;
            n/=i;
        }
    }
    if(n!=1)ret[n]=1;
    return ret;
}

int main(){
    lint n;
    cin>>n;
    vector<vector<pair<lint,lint>>>g(n);
    rep(i,n-1){
        lint s,t;
        cin>>s>>t;
        s--;t--;
        g[s].emplace_back(t,i);
        g[t].emplace_back(s,i);
    }
    function<bool(lint,lint,lint,set<lint>&)> f=[&](lint n,lint p,lint goal,set<lint>& t)->bool{
        bool res=(n==goal);
        for(auto d:g[n]){
            if(d.first==p)continue;
            bool resc=f(d.first,n,goal,t);
            if(resc)t.emplace(d.second);
            if(resc)res=1;
        }
        return res;
    };
    lint m;
    cin>>m;
    vector<pair<lint,lint>>que(m);
    rep(i,m)cin>>que[i].first>>que[i].second;
    cint ans=0;
    rep(i,1<<m){
        lint tmp=0;
        set<lint>s;
        rep(j,m){
            if(i&(1<<j)){
                tmp++;
                f(que[j].first-1,-1,que[j].second-1,s);
            }
        }
        if(tmp%2)ans-=pow(cint(2),n-s.size()-1);
        else ans+=pow(cint(2),n-s.size()-1);
    }
    cout<<ans<<endl;
}