Museum Tour

No attempts yetTime limit1sMemory limit512 MB

Problem

A large museum has many rooms and many corridors. Planning a tour through it takes real work, so the museum put a simple rule on its signs and asks visitors to follow it.

If room vv has dd doors leading through corridors to other rooms, those doors and the matching corridors carry numbers 1,2,,d1, 2, \dots, d that are used only inside that room. The rule has two parts.

  • In the room where the tour starts, leave through door 11.
  • If you entered a room through door ii, leave through the door with the next number: door i+1i + 1 when i<di < d, and door 11 when i=di = d.

The picture below shows a tour that starts in room 11 and passes rooms 1,2,3,4,5,61, 2, 3, 4, 5, 6 in this order, walking through every corridor at least once.

Exhibits hang in the corridors as well as in the rooms. What matters is whether a visitor who follows the rule, does not get bored, and walks long enough finally passes through every corridor at least once. Call a room a good starting room when a tour that begins there does exactly that.

The door numbers are already fixed and are given in the input. Count the good starting rooms.

At most 3 corridors leave each room, and the whole museum is connected: you can walk between any two rooms, possibly passing through other rooms on the way. All corridors leaving one room lead to different rooms.

Input

The input contains several test cases. The first line contains the number of test cases tt (t100t \le 100).

Each test case begins with a line containing the number of rooms nn (3n1053 \le n \le 10^5). The next nn lines describe the doors, one line per room, in order of the door numbers. Line ii starts with the number of doors dd (1d31 \le d \le 3), followed by dd integers r1,r2,,rdr_1, r_2, \dots, r_d. Here rjr_j is the room that door jj of room ii leads to (1rjn1 \le r_j \le n, rjir_j \ne i, and rjrkr_j \ne r_k when jkj \ne k).

All corridors are bidirectional, so if there is a door from room xx to room yy, there is a door from room yy to room xx as well. The total size of the input does not exceed 50MB.

Output

For each test case, print the number of good starting rooms on one line.