The Game of Death

Starting from player 1, follow the pointed-to players and report the first step that reaches player N, or 0 when it never does.

Easy3GraphSimulationInterviewNo attempts yetTime limit1sMemory limit256 MB

Problem

Heehyun and Jookyung like the game of death.

The rules are these.

  1. Every player points at exactly one player. Pointing at yourself is allowed.
  2. The player who starts the game says a positive integer KK.
  3. The player that the starter points at is the first player pointed at, the player that this player points at is the second one, and so on. The KK-th player pointed at loses.

Heehyun starts the game and wants Jookyung to lose. Find the number Heehyun has to say.

Input

The first line has the number of test cases TT (1T1001 \le T \le 100).

The first line of each test case has the number of players NN (1N100001 \le N \le 10000). Each of the next NN lines has one integer, the ii-th of them being AiA_i (1AiN1 \le A_i \le N), the player that player ii points at.

Heehyun is player 1 and Jookyung is player NN. When NN is 1, Heehyun and Jookyung are the same person.

Output

For each test case, print on one line the smallest KK that makes Jookyung lose.

If no number makes Jookyung lose, print 0.