The only difference between easy and hard versions is constraints.

There are $n$ kids, each of them is reading a unique book. At the end of any day, the $i$-th kid will give his book to the $p_i$-th kid (in case of $i = p_i$ the kid will give his book to himself). It is guaranteed that all values of $p_i$ are distinct integers from $1$ to $n$ (i.e. $p$ is a permutation). The sequence $p$ doesn't change from day to day, it is fixed.

For example, if $n=6$ and $p=[4, 6, 1, 3, 5, 2]$ then at the end of the first day the book of the $1$-st kid will belong to the $4$-th kid, the $2$-nd kid will belong to the $6$-th kid and so on. At the end of the second day the book of the $1$-st kid will belong to the $3$-th kid, the $2$-nd kid will belong to the $2$-th kid and so on.

Your task is to determine the number of the day the book of the $i$-th child is returned back to him for the first time for every $i$ from $1$ to $n$.

Consider the following example: $p = [5, 1, 2, 4, 3]$. The book of the $1$-st kid will be passed to the following kids:  after the $1$-st day it will belong to the $5$-th kid,  after the $2$-nd day it will belong to the $3$-rd kid,  after the $3$-rd day it will belong to the $2$-nd kid,  after the $4$-th day it will belong to the $1$-st kid. 

So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.

You have to answer $q$ independent queries.


-----Input-----

The first line of the input contains one integer $q$ ($1 \le q \le 200$) — the number of queries. Then $q$ queries follow.

The first line of the query contains one integer $n$ ($1 \le n \le 200$) — the number of kids in the query. The second line of the query contains $n$ integers $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$, all $p_i$ are distinct, i.e. $p$ is a permutation), where $p_i$ is the kid which will get the book of the $i$-th kid.


-----Output-----

For each query, print the answer on it: $n$ integers $a_1, a_2, \dots, a_n$, where $a_i$ is the number of the day the book of the $i$-th child is returned back to him for the first time in this query.


-----Example-----
Input
6
5
1 2 3 4 5
3
2 3 1
6
4 6 2 1 5 3
1
1
4
3 4 1 2
5
5 1 2 4 3

Output
1 1 1 1 1 
3 3 3 
2 3 3 2 1 3 
1 
2 2 2 2 
4 4 4 1 4

Difficulty: introductory

Starter code:

Solutions:
for _ in range(int(input())):
    n = int(input())
    P = list(map(int, input().split()))
    ans = [0] * n
    for i in range(n):
        if ans[i] == 0:
            now = i
            cnt = 0
            cll = []
            while True:
                now = P[now] - 1
                cnt += 1
                cll.append(now)
                if now == i:
                    break
            for u in cll:
                ans[u] = cnt
    print(' '.join(list(map(str, ans))))

t = int(input())
for _ in range(t):
    n = int(input())
    p = list(map(int, input().split()))
    ans = [-1] * n
    for i in range(n):
        if ans[i] != -1:
            continue
        memo = {}
        tmp = i
        memo[tmp] = 1
        cnt = 1
        while True:
            tmp = p[tmp] - 1
            if tmp in memo:
                break
            else:
                memo[tmp] = 1
                cnt += 1
        for i in memo:
            ans[tmp] = cnt
    print(*ans)


#    !/usr/bin/env python3
#    encoding: UTF-8
#    Last Modified: 22/Oct/2019 08:13:15 PM


import sys


def main():
    for tc in range(int(input())):
        n = int(input())
        arr = get_array()
        ans = [1] * n
        for i in range(n):
            j = arr[i] - 1
            while j != i:
                ans[i] += 1
                j = arr[j] - 1
        print(*ans)


get_array = lambda: list(map(int, sys.stdin.readline().split()))


get_ints = lambda: list(map(int, sys.stdin.readline().split()))


input = lambda: sys.stdin.readline().strip()


def __starting_point():
    main()

__starting_point()

from math import *
t = int(input())
for y in range(t):
    n = int(input())
    a = list(map(int,input().split()))
    for i in range(n):
        j = i
        ct = 0
        while(i != a[j]-1):
            j = a[j]-1
            ct += 1
        print(ct+1,end = " ")
    print()

import math
from collections import deque, defaultdict
from sys import stdin, stdout
input = stdin.readline
# print = stdout.write
listin = lambda : list(map(int, input().split()))
mapin = lambda : map(int, input().split())
for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    for i in range(n):
        a[i]-=1
    col = [0 for i in range(n)]
    c = 1
    for i in range(n):
        if not col[i]:
            s = i
            while col[s] == 0:
                col[s] = c
                s = a[s]
            c+=1
    d = defaultdict(int)
    for i in col:
        d[i]+=1
    ans = [0 for i in range(n)]
    for i in range(n):
        ans[i] = d[col[i]]
    print(*ans)

q=int(input())
for _ in range(q):
    n=int(input())
    it=list(map(int,input().split()))
    cu=[i+1 for i in range(n)]
    ans=[10**9 for i in range(n)]
    j=1
    while 10**9 in ans:
        cc=cu[:]
        for i in range(n):
            cu[it[i]-1]=cc[i]
        for i in range(n):
            if cu[i]==i+1:
                ans[i]=min(ans[i],j)
        j+=1
    print(*ans)
            


for _ in range(int(input())):
    n = int(input())
    p = [0] + list(map(int, input().split()))
    array = []
    for i in range(1, n + 1):
        c = 1
        k = p[i]
        while k != i:
            k = p[k]
            c += 1
        array.append(c)
    print (*array)

tc = int(input())

while tc > 0:
	tc -= 1
	n = int(input())
	p = [0] + list(map(int, input().split()))

	ans = [0] * (n + 1)
	mk = [False] * (n + 1)

	for i in range(1 , n + 1):
		if not mk[i]:
			sz = 1
			curr = p[i]
			mk[i] = True
			while curr != i:
				sz += 1
				mk[curr] = True
				curr = p[curr]

			ans[i] = sz
			curr = p[i]
			while curr != i:
				ans[curr] = sz
				curr = p[curr]

	print(" ".join([str(x) for x in ans[1:]]))




t = int(input())
for _ in range(t):
    n = int(input())
    p = list(map(int, input().split()))
    ans = [-1] * n
    for i in range(n):
        if ans[i] != -1:
            continue
        memo = {}
        tmp = i
        memo[tmp] = 1
        cnt = 1
        while True:
            tmp = p[tmp] - 1
            if tmp in memo:
                break
            else:
                memo[tmp] = 1
                cnt += 1
        for j in memo:
            ans[j] = cnt
    print(*ans)


t=int(input())
while t:
    n=int(input())
    l=list(map(int,input().split()))
    for i in l:
        aa=i
        a=i
        d=0
        while 1:
            a=l[a-1]
            d+=1
            if(a==aa):
                break
        print(d,end=" ")
    print()
    t-=1

q = int(input())
for i in range(q):
    a = int(input())
    cycle = [1 for i in range(a)]

    tab = list(map(int, input().split()))

    for indice, j in enumerate(tab):
        var = indice +1

        while tab[var-1] != indice+1:
            var = tab[var-1]
            cycle[indice] += 1
    
    print(*cycle)

for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    for i in range(n):
        a[i] -= 1
    for i in range(n):
        ans = 0
        x = a[i]
        while x != i:
            ans += 1
            x = a[x]
        print(ans + 1, end=" ")
    print()


import sys
input = sys.stdin.readline

q=int(input())

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        Group[find(y)]=Group[find(x)]=min(find(y),find(x))

for testcases in range(q):
    n=int(input())
    P=list(map(int,input().split()))

    Group=[i for i in range(n)]

    for i in range(n):
        Union(i,P[i]-1)

    G=[find(i) for i in range(n)]

    count=[0]*n

    for g in G:
        count[g]+=1

    for i in range(n):
        print(count[G[i]],end=" ")
    print()



    


Q = int(input())
for q in range(Q):
	N = int(input())
	P = list(map(int, input().split()))
	out = []
	i = 1
	for p in P:
		o = 1
		while p != i:
			p = P[p-1]
			o += 1
		out.append(str(o))
		i += 1
	print(" ".join(out))

q = int(input())
for i in range(q):
    n = int(input())
    a = [int(i) for i in input().split()]
    ans = [0] * n
    for i in range(n):
        now = a[a[i] - 1]
        count = 1
        while now != a[i]:
            now = a[now - 1]
            count += 1
        ans[i] = count
    print(*ans)

def __starting_point():

    q = int(input())

    for t in range(q):
        n = int(input())
        l = [int(i) for i in input().split(" ")]

        flag = True
        ans = []

        for i in range(1, len(l) + 1):
            j = l[i - 1]
            count = 1

            while j != i:
                j = l[j-1]
                count += 1
            ans.append(count)

        for i in ans:
            print(i, end=' ')
        print()

__starting_point()

T=int(input())
while(T>0):
    n=int(input())
    p=[(int(i)-1) for i in input().split()]
    #print(p)
    ans=[]
    for i in range(0,n):
        curr=i
        count=1
        next=p[curr]
        while(next!=curr):
            next=p[next]
            count+=1
        ans.append(count)
    for i in range(0,len(ans)):
        print(ans[i],end=' ')
    print()
    T-=1

N = []
AA = []
t = int(input())
for i in range(t):
    N.append(int(input()))
    a = list(map(int,input().split()))
    for j in range(len(a)):
        a[j] -= 1
    AA.append(a)

for i in range(t):
    n = N[i]
    A = AA[i]
    ans = [0] * n
    for j in range(n):
        c = 1
        b = A[j]
        while b != j:
            b = A[b]
            c += 1
        ans[j] = c
    print(" ".join([str(i) for i in ans]))

import sys
def I():
    return sys.stdin.readline().rstrip()
q = int( I() )
for _ in range( q ):
    n = int( I() )
    c = [ -1 ] * n
    def getp( i ):
        s = []
        while c[ i ] >= 0:
            s.append( i )
            i = c[ i ]
        for j in s:
            c[ j ] = i
        return i
    p = list( map( int, I().split() ) )
    p = [ i - 1 for i in p ]
    for i, q in enumerate( p ):
        a, b = getp( i ), getp( q )
        if a != b:
            c[ a ] += c[ b ]
            c[ b ] = a
    for i in p:
        print( -c[ getp( i ) ], end = " " )
    print()


q = int(input())
for i in range(q):
    n = int(input())
    a = list(map(int, input().split()))
    b = [-1 for i in range(n)]
    c = [0 for i in range(n + 2)]
    used = [True for i in range(n)]
    for i in range(n):
        if used[i]:
            d = a[i] - 1
            cell = a[i] - 1
            b[cell] = d
            c[d] += 1
            used[cell] = False
            while cell != i:
                cell = a[cell] - 1
                c[d] += 1
                b[cell] = d
                used[cell] = False
    for i in range(n):
        print(c[b[i]], end=" ")
    print()

#n = int(input())
#a = [int(x) for x in input().split()]

q = int(input())


def solve():
  n = int(input())
  p = [int(x) for x in input().split()]
  days = [0] * n
  d = 0
  for i in range(n):
    d = 0
    j = p[i] - 1
    d += 1
    while(j != i):
      d += 1
      j = p[j] - 1
    days[i] = str(d)
  print(' '.join(days))
for _ in range(q):
  solve()

import sys,math,string,bisect
input=sys.stdin.readline
from collections import deque,defaultdict
L=lambda : list(map(int,input().split()))
Ls=lambda : list(input().split())
M=lambda : list(map(int,input().split()))
I=lambda :int(input())
n=int(input())
for _ in range(n):
    k=I()
    g=[]
    l=[0]+L()
    for i in range(1,k+1):
        s=i
        c=1
        while(l[s]!=i):
            s=l[s]
            c+=1
        g.append(c)
    print(*g)
            


import sys
input=sys.stdin.readline
q=int(input())
for _ in range(q):
    n=int(input())
    a=list(map(int,input().split()))
    ans=[]
    for i in range(n):
        x=a[i]
        c=1
        while not (i+1)==x:
            x=a[x-1]
            c+=1
        ans.append(c)
    print(*ans)