Cats and Dogs
Time limit1sMemory limit128 MB
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 . ()
The first line of each test case contains the number of cats , the number of dogs , and the number of viewers , separated by spaces. (, )
Each of the next 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 and dog numbers are at most . 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.