In Shane's neighbourhood, there are streets, and Halloween is celebrated by every house on each street. As part of the spooky season, each house only gives out one type of candy, and the candy given out by all houses follow a unique rule: the tastiness of any candy given out is a power of .
Shane's friend, Aaron, tells him to visit every house on each street from left to right, and keep the candies he has recieved in a list. Aaron also informs Shane that at any given house on street , the following will occur:
If the last candies recieved from street in Shane's list are of the same tastiness, say , Shane can exchange those two candies and recieve a new candy of tastiness . Shane can repeat this exchange as many times as he likes at any given house.
Shane will recieve a candy of tastiness .
Note that when an exchange is made, the two candies exchanged are removed from the list, while the candy recieved is added.
Aaron then proceeds to tell Shane that he managed to obtain a certain sequence of candies from each street, following the above rules, and challenges Shane to do the same. However, knowing that Aaron is a massive troll, Shane wonders if he is being truthful about every sequence. In addition, Shane needs to leave to go trick-or-treating with his girlfriend soon, as he told her dad "I'll have her home by 10".
Can you help Shane figure out which sequences Aaron is being truthful about?
Constraints
, the number of houses on street .
, the number of candies in Aaron's claimed sequence from street .
, where is the tastiness of the candy from house on street .
, where is the tastiness of the th candy in Aaron's claimed sequence from street .
Subtask 1 [20%]
Subtask 2 [80%]
No Further Constraints.
Input Specification
The first line of input will contain .
The next lines of input will contain the following repeated lines:
- integers, and ,
- integers, with representing the exponent of the tastiness of a candy from house on street ,
- integers, with representing the exponent of the tastiness of the th candy in Aaron's claimed sequence from street .
Ouput Specification
For each street, print VALID
if Shane can exchange his way to the sequence of candies Aaron claims to have, and print INVALID
otherwise.
Sample Input
6
3 3
1 2 3
1 2 3
3 2
1 1 3
2 3
4 3
1 2 1 4
2 2 4
5 4
1 2 3 3 3
1 2 4 3
5 4
1 2 3 3 3
1 2 3 4
6 2
2 1 1 3 4 1
5 1
Sample Output
VALID
VALID
INVALID
VALID
INVALID
VALID
Sample Explanation
Explanations labelled by street number.
No exchanges are needed, as Aaron's sequence is identical to the candies given out by houses on street .
Shane can exchange the candies recieved from houses and (both of tastiness ) at house for a candy of tastiness .
At no house can Shane exchange candies of equal tastiness for the first candy in Aaron's sequence.
Shane can exchange the candies recieved from houses and (both of tastiness ) at house for a candy of tastiness .
At no house can Shane exchange candies of equal tastiness for the fourth candy in Aaron's sequence.
Shane can exchange the candies recieved from houses and (both of tastiness ) at house for a candy of tastiness , then exchange that and the candy from house (both of tastiness ) for a candy of tastiness ; Shane's list after visiting house :
3 3
. At house , Shane can exchange the candy recieved from the last exchange and the candy recieved from house (both of tastiness ) for a candy of tastiness ; Shane's list after visiting house :4 4
. Finally, at the th house, Shane can exchange that candy and the one recieved from house (both of tastiness ) for a candy of tastiness ; Shane's list after visiting house :5 1
.
Comments