Under construction

Still being built. Things may change

Circle Dance

Interview

Time limit1sMemory limit128 MB

Summary
Find the largest want-link cycle in which each girl is disliked by fewer than half of the members.
Level

Medium4 of 10

Topics
Graph, DFS
Solved
No attempts yet

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 NN girls who want to take part in this circle dance, numbered from 11 to NN. 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 SS of girls that satisfies all of the following conditions:

  1. SS contains at least 22 girls.
  2. The girls of SS can be arranged in a circle i1,i2,…,imi_1, i_2, \dots, i_m (where m=∣S∣m = |S|) such that i1i_1 wants to stand next to i2i_2, i2i_2 wants to stand next to i3i_3, ..., im−1i_{m-1} wants to stand next to imi_m, and imi_m wants to stand next to i1i_1.
  3. For every girl in SS, the number of girls in SS who do not want to dance with her is strictly less than ⌈m/2⌉\lceil m/2 \rceil. (For a real number xx, ⌈x⌉\lceil x \rceil is the smallest integer that is greater than or equal to xx.)

For example, suppose girl 11 wants to stand next to girl 22, girl 22 next to girl 33, girl 33 next to girl 44, girl 44 next to girl 11, girl 55 next to girl 66, and girl 66 next to girl 55. Suppose also that girl 11 and girl 22 do not want to dance with girl 44. The group {1,2,3,4}\{1, 2, 3, 4\} satisfies conditions (1) and (2) but not condition (3), because two girls (girls 11 and 22) do not want to dance with girl 44, and 22 is not less than ⌈4/2⌉=2\lceil 4/2 \rceil = 2. Therefore the largest good group is {5,6}\{5, 6\}.

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 TT.

Each test case starts with a line containing an integer NN (1≤N≤10,0001 \le N \le 10{,}000), the number of girls. Each of the next NN lines describes one girl: line ii contains two integers jj and kk, where jj is the girl whom girl ii wants to stand next to, and kk is the girl whom girl ii does not want to dance with. If girl ii named no disliked girl, then kk is −1-1.

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 −1-1.

Examples1

  1. Example 1

    Input
    3
    6
    2 4
    3 4
    4 -1
    1 -1
    6 -1
    5 -1
    5
    2 5
    3 5
    4 5
    5 -1
    1 -1
    11
    2 5
    3 5
    4 5
    5 10
    6 10
    1 10
    8 10
    9 10
    10 7
    11 -1
    7 9
    
    Expected output
    2
    -1
    5