Crazy old lady
The following problem is a variation of a well known puzzle in the Probability theory. There are n seats on a plane and all of the seats are assigned to passengers. Passengers are asked to enter the plane, one at a time, in order of their seats, from 1 to n (the passenger that is assigned seat number 1 enters first, next enters the passenger that is assigned seat number 2, and so on...).
As you might have guessed, queues are too mainstream for a crazy old lady, so she usually goes ahead of everyone and enters first (despite the fact that she might not be assigned the first seat), furthermore she takes some seat that she likes (by chance, it may happen to be her own seat).
After she has taken a seat, the next passengers enter with respect to their seat number (as described above) and take seat as follows:
if his/her seat is vacant, then he/she takes this seat.
but if his/her seat is already taken, he/she takes any of the remaining seats.
Which seat takes the last passenger? The answer is simple: the last passenger takes either his own seat or the seat of the crazy old lady!
Assume that crazy old lady occupy seat j, which is not her own. When passenger that was assigned the seat j will enter, he/she will have to take some other seat from a set of free seats. This situation then may repeat with next passengers. But when some passenger take the seat of old crazy lady, obviously (!) all of the following passengers take their own seats. In this problem we would like to know the original seat of old crazy lady.
Input
May contain several tests. First line of Input contains the number of tests t (1 ≤ t ≤ 10). Each of the next t lines contains n + 1 numbers, separated by space, first is n (1 ≤ n ≤ 10^3
), then n numbers follow: the seats taken by passengers in order of their entrance (p[1]
, p[2]
, ..., p[n]
- where p[i]
means that the passenger that was i-th to enter took seat p[i]
). Input is guaranteed to be correct, i.e. to satisfy conditions of the problem statement.
Output
Output the seat of old crazy lady, if it can be determined uniquely, otherwise, output 0.