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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using CompLib.Util;

public class Program
{
    private int N;
    private List<Edge>[] Edges;
    private int M;
    private int[] U, V;

    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        Edges = new List<Edge>[N];
        for (int i = 0; i < N; i++)
        {
            Edges[i] = new List<Edge>();
        }

        for (int i = 0; i < N - 1; i++)
        {
            int a = sc.NextInt() - 1;
            int b = sc.NextInt() - 1;
            Edges[a].Add(new Edge(b, i));
            Edges[b].Add(new Edge(a, i));
        }

        M = sc.NextInt();
        U = new int[M];
        V = new int[M];
        for (int i = 0; i < M; i++)
        {
            U[i] = sc.NextInt() - 1;
            V[i] = sc.NextInt() - 1;
        }


        long[] flag = new long[M];

        for (int i = 0; i < M; i++)
        {
            Search(U[i], -1, V[i], out flag[i]);
        }

        long l = 1 << M;
        long ans = 0;
        for (int i = 0; i < l; i++)
        {
            long t = 0;
            int cnt = 0;
            for (int j = 0; j < M; j++)
            {
                if ((i & (1 << j)) == 0) continue;
                t |= flag[j];
                cnt++;
            }

            long n = 1;
            for (int j = 0; j < N - 1; j++)
            {
                if ((t & (1L << j)) == 0) n *= 2;
            }

            if (cnt % 2 == 0)
            {
                ans += n;
            }
            else
            {
                ans -= n;
            }
        }

        Console.WriteLine(ans);
    }

    bool Search(int p, int parent, int target, out long ans)
    {
        ans = -1;
        if (p == target)
        {
            ans = 0;
            return true;
        }

        foreach (Edge edge in Edges[p])
        {
            if (edge.To == parent) continue;
            long o;
            if (Search(edge.To, p, target, out o))
            {
                ans = o | (1L << edge.Num);
                return true;
            }
        }

        return false;
    }

    public static void Main(string[] args) => new Program().Solve();
}

struct Edge
{
    public int To, Num;

    public Edge(int t, int i)
    {
        To = t;
        Num = i;
    }
}


namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            while (_index >= _line.Length)
            {
                _line = Console.ReadLine().Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            _line = Console.ReadLine().Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}