Cats and Dogs

Time limit1sMemory limit128 MB

Summary
placeholder
Level

Medium6 of 10

Topics
Graph
Solved
No attempts yet

Problem

"Cats and Dogs" is a popular survival TV show. Several dogs and cats appear on it, and in each round one animal is eliminated until a single survivor claims the title of "Best Pet."

In every episode, each viewer votes for one animal to advance to the next round and one animal to be eliminated in the current round. Every viewer either loves cats and dislikes dogs, or loves dogs and dislikes cats. Therefore, each vote consists of the number of one cat and the number of one dog.

A viewer keeps watching the show if and only if their vote is honored; otherwise they stop watching. A vote is honored when the animal the viewer chose to advance actually advances to the next round, and at the same time the animal the viewer chose to eliminate is actually eliminated in this round.

The producer wants to maximize the number of viewers who keep watching. Given every viewer's vote, find the maximum possible number of viewers whose votes are honored.

Input

The first line contains the number of test cases TT. (T≤100T \le 100)

The first line of each test case contains the number of cats cc, the number of dogs dd, and the number of viewers vv, separated by spaces. (1≤c,d≤1001 \le c, d \le 100, 0≤v≤5000 \le v \le 500)

Each of the next vv lines describes one viewer's vote. On each line, the first animal is the one that viewer chose to advance to the next round, and the second animal is the one they chose to eliminate in this round. A cat is written starting with C and a dog starting with D, followed by the animal's number. Cat numbers are at most cc and dog numbers are at most dd. For example, D42 denotes dog number 42.

Output

For each test case, print on its own line the maximum number of viewers whose votes are honored.

Examples4

  1. Example 1

    Input
    2
    1 1 2
    C1 D1
    D1 C1
    1 2 4
    C1 D1
    C1 D1
    C1 D2
    D2 C1
    
    Expected output
    1
    3
    
  2. Example 2

    Input
    1
    5 5 0
    
    Expected output
    0
    
  3. Example 3

    Input
    1
    3 3 3
    C1 D1
    C2 D2
    C3 D3
    
    Expected output
    3
    
  4. Example 4

    Input
    1
    2 2 2
    C1 D1
    D2 C2
    
    Expected output
    2