Network Flow - Homework Problem #1

Problem: flow1

You have just moved into a new home and are setting up the furnitures. However, you immediately run into a problem—how to plug in your electronics into the electric receptacles. The problem is that various electronics use different types of receptacles (for example, your toaster may use a different type from your laptop). Furthermore, you only have certain type of electric receptacles equipped in your new home. For obvious reason, you want to plug in as many electronics as possible.

To solve the problem you are facing, you decide to visit a nearby supply store. You find that they sell certain types of converter between receptacles, which will certainly help you set up your home. If your cellphone uses receptacle A and you have a converter from A to B, then you may plug the cellphone into a receptacle B using the converter. Furthermore, you can apply multiple converter on the same electronic. If you also have a converter from receptacle B to receptacle C, then you may use both converters and connect the cellphone to a receptacle C. You may assume that for each type of converter, the store has enough stock to satisfy however many you need. Note that a converter from A to B is different from a converter from B to A.

You have also found that the store sell extension cord for all types of receptacles, thereby solving the problem that the electronic may be located too far from the receptacles. Thus, you only have to worry about matching the correct receptacle. After surveying the supply store, you set out to figure out what is the maximum number of electronics you can plug in

Input

The input will consists of several test cases. The first line will contain the number of test cases. The next line will contain the number r (1 ≤ r ≤ 100), the number of different receptacle types. The receptacle types will be labeled as 0, 1, …, r-1. On a separate line is an integer n (1 ≤ n ≤ 100), the number of receptacles in your new house. The next line will contain n integers, describing the types of receptacles in the house.

The next line will contain an integer m (1 ≤ m ≤ 100), the number of electronics you have. Next comes m integers in the same line, type of receptacle that each electronic uses.

Finally, there will be an integer k (1 ≤ k ≤ 100) on its own line, the number of types of converter the supply store has. Each of the next k lines will contain a pair of integer separated by a space, describing the converter. For example "2 3" (quotes for clarity only) means the converter can connect a type 2 electronic to a type 3 receptacle (or a converter whose left side is a 3).

Output

For each test case, output an integer on a line—the maximum number of electronics you can connect to an electric receptacle.

Sample Input

2
5
4
0 1 2 3
5
1 2 1 1 4
3
1 4
4 0
4 3
6
2
0 3
2
2 5
4
2 4
4 1
1 0
3 5

Sample Output

4
1