In Summer Informatics School, if a student doesn't behave well, teachers make a hole in his badge. And today one of the teachers caught a group of $n$ students doing yet another trick. 

Let's assume that all these students are numbered from $1$ to $n$. The teacher came to student $a$ and put a hole in his badge. The student, however, claimed that the main culprit is some other student $p_a$.

After that, the teacher came to student $p_a$ and made a hole in his badge as well. The student in reply said that the main culprit was student $p_{p_a}$.

This process went on for a while, but, since the number of students was finite, eventually the teacher came to the student, who already had a hole in his badge.

After that, the teacher put a second hole in the student's badge and decided that he is done with this process, and went to the sauna.

You don't know the first student who was caught by the teacher. However, you know all the numbers $p_i$. Your task is to find out for every student $a$, who would be the student with two holes in the badge if the first caught student was $a$.


-----Input-----

The first line of the input contains the only integer $n$ ($1 \le n \le 1000$) — the number of the naughty students.

The second line contains $n$ integers $p_1$, ..., $p_n$ ($1 \le p_i \le n$), where $p_i$ indicates the student who was reported to the teacher by student $i$.


-----Output-----

For every student $a$ from $1$ to $n$ print which student would receive two holes in the badge, if $a$ was the first student caught by the teacher.


-----Examples-----
Input
3
2 3 2

Output
2 2 3 

Input
3
1 2 3

Output
1 2 3 



-----Note-----

The picture corresponds to the first example test case.

 $8$ 

When $a = 1$, the teacher comes to students $1$, $2$, $3$, $2$, in this order, and the student $2$ is the one who receives a second hole in his badge.

When $a = 2$, the teacher comes to students $2$, $3$, $2$, and the student $2$ gets a second hole in his badge. When $a = 3$, the teacher will visit students $3$, $2$, $3$ with student $3$ getting a second hole in his badge.

For the second example test case it's clear that no matter with whom the teacher starts, that student would be the one who gets the second hole in his badge.

Difficulty: interview

Starter code:

Solutions:
n = int(input())
p = [int(i) for i in input().split()]
for i in range(n):
    tmp = [0] * n
    j = i
    while tmp[j] != 1:
        tmp[j] = 1
        j = p[j] - 1
    print(j + 1, end = " ")


n=int(input())
l=list(map(int,input().strip().split()))
for i in range(1,n+1):
	l1=[0 for i in range(n+1)]
	l1[i]=1
	j=l[i-1]
	while(True):
		if l1[j]==0:
			l1[j]=1
			j=l[j-1]
		else:
			print (j,end=" ")
			break
print ()



n = int(input())
a = list(map(int, input().split()))
for i in range(n):
    a[i] -= 1

rec = []
for i in range(n):
    s = set()
    k = i
    while k not in s:
        s.add(k)
        k = a[k]

    rec.append(k + 1)

print(" ".join(map(str, rec)))

n = int(input())
p = input().split(" ")
for i in range(len(p)):
    p[i] = int(p[i])
ans = []
for i in range(len(p)):
    scores = [0 for _ in range(n)]
    max_score = 0
    curr = i
    while True:
        #print(scores, curr)
        scores[curr] += 1
        if scores[curr] == 2:
            ans.append(curr + 1)
            break
        curr = p[curr] - 1
s = ""
for i in range(len(ans)):
    if i != len(ans) - 1:
        s += str(ans[i]) + " "
    else:
        s += str(ans[i])
print(s)


n=int(input())
# for i in range(t):
p=list(map(int,input().strip().split()))

l=[0]*n


for i in range(1,n+1):
    l=[0]*(n+1)
    j=i
    while(l[j]!=1):
        l[j]=l[j]+1
        j=p[j-1]
    print(j,end=" ")

import sys
import io

stream_enable = 0

inpstream = """
3
2 3 2
"""

if stream_enable:
    sys.stdin = io.StringIO(inpstream)
    input()

def inpmap():
    return list(map(int, input().split()))

n = int(input())
arr = [x - 1 for x in inpmap()]
for i in range(n):
    vis = [0] * n
    p = i
    while True:
        if vis[p]:
            print(p + 1, end=' ')
            break
        vis[p] = 1
        p = arr[p]
print()


from sys import stdin
from math import *

line = stdin.readline().rstrip().split()
n = int(line[0])

numbers = list(map(int, stdin.readline().rstrip().split()))



for i in range(n):
    point = i + 1
    holed = [False] * n
    while not holed[point-1]:
        holed[point-1] = True
        point = numbers[point-1]
    print(point, end=" ")



n=int(input())
l=list(map(int,input().split()))

for i in range(n):
    p=[0]*n
    j=i
    while p[j]!=2:
        p[j]+=1
        j=l[j]-1
    print(j+1,end=' ')

n = int(input())
ls = list(map(int,input().split()))
rls =[]
for i in range(n):
  mls = [0]*n
  mls[i]+=1
  up = i
  for j in range(n):
    up = ls[up]-1
    mls[up]+=1
    if(mls[up]==2):
      rls.append(up+1)
      break
print(*rls)

n=int(input())
arr=list(map(int,input().split()))
arr.insert(0,0)
arrx=[]
for i in range(1,n+1):
    arr1=[0]*(n+1)
    arr1[i]=1
    flag=0
    k=i
    ans=-1
    while(flag==0):
        if(arr1[arr[k]]==1):
            ans=arr[k]
            flag=1
            break
        else:
            arr1[arr[k]]=1
            k=arr[k]
    arrx.append(ans)
print(*arrx)


n=int(input())
a=list(map(int,input().split()))
for i in range(n):
    z=[0]*n
    z[i]+=1
    j=i
    while z[j]!=2:
        j=a[j]-1
        z[j]+=1
    print(j+1,end=' ')
        


from math import sqrt, floor
from collections import defaultdict

n = int(input())
p = list(map(int, input().split()))
dic = defaultdict(int)

for i in range(n):
    dic[i + 1] = p[i]

ans = [0]
for i in range(1, n + 1):
    dic_ = defaultdict(int)
    dic_[i] += 1
    tmp = dic_[i]
    cur = i
    while tmp != 2:
        cur = dic[cur]
        dic_[cur] += 1
        tmp = dic_[cur]
    #     print(cur, tmp, sep=' ')
    # print()
    print(cur, end=' ')

n=int(input())
a=list(map(int,input().split()))
b=[0]*(n+1)
q=[]
for i in range(1,n+1):
    b[i]=a[i-1]
for i in range(1,n+1):
    c=[0]*(n+1)
    c[i]=1
    x=i
    while(1):
        x=b[x]
        if c[x]==1:
            q.append(x)
            break
        else:
            c[x]=1
print(*q)            
        


def main():
    n = int(input())
    a = [int(x) - 1 for x in input().split()]

    for i, s1 in enumerate(a):
        visited = {i}

        next = s1
        while next not in visited:
            visited.add(next)
            next = a[next]

        print(next + 1, end=' ')


def __starting_point():
    main()

__starting_point()

USE_STDIO = False

if not USE_STDIO:
    try: import mypc
    except: pass

def main():
    n, = list(map(int, input().split(' ')))
    p = [x-1 for x in map(int, input().split(' '))]
    ans = [-1] * n
    for i in range(n):
        cnts = [0] * n
        j = i
        while True:
            cnts[j] += 1
            if cnts[j] == 2:
                ans[i] = j + 1
                break
            j = p[j]
    print(*ans)



    pass

def __starting_point():
    main()




__starting_point()

def f(i, a):
	used = [False] * n
	while not used[i]:
		used[i] = True
		i = a[i] - 1
	return i
	

n = int(input())
a = [int(i) for i in input().split()]
for i in range(n):
	print(f(i, a) + 1, end = ' ')

n = int(input())
p = [int(x) for x in input().split()]
ans = []
for i in range(n):
    mask = [0]*n
    st = [i]
    while len(st) > 0:
        a = st.pop()
        if mask[a] == 1:
            ans.append(str(a+1))
            break
        mask[a] = 1
        st.append(p[a]-1)
print(' '.join(ans))



3

def solve(N, A):
    ans = []
    for a in range(N):
        visited = bytearray(N)
        p = a
        while True:
            if visited[p] == 1:
                ans.append(p + 1)
                break
            visited[p] = 1
            p = A[p] - 1

    return ans


def main():
    N = int(input())
    A = [int(e) for e in input().split(' ')]
    assert len(A) == N
    print(*solve(N, A))


def __starting_point():
    main()

__starting_point()

N = int(input())
S = [int(x) for x in input().split(" ")]
lookup = {}

for i, v in enumerate(S):
    lookup[i+1] = v

for i in range(1, N + 1):
    visited = {}
    student = i

    while not (student in visited):
        visited[student] = True
        student = lookup[student]

    print(student, end=" ")


n = int(input())
l = [-1]+list(map(int,input().split()))
for i in range(1,n+1):
    x = [0]*(n+1)
    x[i]+=1
    z = i
    while(1):
        x[l[z]]+=1
        if(x[l[z]]==2):
            print(l[z],end=' ')
            break
        z = l[z]

import sys
import os

def dfs(p, result, visited, start):
    q = [start]
    ret = None
    while len(q) > 0:
        s = q[len(q) - 1]
        if ret is not None:
            visited.remove(s)
            r = ret
            if r - 1 in visited:
                result[s] = s + 1
            else:
                result[s] = r
            q.pop()
            continue

        if result[s] > 0:
            ret = result[s]
            q.pop()
            continue
        if s in visited:
            ret = s + 1
            q.pop()
            continue

        next = p[s] - 1
        visited.add(s)
        q.append(next)

def solve(p):
    n = len(p)
    result = [-1] * n
    visited = set()
    for i in range(n):
        if i not in visited:
            dfs(p, result, visited, i)

    return result

def main():
    n = int(input())
    p = list(map(int, input().split()))
    print(' '.join(map(str, solve(p))))


def __starting_point():
    main()
__starting_point()

#!/usr/bin/env python3
from sys import stdin, stdout

def rint():
    return map(int, stdin.readline().split())
#lines = stdin.readlines()

n = int(input())
p = list(rint())
p = [0] + p

ans = []
for i in range(1, n+1):
    cnt = [0]*(n+1)
    a = i
    cnt[a] +=1
    while True:
        if cnt[a] == 2:
            ans.append(a)
            break
        cnt[p[a]] += 1
        a = p[a]

print(*ans)

n = int(input())
p = list(map(int, input().split()))

ans = list()
for i in range(n):
    co = [0 for i in range(n)]
    co[i] = 1

    k = i
    for j in range(n):
        k = p[k] - 1

        if co[k]:
            ans.append(k + 1)
            break

        co[k] += 1

print(' '.join(map(str, ans)))



n = int(input())
pointers = list([int(x) - 1 for x in input().split()])
results = []

for i in range(n):
    visited = [False] * n
    while not visited[i]:
        visited[i] = True
        i = pointers[i]
    results.append(str(i+1))

print(' '.join(results))



n = int(input())
inp = [int(i) - 1 for i in input().split()]
ans = []
for i in range(n):
    a = [False] * n
    ii = i
    while not a[ii]:
        a[ii] = True
        ii = inp[ii]
    ans.append(ii + 1)
print(*ans)