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# (.NET Core 3.1.201) (judge verdict: Time Limit Exceeded)

using System;
using System.Linq;
using System.Collections.Generic;
using static System.Console;
using System.Text;
using System.IO;
using static System.Math;
using System.Numerics;

namespace AtCoder
{
    class Program
    {
        static public long[] Sarray() { return ReadLine().Split().Select(long.Parse).ToArray(); }
        static public List<long> Slist() { return ReadLine().Split().Select(long.Parse).ToList(); }
        static public (T1 a, T2 b) Slice<T1, T2>()
        {
            var t = ReadLine().Split();
            return (
            (T1)Convert.ChangeType(t[0], typeof(T1)),
            (T2)Convert.ChangeType(t[1], typeof(T2)));
        }
        static long Mod = (long)1e9 + 7;
        static void Main(string[] args)
        {
            SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
            Solve();
            Out.Flush();
            Read();
        }
        static void Solve()
        {
            var N = Sarray()[0];
            graph = Enumerable.Repeat(0, (int)N).Select(_ => new List<(long v, long n)>()).ToArray();
            for (var i = 0; i < N - 1; ++i)
            {
                (long a, long b) = Slice<long, long>();
                --a; --b;
                graph[a].Add((b, i));//i:枝番号
                graph[b].Add((a, i));
            }
            var M = Sarray()[0];
            var ele = Enumerable.Repeat(0, (int)M).Select(_ => new List<long>()).ToArray();
            for (var i = 0; i < M; ++i)
            {
                (long u, long v) = Slice<long, long>();
                --u; --v;
                dfs(-1, u, v, ele[i]);//ele:(u,v)間の枝の集合
            }
            var edgeNum = N - 1;
            var ans = 0L;
            var _2Pow = new long[50];
            _2Pow[0] = 1;
            for (var i = 1; i < 50; ++i)
                _2Pow[i] = _2Pow[i - 1] * 2;
            // 包除原理
            for (var bit = 1L; bit < (1L << (int)M); ++bit)
            {
                var popcount = 0;
                var uniCnd = new HashSet<long>();
                var bp = 1L;
                for (var i = 0; i < M; ++i)
                {
                    if (0 != (bit & bp))
                    {
                        ++popcount;
                        foreach (var e in ele[i])
                            uniCnd.Add(e);
                    }
                    bp <<= 1;
                }
                var cndEdge = uniCnd.Count();//使う条件で使う枝の数
                var t = _2Pow[edgeNum - cndEdge] * (_2Pow[cndEdge] - 1);//条件外の枝は白黒フリー。条件の枝は全部白の場合を除外
                if (popcount % 2 == 1) ans += t;
                else ans -= t;
            }
            WriteLine(ans);
        }
        static List<(long v, long n)>[] graph;
        static bool dfs(long p, long n,long g, List<long> ele)
        {
            if (n == g) return true;
            foreach (var vn in graph[n])
            {
                if (vn.v == p) continue;
                ele.Add(vn.n);
                if (dfs(n, vn.v, g, ele)) 
                    return true;
                ele.RemoveAt(ele.Count - 1);
            }
            return false;
        }
    }
}