1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def check_rhymes(t, days):
    results = []
    for n, rhyme, sequence in days:
        index = 0
        rhyme_length = len(rhyme)
        found = False
        
        for start in range(n):
            temp_sequence = []
            idx = start
            while len(temp_sequence) < len(sequence):
                temp_sequence.append(idx + 1)
                idx = (idx + rhyme[idx % rhyme_length]) % n
                
            if temp_sequence == sequence:
                found = True
                break
        
        results.append("TAK" if found else "NIE")
    
    return results

t = int(input())
days = []

for _ in range(t):
    n = int(input())
    rhyme = list(map(int, input().split()))
    sequence = list(map(int, input().split()))
    days.append((n, rhyme, sequence))


results = check_rhymes(t, days)
print("\n".join(results))