Circle Dance
InterviewTime limit1sMemory limit128 MB
Find the largest want-link cycle in which each girl is disliked by fewer than half of the members.
Problem
Ganggangsullae is a Korean circle dance traditionally performed by women during Chuseok, the harvest festival held on the fifteenth day of the eighth lunar month. Wearing the traditional dress called hanbok, the women gather in an open field, join hands in a circle facing the center, and move slowly around to the rhythm of a song. The pace gradually quickens until the dance ends in a whirling climax.
There are girls who want to take part in this circle dance, numbered from to . A director wants to form a group for the dance in which cooperation among the members matters a great deal. Every girl must name exactly one girl she wants to stand next to while dancing, and she may optionally name one girl she does not want to dance with.
From these lists, the director looks for the largest good group. A good group is a set of girls that satisfies all of the following conditions:
- contains at least girls.
- The girls of can be arranged in a circle (where ) such that wants to stand next to , wants to stand next to , ..., wants to stand next to , and wants to stand next to .
- For every girl in , the number of girls in who do not want to dance with her is strictly less than . (For a real number , is the smallest integer that is greater than or equal to .)
For example, suppose girl wants to stand next to girl , girl next to girl , girl next to girl , girl next to girl , girl next to girl , and girl next to girl . Suppose also that girl and girl do not want to dance with girl . The group satisfies conditions (1) and (2) but not condition (3), because two girls (girls and ) do not want to dance with girl , and is not less than . Therefore the largest good group is .
Given every girl's submitted list, write a program that reports the number of girls in the largest good group.
Input
The first line contains the number of test cases .
Each test case starts with a line containing an integer (), the number of girls. Each of the next lines describes one girl: line contains two integers and , where is the girl whom girl wants to stand next to, and is the girl whom girl does not want to dance with. If girl named no disliked girl, then is .
Output
For each test case, print a single line containing the number of girls in the largest good group. If there is no good group, print .