This page is still under construction.

Parts of this page are still being built. What you see may change.

Parades

Time limit3sMemory limit256 MB

Summary
Pick the most parade routes between pairs of junctions in a tree so no street is shared by two parades.
Level

Hard8 of 10

Topics
Dynamic programming, Tree, Graph
Solved
No attempts yet

Problem

The City of Eternal Festivities has nn street junctions and n−1n-1 two way streets, and each street connects two junctions. Between any two junctions there is exactly one path along the streets. No junction has more than 10 streets attached to it.

Every 13th of September, the 256th day of the year, festivities are held all over the city. The citizens have planned mm parades. Parade ii starts at junction uiu_i and ends at junction viv_i, following the unique path between those two junctions.

As the mayor you are responsible for public safety, so you ruled that no street may be used by two parades. Parades may share junctions, and they may even share an endpoint.

Find the largest number of the planned parades that can be held under that rule.

Input

The first line contains the number of test cases TT. Each test case follows in this format.

The first line contains the number of junctions nn (2≤n≤10002 \le n \le 1000). Each of the next n−1n-1 lines contains two integers aa and bb, meaning that junctions aa and bb are connected by a street (1≤a,b≤n1 \le a, b \le n, a≠ba \ne b). At most 10 streets leave any junction. The next line contains the number of planned parades mm (0≤m≤n(n+1)/20 \le m \le n(n+1)/2).

Each of the next mm lines contains two integers uiu_i and viv_i, meaning that parade ii starts at junction uiu_i and ends at junction viv_i (1≤ui,vi≤n1 \le u_i, v_i \le n, ui≠viu_i \ne v_i). No two parades share both endpoints.

Output

For each test case, print on one line the largest number of parades that can be held with no street used by more than one parade.

Examples2

  1. Example 1

    Input
    1
    6
    1 2
    2 3
    3 4
    3 5
    3 6
    4
    1 3
    4 5
    5 6
    6 4
    
    Expected output
    2
    
  2. Example 2

    Input
    1
    2
    1 2
    1
    1 2
    
    Expected output
    1