A Ducci sequence is a sequence of integer n-tuples. Given an integer n-tuple (a1,a2,…,an), the next tuple in the sequence is formed from the absolute differences of adjacent entries, where the last entry an is treated as adjacent to the first entry a1.
(a1,a2,…,an)→(∣a1−a2∣,∣a2−a3∣,…,∣an−a1∣)
Every Ducci sequence eventually reaches the all-zero tuple or falls into a periodic loop. For example, the sequence starting from (8,11,2,7) reaches the all-zero tuple after 5 steps.
(8,11,2,7)→(3,9,5,1)→(6,4,4,2)→(2,0,2,4)→(2,2,2,2)→(0,0,0,0)
By contrast, the sequence starting from (4,2,0,2,0) never reaches the all-zero tuple and instead enters a periodic loop. Notice below that the tuple (0,0,0,2,2) recurs.
(4,2,0,2,0)→(2,2,2,2,4)→(0,0,0,2,2)→(0,0,2,0,2)→(0,2,2,2,2)→(2,0,0,0,2)→(2,0,0,2,0)→(2,0,2,2,2)→(2,2,0,0,0)→(0,2,0,0,2)→(2,2,0,2,2)→(0,2,2,0,0)→(2,0,2,0,0)→(2,2,2,0,2)→(0,0,2,2,0)→(0,2,0,2,0)→(2,2,2,2,0)→(0,0,0,2,2)→⋯
Given an n-tuple, write a program that determines whether the Ducci sequence starting from it reaches the all-zero tuple or falls into a periodic loop.
The first line contains the number of test cases T. Each test case consists of two lines: the first line contains the tuple size n (3≤n≤15), and the second line contains n integers separated by spaces. Each integer is between 0 and 1000 inclusive. The number of steps needed for a Ducci sequence to reach the all-zero tuple or enter a loop does not exceed 1000.
For each test case, print the answer on its own line. Print LOOP if the Ducci sequence falls into a periodic loop, or ZERO if it reaches the all-zero tuple.