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: EASY
Tags: Graph algorithms

Graph traversal

Complete search
Skill types: Complete search

Starter code:

Solutions:
import sys
import random
from math import log2, log, ceil
input = sys.stdin.readline

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a > 0:
		return gcd(b % a, a)
	return b

def primefs(n):
	if n == 1:
		return 1
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

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

def union(x, y, size, link):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().split()))
MOD = int(1000000000.0) + 7
CMOD = 998244353

def dfs(x):
	global ans
	if visited[x]:
		ans.append(x + 1)
		return
	visited[x] = True
	for i in graph[x]:
		dfs(i)
n = int(input())
a = int_array()
graph = [[] for _ in range(n)]
for i in range(n):
	graph[i].append(a[i] - 1)
ans = []
for i in range(n):
	visited = [False] * n
	dfs(i)
print(*ans)


n = int(input())
graph = list(map(int, input().split()))
for i in range(n):
	graph[i] -= 1
for i in range(n):
	cur = i
	visited = [0] * n
	visited[cur] = 1
	while visited[cur] < 2:
		cur = graph[cur]
		visited[cur] += 1
	print(cur + 1, end=' ')


def dfs(start):
	if color[start] == True:
		print(start + 1, end=' ')
		return
	else:
		color[start] = True
		dfs(mas[start] - 1)
n = int(input())
mas = [int(s) for s in input().split()]
for i in range(n):
	color = [False] * n
	dfs(i)


Input = lambda : map(int, input().split())
n = int(input())
a = list(Input())
for i in range(n):
	Tmp = {}
	Tmp[i] = 1
	t = i
	while True:
		t = a[t] - 1
		if Tmp.get(t, -1) == -1:
			Tmp[t] = 1
		else:
			print(t + 1, end=' ')
			break


n = int(input())
mas = list(map(int, input().split()))
dop = []
for i in range(n):
	dop.append(0)
for i1 in range(n):
	now = dop.copy()
	cel = i1 + 1
	while True:
		now[cel - 1] += 1
		if now[cel - 1] > 1:
			print(cel, end=' ')
			break
		cel = mas[cel - 1]


N = int(input())
lst = list(map(int, input().split(' ')))[:N]
for i in range(N):
	s = [1] * N
	while s[i]:
		s[i] = 0
		i = lst[i] - 1
	print(i + 1, end=' ')


student = int(input())
badStudents = [int(x) - 1 for x in input().split()]
answer = []
for i in range(student):
	badgeHoles = [0] * student
	badgeHoles[i] += 1
	badStudent = badStudents[i]
	while True:
		badgeHoles[badStudent] += 1
		if badgeHoles[badStudent] >= 2:
			answer.append(badStudent + 1)
			break
		badStudent = badStudents[badStudent]
print(*answer)


n = int(input())
p = list(map(int, input().split()))
for i in range(len(p)):
	used = [False] * n
	k = i
	while not used[k]:
		used[k] = True
		k = p[k] - 1
	print(k + 1, end=' ')


n = int(input())
l = list(map(int, input().split()))
ans = []
for i in range(n):
	d = dict()
	w = i
	while True:
		if w in d:
			ans.append(w + 1)
			break
		else:
			d[w] = 1
		w = l[w] - 1
print(*ans)


n = int(input())
s = list(map(int, input().split()))
output = []
for i in range(1, n + 1):
	sc = [0] * (n + 1)
	start = i
	while sc[start] != 1:
		sc[start] += 1
		start = s[start - 1]
	output.append(start)
print(' '.join([str(i) for i in output]))


n = int(input())
p = list(map(int, input().split()))
res = [0] * n
for i in range(n):
	a = [0] * n
	k = p[i]
	a[i] += 1
	while a[k - 1] != 2:
		a[k - 1] += 1
		k = p[k - 1]
	res[i] = k
print(' '.join((str(_) for _ in res)))


import sys
import collections

def get_ints():
	return list(map(int, sys.stdin.readline().split()))
n = int(input())
listt = get_ints()
for i in range(n):
	c = [0] * n
	a = i
	while c[a] != 2:
		c[a] += 1
		a = listt[a] - 1
	print(a + 1, end=' ')


n = int(input())
arr = list(map(int, input().split()))
ans = []
for i in range(n):
	seen = set()
	seen.add(i)
	curr = arr[i] - 1
	while curr not in seen:
		seen.add(curr)
		curr = arr[curr] - 1
	ans.append(curr + 1)
print(' '.join(map(str, ans)))


n = int(input())
l = list(map(int, input().split()))
d = {}
for i in range(1, n + 1):
	d[i] = l[i - 1]
for j in range(1, n + 1):
	t = [0] * n
	i = j
	while True:
		t[i - 1] = t[i - 1] + 1
		if t[i - 1] == 2:
			print(i, end=' ')
			break
		i = d[i]


input = __import__('sys').stdin.readline
n = int(input())
graph = [0] + [*map(int, input().split())]
visited = []
res = -1

def dfs(v1):
	global res
	v2 = graph[v1]
	if not visited[v2]:
		visited[v2] = True
		dfs(v2)
	else:
		res = v2
for i in range(1, n + 1):
	visited = [False] * (n + 1)
	visited[i] = True
	dfs(i)
	print(res, end=' ')


n = int(input())
l = list(map(int, input().split()))
for i in range(n):
	visted = [False] * (n + 1)
	a = i + 1
	while True:
		if visted[a]:
			break
		visted[a] = True
		a = l[a - 1]
	print(a, end=' ')


n = int(input())
P = input().split()
R = []
for i in range(n):
	P_copy = [{'next': int(i) - 1, 'holes': 0} for i in P]
	curr = int(i)
	while P_copy[curr]['holes'] < 2:
		P_copy[curr]['holes'] = P_copy[curr]['holes'] + 1
		curr = P_copy[curr]['next']
	R.append(str(curr + 1))
print(' '.join(R))


n = int(input())
commands = list(map(int, input().split()))
g = [[] for i in range(n)]
for i in range(len(commands)):
	g[i].append(commands[i] - 1)
visited = []

def dfs(start):
	global visited
	visited.append(start)
	for i in g[start]:
		if i not in visited:
			dfs(i)
		else:
			visited = []
			print(str(i + 1), end=' ')
			return 0
for i in range(n):
	dfs(i)


from collections import defaultdict

def addEdge(g, u, v):
	g[u].append(v)

def helper(v, black, gray):
	black[v] = True
	gray[v] = True
	for neigh in g[v]:
		if black[neigh] == False:
			if helper(neigh, black, gray):
				return True
		elif gray[neigh] == True:
			global ans
			ans = neigh
			return True
	gray[neigh] = False
	return False

def detectCycle(res):
	for i in range(len(res)):
		(black, gray) = ([False] * len(res), [False] * len(res))
		helper(i, black, gray)
		global ans
		res[i] = ans + 1
		ans = -1
n = int(input())
arr = list(map(int, input().split()))
g = defaultdict(list)
for i in range(len(arr)):
	addEdge(g, i, arr[i] - 1)
ans = -1
res = [False] * len(arr)
detectCycle(res)
print(' '.join([str(i) for i in res]))


n = int(input()) + 1
g = [0, *map(int, input().split())]
r = [0] * n
for i in range(1, n):
	if not r[i]:
		j = i
		while not r[j]:
			(r[j], j) = (j, g[j])
		if i != j:
			k = r[j]
			while i != j:
				(r[i], i) = (k, g[i])
print(' '.join(map(str, r[1:])))


import sys
sys.setrecursionlimit(1000000)

def check(k):
	c[k] = c[k] + 1
	if c[k] == 2:
		print(k, end=' ')
		return
	else:
		k = l[k]
		check(k)
n = int(input())
l = list(map(int, input().split()))
l = [0] + l
m = []
for i in range(1, n + 1):
	c = [0] * (n + 1)
	check(i)


n = int(input())
arr = [int(n) for n in input().split()]
l = len(arr)
res = ''
for i in range(l):
	cued = set([])
	indicator = i
	cued.add(indicator + 1)
	while True:
		if arr[indicator] in cued:
			res += str(arr[indicator]) + ' '
			break
		cued.add(arr[indicator])
		indicator = arr[indicator] - 1
print(res[:-1])


n = int(input())
l = list(map(int, input().split()))
s = ''
for x in range(n):
	y = x
	p = []
	while True:
		if y not in p:
			p.append(y)
			y = l[y] - 1
		else:
			s += str(y + 1) + ' '
			break
print(s[:-1])


def dfs(adj, s):
	stack = []
	visited = [False] * (n + 1)
	stack.append(s)
	visited[s] = True
	while len(stack) != 0:
		v = stack.pop()
		for w in adj[v]:
			if visited[w] == False:
				visited[w] = True
				stack.append(w)
			else:
				return w
n = int(input())
p = list(map(int, input().split()))
adj = [[] for _ in range(n + 1)]
for i in range(len(p)):
	adj[i + 1].append(p[i])
ans = []
for i in range(1, n + 1):
	ans.append(dfs(adj, i))
print(*ans)


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))
main()


n = int(input())
data = list(map(int, input().split()))
data.insert(0, 0)
for i in range(1, n + 1):
	j = i
	visited = [False] * (n + 1)
	while j < n + 1:
		visited[j] = True
		if visited[data[j]] == True:
			print(data[j], end=' ')
			break
		else:
			j = data[j]


n = int(input())
ss = list(map(lambda x: int(x) - 1, input().split()))
used = None
for i in range(n):
	used = [False for _ in range(n)]
	v = i
	while True:
		if used[v]:
			print(v + 1, end=' ')
			break
		used[v] = True
		v = ss[v]
print()


N = int(input())
P = list(map(int, input().split()))
NB = {i: [] for i in range(N)}
for i in range(N):
	NB[i].append(P[i] - 1)
	NB[P[i] - 1].append(i)
V = set(range(N))
CCP = []
while True:
	n = V.pop()
	new_nodes = {n}
	visited = {n}
	while len(new_nodes) > 0:
		next_node = set()
		for n in new_nodes:
			next_node |= set(NB[n])
		next_node &= V
		visited |= new_nodes
		new_nodes = next_node
		V = V - next_node
	CCP.append(list(visited))
	if len(V) == 0:
		break
CY = []
for c in CCP:
	cyc = [c[0]]
	while P[cyc[-1]] - 1 not in cyc:
		cyc.append(P[cyc[-1]] - 1)
	ind = cyc.index(P[cyc[-1]] - 1)
	CY.append(cyc[ind:])
S = list(range(N))
for i in range(len(CCP)):
	for n in CCP[i]:
		m = n
		while m not in CY[i]:
			m = P[m] - 1
		S[n] = m + 1
print(*S)


import sys

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def minput():
	return map(int, input().split())

def listinput():
	return list(map(int, input().split()))
n = iinput()
l = listinput()
l.insert(0, 999)
for i in range(1, len(l)):
	d = {}
	while True:
		d[i] = d.get(i, 0) + 1
		i = l[i]
		if d.get(i, 0) == 2:
			print(i, end=' ')
			break


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


import sys, collections

def go(cur):
	vis[cur] += 1
	if vis[cur] == 2:
		return cur
	return go(adj[cur][0])
n = int(sys.stdin.readline())
arr = list(map(int, sys.stdin.readline().split()))
adj = [[] for _ in range(n + 1)]
for i in range(1, n + 1):
	adj[i].append(arr[i - 1])
for i in range(1, n + 1):
	vis = [0] * (n + 1)
	print(go(i))


n = int(input())
lst = [[int(i) - 1, 0] for i in input().split()]
for i in range(n):
	cnt = i
	while True:
		lst[cnt][1] += 1
		if lst[cnt][1] >= 2:
			print(cnt + 1, end=' ')
			break
		cnt = lst[cnt][0]
	for j in lst:
		j[1] = 0


blamed = {}
n = int(input())
p = list(map(int, input().split()))
for i in range(len(p)):
	blamed[i + 1] = p[i]
for i in range(1, n + 1):
	hole = {i: 1}
	blame = blamed[i]
	while blame not in hole:
		hole[blame] = 1
		blame = blamed[blame]
	print(blame, end=' ')


from collections import defaultdict
from math import sqrt, factorial, gcd, log2
mod = 10 ** 9 + 7
import sys
sys.setrecursionlimit(10 ** 9)
n = int(input())
l = [0] + list(map(int, input().split()))
i = 1
ans = [0] * (n + 1)
cnt = 1
while i < len(l):
	j = i
	z = cnt
	yo = []
	boo = defaultdict(bool)
	while boo[z] == False:
		boo[z] = True
		yo.append(z)
		z = l[z]
	flag = 0
	for x in yo:
		if ans[x] == 0:
			if x != z and flag == 0:
				ans[x] = z
			else:
				flag = 1
				ans[x] = x
	i += 1
	cnt += 1
ans = ans[1:]
print(*ans)


import sys
from collections import defaultdict
mod = 1000000007

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline()

def print_array(a):
	print(' '.join(map(str, a)))

def main():
	n = int(input())
	a = get_array()
	for i in range(n):
		a[i] -= 1
	for i in range(n):
		visited = [False] * n
		x = a[i]
		visited[i] = True
		while True:
			if visited[x]:
				print(x + 1, end=' ')
				break
			else:
				visited[x] = True
				x = a[x]
main()


n = int(input().strip())
a = list(map(int, input().strip().split()))
aset = set()
r = []
for i in range(n):
	while 1:
		aset.add(i + 1)
		i = a[i] - 1
		if i + 1 in aset:
			aset = set()
			r.append(i + 1)
			break
print(' '.join(map(str, r)))


def main():
	n = int(input())
	arr = list(map(int, input().split()))
	adj = [[] for i in range(n)]
	for i in range(n):
		adj[i].append(arr[i] - 1)

	def dfs_iter(s):
		nb_holes = [0] * n
		path = list()
		stack = list()
		stack.append(s)
		while stack:
			s = stack.pop()
			nb_holes[s] += 1
			path.append(s)
			for next in adj[s]:
				if nb_holes[s] < 2:
					stack.append(next)
				else:
					return path[-1] + 1
		return path[-1] + 1
	res = [str(dfs_iter(i)) for i in range(n)]
	print(' '.join(res))
main()


import sys
import random
from math import log2, log, ceil
input = sys.stdin.readline

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a > 0:
		return gcd(b % a, a)
	return b

def primefs(n):
	if n == 1:
		return 1
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

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

def union(x, y, size, link):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]
spf_sieve()

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().split()))
n = int(input())
a = int_array()
ans = []
for i in range(n):
	a[i] -= 1
for i in range(n):
	visited = set()
	ind = i
	while ind not in visited:
		visited.add(ind)
		ind = a[ind]
	ans.append(ind + 1)
print(*ans)


n = int(input())
a = list(map(int, input().split()))
for i in range(0, n):
	s = set()
	p = i
	while p not in s:
		s.add(p)
		p = a[p] - 1
	print(p + 1)


n = int(input())
p = list(map(int, input().split()))
for i in range(1, n + 1):
	a = p.copy()
	j = i
	while 1:
		if a[j - 1] == -1:
			print(j, end=' ')
			break
		else:
			t = j
			j = a[j - 1]
			a[t - 1] = -1


n = int(input())
a = list(map(int, input().split()))
for i in range(n):
	val = [False] * n
	curr = i
	while val[curr] == False:
		val[curr] = True
		curr = a[curr] - 1
	print(curr + 1, end=' ')


students = int(input())
tattle = [int(x) - 1 for x in input().strip().split(' ')]
visited = [False] * students
for i in range(students):
	current = i
	while not visited[current]:
		visited[current] = True
		current = tattle[current]
	visited = [False] * students
	print('{}'.format(current + 1), end=' ')


n = int(input())
grf = list(map(lambda x: int(x) - 1, input().split()))
color = [0] * (n + 1)

def dfs(v):
	global color
	color[v] = 1
	nxt = grf[v]
	if color[nxt] == 1:
		return (True, nxt)
	if color[nxt] == 0:
		answer = dfs(nxt)
		if answer[0]:
			return answer
	color[v] = 2
	return (False, 0)
for v in range(n):
	color = [0] * (n + 1)
	print(dfs(v)[1] + 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()


x = int(input())
s = [int(n) - 1 for n in input().split()]
for n in range(x):
	z = [False] * x
	z[n] = True
	l = s[n]
	while 1:
		if z[l]:
			print(l + 1, end=' ')
			break
		else:
			z[l] = True
			l = s[l]


def solve():
	_ = int(input())
	a = [val - 1 for val in list(map(int, input().split()))]
	visited = [False] * len(a)
	res = []
	for start in range(len(a)):
		for i in range(len(a)):
			visited[i] = False
		cur = start
		while not visited[cur]:
			visited[cur] = True
			cur = a[cur]
		res.append(cur + 1)
	print(' '.join(map(str, res)))
solve()


n = int(input())
p = list(map(lambda x: int(x) - 1, input().split()))
for i in range(n):
	temp = [0 for j in range(n)]
	x = p[i]
	temp[i] = 1
	while temp[x] == 0:
		temp[x] += 1
		x = p[x]
	print(x + 1, end=' ')


n = int(input())
l = list(map(int, input().split()))
lf = []
for i in range(n):
	ss = {i + 1}
	blame = l[i]
	while True:
		if blame in ss:
			lf.append(blame)
			break
		else:
			ss.add(blame)
			blame = l[blame - 1]
print(*lf, sep=' ')


n = int(input())
p = tuple(map(int, input().split()))
for i in range(n):
	st = set()
	ref = i + 1
	while ref not in st:
		st.add(ref)
		ref = p[ref - 1]
	print(ref, end=' ')


n = int(input())
l = [int(x) - 1 for x in input().split()]
for i in range(n):
	v = {}
	while 1:
		if i not in v:
			v[i] = 1
			i = l[i]
		else:
			v[i] += 1
			if v[i] == 2:
				print(i + 1, end=' ')
				break
			i = l[i]


import sys
input = sys.stdin.readline

def main():

	def check(x):
		vis = [False] * n
		cur = x
		for _ in range(n + 1):
			if not vis[cur]:
				vis[cur] = True
				cur = ar[cur] - 1
			else:
				return cur
	n = int(input())
	ar = list(map(int, input().split()))
	top = [str(check(x) + 1) for x in range(n)]
	print(' '.join(top))
main()


n = int(input())
a = list(map(int, input().split()))
for i in range(n):
	s = set()
	while True:
		if i + 1 in s:
			flag = 1
			break
		s.add(i + 1)
		i = a[i] - 1
	print(i + 1, end=' ')


n = int(input())
G = [0]
G += [int(x) for x in input().split()]
vis = []
for i in range(0, 1001):
	vis.append(0)
for i in range(1, n + 1):
	for x in range(1, n + 1):
		vis[x] = 0
	vis[i] = 1
	t = i
	while vis[G[t]] == 0:
		t = G[t]
		vis[t] = 1
	print(G[t], end=' ')
print()


n = int(input())
p = list(map(int, input().split()))
for j in range(n):
	a = set()
	a.add(j + 1)
	i = p[j]
	while not i in a:
		a.add(i)
		i = p[i - 1]
	print(i, end=' ')


n = int(input())
p = [*map(int, input().split())]
r = [0] * n
for i in range(n):
	if r[i] == 0:
		a = []
		s = [1] * n
		while s[i]:
			a += [i]
			s[i] = 0
			i = p[i] - 1
		f = 0
		for j in a:
			f |= j == i
			r[j] = (i, j)[f] + 1
print(*r)


n = int(input().strip())
arr = list(map(int, input().strip().split()))
res = []
visited = [False] * (n + 1)

def dfs(node):
	global visited
	global ans
	if visited[node]:
		ans = node
		return
	visited[node] = True
	dfs(arr[node - 1])
for i in range(1, n + 1):
	dfs(i)
	res.append(ans)
	visited = [False] * (n + 1)
print(*res)


n = int(input())
p = [*map(int, input().split())]
r = []
for i in range(n):
	s = [1] * n
	while s[i]:
		s[i] = 0
		i = p[i] - 1
	r += [i + 1]
print(*r)


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())
a = list(map(int, input().split()))
ans = []
for i in range(n):
	ok = [0] * n
	k = i
	while ok[k] == 0:
		ok[k] = 1
		k = a[k] - 1
	ans.append(k + 1)
print(*ans)


def dfs(u):
	if u in vis:
		return u
	else:
		vis[u] = 1
		return dfs(adj[u])
N = int(input())
List = [0]
List += list(map(int, input().split()))
adj = {}
for i in range(1, N + 1):
	adj[i] = List[i]
for i in range(1, N + 1):
	vis = {}
	print(dfs(i), end=' ')


n = int(input())
p = [0] + [int(x) for x in input().split(' ')]
q = [0] * (n + 1)
while 0 in q[1:]:
	i = q[1:].index(0) + 1
	temp = [i]
	nextn = p[i]
	while nextn not in temp:
		if q[nextn] != 0:
			for k in temp:
				q[k] = q[nextn]
			break
		temp.append(p[temp[-1]])
		nextn = p[nextn]
	if nextn in temp:
		j = temp.index(nextn)
		for k in temp[:j]:
			q[k] = nextn
		for k in temp[j:]:
			q[k] = k
print(' '.join([str(x) for x in q[1:]]))


n = int(input())
a = [int(i) for i in input().split()]
b = ['W' for i in range(n + 1)]
for i in range(n):
	c = []
	for x in b:
		c.append(x)
	c[i + 1] = 'G'
	t = a[i]
	for j in range(n):
		if c[t] == 'G':
			ans = t
			break
		elif c[t] == 'W':
			c[t] = 'G'
			t = a[t - 1]
	print(ans, end=' ')


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)


n = int(input())
p = list(map(int, input().split()))
for i in range(n):
	p[i] -= 1
res = []
for i in range(n):
	a = [0] * n
	j = i
	while a[j] == 0:
		a[j] += 1
		j = p[j]
	res.append(j + 1)
print(*res)


n = int(input())
a = [int(x) for x in input().split()]
t = dict()
for i in range(n):
	t[i + 1] = a[i]
ans = []
for i in range(n):
	x = t[i + 1]
	s = dict()
	s[i + 1] = 1
	while True:
		if x in s:
			ans.append(x)
			break
		else:
			s[x] = 1
		x = t[x]
for i in range(n):
	print(ans[i], end=' ')


n = int(input())
s = list(map(int, input().split()))
for i in range(1, n + 1):
	p = [i]
	while True:
		if s[i - 1] not in p:
			i = s[i - 1]
			p.append(i)
		else:
			print(s[i - 1], end=' ')
			break


n = int(input())
L = list(map(int, input().split()))
L.insert(0, 0)
Q = [i for i in range(n)]
for i in range(1, n + 1):
	vis = [0 for i in range(n + 1)]
	cur = i
	while True:
		if vis[cur] != 0:
			break
		vis[cur] = 1
		cur = L[cur]
	print(cur, end=' ')


n = int(input())
arr = list(map(int, input().split()))
ans = []
graph = {}
for i in range(1, n + 1):
	graph[i] = arr[i - 1]
for i in range(1, n + 1):
	temp = [0] * (n + 1)
	cur = i
	while 2 not in temp:
		temp[cur] += 1
		cur = graph[cur]
	ans.append(temp.index(2))
print(*ans)


n = int(input())
l = list(map(int, input().split()))
l.insert(0, 0)
r = []
for i in range(1, n + 1):
	s = set([i])
	j = l[i]
	while True:
		if j in s:
			r.append(str(j))
			break
		else:
			s.add(j)
			j = l[j]
print(' '.join(r))


n = int(input())
V = [int(i) for i in input().split()]
W = [0] * n
for i in range(0, n):
	j = i
	while W[j] == 0:
		W[j] = 1
		j = V[j] - 1
	print(j + 1)
	W = [0] * n


n = int(input())
a = []
D = {}
a = list(map(int, input().strip().split()))
for i in range(n):
	a[i] -= 1
for i in range(n):
	D.clear()
	D[i] = 1
	t = i
	for j in range(n):
		t = a[t]
		if D.get(t):
			print(t + 1)
			break
		D[t] = 1


n = int(input())
arr = list(map(int, input().split()))
arr3 = []
for k in range(1, n + 1):
	arr2 = []
	z = k
	while k:
		if k in arr2:
			arr3.append(k)
			break
		else:
			arr2.append(k)
			k = arr[k - 1]
print(*arr3)


def solve(arr, n):
	ans = ''
	for i in range(1, n + 1):
		holes = [0] * (n + 1)
		holes[i] = 1
		j = i
		while holes[j] < 2:
			j = arr[j]
			holes[j] += 1
		ans += str(j) + ' '
	print(ans)

def main():
	n = int(input())
	arr = list(map(int, input().split()))
	arr2 = [0]
	arr2.extend(arr)
	solve(arr2, n)
main()


n = int(input())
lst = list(map(int, input().strip().split()))[:n]
for i in range(n):
	s = [1] * n
	while s[i]:
		s[i] = 0
		i = lst[i] - 1
	print(i + 1, end=' ')


n = int(input())
arr = list(map(int, input().split()))
for i in range(n):
	markings = [0 for _ in range(n)]
	curr = i
	while markings[curr] != 1:
		markings[curr] = 1
		curr = arr[curr] - 1
	print(curr + 1, end=' ')


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()


n = int(input())
A = list(map(int, input().split()))
an = []
for (i, q) in enumerate(A):
	s = set([i])
	q -= 1
	while q not in s:
		s.add(q)
		q = A[q] - 1
	print(q + 1, end=' ')


def pilha_som(x):
	if count[x] == 1:
		return str(x)
	count[x] = 1
	ite = pilha_som(arr_valores[x])
	count[x] = 0
	return str(ite)
n = int(input())
arr = input().split()
arr_valores = [0] * 10000
for i in range(len(arr)):
	arr_valores[i + 1] = int(arr[i])
count = [0] * 10000
ans = ''
for i in range(1, n):
	ans += pilha_som(i) + ' '
ans += pilha_som(n)
print(ans)


n = int(input())
p = list(map(int, input().split()))
ans = []
for i in range(n):
	d = {}
	for k in range(n):
		d[k] = 0
	d[i] = 1
	r = i
	for j in range(n):
		d[p[r] - 1] += 1
		if d[p[r] - 1] == 2:
			ans.append(p[r])
			break
		r = p[r] - 1
print(*ans)


n = int(input())
dict = {}
k = 1
for i in input().split():
	dict[k] = int(i)
	k += 1
for i in range(n):
	hash = {}
	for j in range(n):
		hash[j + 1] = 0
	k = i + 1
	while 1:
		if hash[k]:
			print(k, end=' ')
			break
		else:
			hash[k] = 1
			k = dict[k]


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [tuple(map(int, l.split())) for l in sys.stdin]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def main():
	n = I()
	p = LI_()
	r = []
	for i in range(n):
		v = [None] * n
		k = i
		while v[k] is None:
			v[k] = 1
			k = p[k]
		r.append(k + 1)
	return ' '.join(map(str, r))
print(main())


n = int(input())
A = [*map(int, input().split())]
B = []

def dfs(j):
	visited = set([j])
	while True:
		nxt = A[j] - 1
		if nxt in visited:
			return nxt
		visited.add(nxt)
		j = nxt
for i in range(len(A)):
	B.append(dfs(i) + 1)
print(' '.join(map(str, B)))


n = int(input())
p = list(map(lambda x: int(x) - 1, input().split()))
a = [0 for _ in range(n)]
for i in range(n):
	c = [0 for _ in range(n)]
	s = i
	while c[s] == 0:
		c[s] = 1
		s = p[s]
	a[i] = s
print(*list(map(lambda x: x + 1, a)))


n = int(input())
word = list(map(int, input().split()))
ans = ''
for i in range(n):
	visited = [0 for _ in range(n)]
	curr = i
	while True:
		visited[curr] += 1
		if visited[curr] == 2:
			ans += str(curr + 1) + ' '
			break
		curr = word[curr] - 1
print(ans)


n = int(input())
l = list(map(int, input().split()))
ans = []
d = {}
for i in range(1, n + 1):
	d = {}
	while d.get(i, 0) == 0:
		d[i] = d.get(i, 0) + 1
		i = l[i - 1]
	ans.append(str(i))
print(' '.join(ans))


n = int(input())
a = list(map(int, input().split()))
b = [0] * (n + 1)
for j in range(1, n + 1):
	b[j] = a[j - 1]
for k in range(1, n + 1):
	u = [0] * 1001
	u[k] = 1
	if k == b[k]:
		print(k, end=' ')
		continue
	z = k
	while u[b[z]] != 1:
		u[b[z]] = 1
		z = b[z]
	print(b[z], end=' ')


import sys
sys.setrecursionlimit(10000)
n = int(input())
pupils = list(map(int, input().split()))
for i in range(n):
	pupils[i] -= 1
visited = [False] * n

def dfs(v):
	if visited[v]:
		return v
	visited[v] = True
	return dfs(pupils[v])
for i in range(n):
	visited = [False] * n
	print(dfs(i) + 1, end=' ')


n = int(input())
l = list(map(int, input().split()))
a = [0 for i in range(n)]
b = []
c = 0
d = 0
for i in range(n):
	if a[i] == 0:
		c = i
		while (c in b) == False and a[c] == 0:
			b.append(c)
			c = l[c] - 1
		if (c in b) == True:
			k = b.index(c)
			for r in b[k:]:
				a[r] = r + 1
			for s in b[:k]:
				a[s] = c + 1
		else:
			for t in b:
				a[t] = a[c]
		b = []
print(' '.join((str(x) for x in a)))


from sys import stdin, stdout
cin = stdin.readline
cout = stdout.write

def bfs(i):
	visited = [0] * (n + 1)
	visited[i] = 1
	while True:
		if visited[p[i - 1]]:
			return p[i - 1]
		visited[p[i - 1]] = 1
		i = p[i - 1]
n = int(cin())
p = list(map(int, cin().split()))
for i in range(1, n + 1):
	cout(str(bfs(i)) + ' ')


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


n = int(input())
lista = list(map(int, input().split()))
lista.insert(0, 0)
saida = []
for i in range(1, n + 1):
	s = set()
	p = i
	while True:
		if p in s:
			saida.append(str(p))
			break
		else:
			s.add(p)
			p = lista[p]
print(' '.join(saida))


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)


from sys import stdin, stdout
import bisect
import math
from collections import deque
mod = 10 ** 9 + 7

def st():
	return list(stdin.readline().strip())

def inp():
	return int(stdin.readline())

def li():
	return list(map(int, stdin.readline().split()))

def mp():
	return map(int, stdin.readline().split())

def pr(n):
	stdout.write(str(n) + '\n')

def DFS(d, i, visited):
	visited[i] = True
	stack = [i]
	while stack:
		a = stack.pop()
		for i in d[a]:
			if visited[i]:
				x.append(i)
				break
			else:
				visited[i] = True
				stack.append(i)
	pr(x)

def soe(limit):
	l = [1] * (limit + 1)
	l[0] = 0
	l[1] = 0
	prime = []
	for i in range(2, limit + 1):
		if l[i]:
			for j in range(i * i, limit + 1, i):
				l[j] = 0
	for i in range(2, limit + 1):
		if l[i]:
			prime.append(i)
	return prime

def segsoe(low, high):
	limit = int(high ** 0.5) + 1
	prime = soe(limit)
	n = high - low + 1
	l = [0] * (n + 1)
	for i in range(len(prime)):
		lowlimit = low // prime[i] * prime[i]
		if lowlimit < low:
			lowlimit += prime[i]
		if lowlimit == prime[i]:
			lowlimit += prime[i]
		for j in range(lowlimit, high + 1, prime[i]):
			l[j - low] = 1
	for i in range(low, high + 1):
		if not l[i - low]:
			if i != 1:
				print(i)

def gcd(a, b):
	while b:
		a = a % b
		(b, a) = (a, b)
	return a

def power(a, n):
	r = 1
	while n:
		if n & 1:
			r = r * a
		a *= a
		n = n >> 1
	return r
x = []

def solve():
	n = inp()
	l = li()
	l = [0] + l
	for i in range(1, n + 1):
		visited = [False for i in range(n + 1)]
		visited[i] = True
		j = l[i]
		while not visited[j]:
			visited[j] = True
			j = l[j]
		x.append(j)
	print(*x)
for _ in range(1):
	solve()


import collections
import math
import sys
from functools import reduce
import decimal
sys.setrecursionlimit(10 ** 9)
decimal.getcontext().rounding = decimal.ROUND_HALF_UP
graphDict = collections.defaultdict

class Graphs:

	def __init__(self):
		self.graph = graphDict(list)

	def add_edge(self, u, v):
		self.graph[u].append(v)

	def dfs_utility(self, nodes, visited_nodes):
		visited_nodes.add(nodes)
		for neighbour in self.graph[nodes]:
			if neighbour not in visited_nodes:
				self.dfs_utility(neighbour, visited_nodes)
			else:
				return neighbour

	def dfs(self, node):
		visited = set()
		self.dfs_utility(node, visited)

def rounding(n):
	return int(decimal.Decimal(f'{n}').to_integral_value())

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def inp():
	return sys.stdin.readline().strip()

def map_inp(v_type):
	return map(v_type, inp().split())

def list_inp(v_type):
	return list(map_inp(v_type))
n = int(inp())
arr = list_inp(int)
ans = []
for i in range(1, n + 1):
	visited = {i: 1}
	temp = arr[i - 1]
	while temp not in visited:
		visited[temp] = 1
		temp = arr[temp - 1]
	else:
		ans.append(temp)
print(*ans)


n = int(input())
p = list(map(lambda x: int(x) - 1, input().split()))
for i in range(n):
	visited = [False] * n
	x = i
	while not visited[x]:
		visited[x] = True
		x = p[x]
	print(x + 1, end=' ')


a = int(input())
li = list(map(int, input().split()))
li2 = []
for i in range(a):
	li1 = [1] * (a + 1)
	while li1[i]:
		li1[i] = 0
		i = li[i] - 1
	li2.append(i + 1)
print(*li2)


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


n = int(input())
ls = list(map(int, input().split()))
ans = []

def dfs(i):
	lst = []
	while 1:
		if i in lst:
			x = i
			break
		lst.append(i)
		i = ls[i - 1]
	return x
for i in range(1, n + 1):
	ans.append(dfs(i))
print(*ans)


n = int(input())
p = list(map(lambda x: int(x) - 1, input().split()))

def solve(i):
	v = [False] * n
	while not v[i]:
		v[i] = True
		i = p[i]
	return i + 1
print(*map(solve, range(n)))


n = int(input())
p = [int(s) for s in input().split()]
visited = [-2] * n
rez = [None] * n
for i in range(n):
	if visited[i] == -2:
		start = i
		while visited[start] == -2:
			visited[start] += 1
			start = p[start] - 1
		br = start
		while visited[start] == -1:
			visited[start] = start + 1
			start = p[start] - 1
		start = i
		while start != br:
			visited[start] = visited[br]
			start = p[start] - 1
print(*visited)


n = int(input())
p = list(map(int, input().split()))
for i in range(n):
	punished = [0 for k in range(n)]
	j = i
	while punished[j] < 2:
		punished[j] += 1
		j = p[j] - 1
	print(j + 1, end=' ')


from collections import defaultdict as dc
n = int(input())
a = [int(i) for i in input().split()]
for i in range(0, n):
	z = i
	b = dc(int)
	b[i + 1] = 1
	while 1:
		t = a[z]
		z = a[z] - 1
		b[t] += 1
		if b[t] == 2:
			print(t, end=' ')
			break


n = int(input())
m = tuple(map(int, input().split()))
for i in range(n):
	state = [1] * n
	while state[i]:
		state[i] = 0
		i = m[i] - 1
	print(i + 1)


def readNums(type=int):
	return list(map(type, input().split()))
n = readNums()[0]
arr = readNums()
ans = []
for j in range(n):
	st = set()
	i = j
	while i not in st:
		st.add(i)
		i = arr[i] - 1
	ans.append(i + 1)
for x in ans:
	print(x, end=' ')


n = int(input())
peoples = list(map(int, input().split()))
for i in range(n):
	visited = [0 * x for x in range(n)]
	visited[i] = 1
	cur = peoples[i] - 1
	while visited[cur] != 2:
		p = cur
		visited[cur] += 1
		cur = peoples[p] - 1
	print(cur + 1)


from collections import defaultdict as d
n = int(input())
l = list(map(int, input().split()))
adj = d(list)
used = [False] * (n + 1)
ans = -1

def dfs(src):
	global used, ans
	used[src] = True
	if src in adj:
		for i in adj[src]:
			if not used[i]:
				dfs(i)
			else:
				ans = i
ansl = []
for i in range(n):
	adj[i + 1].append(l[i])
for i in range(n):
	ans = i + 1
	used = [False] * (n + 1)
	dfs(i + 1)
	ansl.append(ans)
for i in ansl:
	print(i, end=' ')
print()


n = int(input())
p = [0] + list(map(int, input().split()))
ans = []
for i in range(1, n + 1):
	d = {}
	d[i] = 1
	last = i
	while True:
		ask = p[last]
		if d.get(ask, 0) == 1:
			ans.append(ask)
			break
		d[ask] = 1
		last = ask
print(*ans)


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


n = int(input())
A = []
S = []
A = list(map(int, input().split()))
for a in range(1, n + 1):
	times = [0] * (n + 1)
	p = a
	while times[p] == 0:
		times[p] = 1
		p = A[p - 1]
	S.append(p)
for i in S:
	print(i)


n = int(input())
a = input().split()
s = [0 for i in range(n + 1)]
for i in range(n):
	s[i + 1] = int(a[i])
for i in range(1, n + 1):
	t = i
	M = {}
	M.clear()
	M[t] = 1
	while True:
		t = s[t]
		if t in M:
			M[t] = M[t] + 1
		else:
			M[t] = 1
		if M[t] == 2:
			print(t, end=' ')
			break


n = int(input())
ar = list(map(int, input().split()))
out = []
for i in range(n):
	holes = []
	ele = i + 1
	while ele not in holes:
		holes.append(ele)
		ele = ar[ele - 1]
	out.append(str(ele))
print(' '.join(out))


n = int(input())
p = [int(i) for i in input().split()]
p.insert(0, 0)
visited = [False] * (n + 1)
mas_vix = [0]

def DFS(node):
	global visited
	global mas_vix
	if visited[node] == True:
		for i in range(len(visited)):
			visited[i] = False
		mas_vix.append(node)
		return
	visited[node] = True
	DFS(p[node])
for i in range(1, len(p)):
	DFS(i)
mas_vix.remove(0)
print(*mas_vix)


from sys import stdin, stdout

def main():
	n = int(input())
	p = list(map(int, input().split()))
	for i in range(n):
		holes = [0] * n
		j = i
		while holes[j] < 2:
			holes[j] += 1
			j = p[j] - 1
		print(j + 1, end=' ')
main()


n = int(input())
p = list(map(int, input().split()))
out = []
for i in range(n):
	hit = [False] * n
	cur = i
	while not hit[cur]:
		hit[cur] = True
		cur = p[cur] - 1
	out.append(cur + 1)
print(*out)


n = int(input())
G = [int(x) for x in input().split()]
G.insert(0, 0)
SecondBadge = [0] * (n + 1)
for u in range(1, n + 1):
	visited = [False] * (n + 1)
	visited[u] = True
	v = G[u]
	while not visited[v]:
		visited[v] = True
		v = G[v]
	SecondBadge[u] = v
for i in range(1, n + 1):
	print(SecondBadge[i], end=' ')


n = int(input())
arr = [int(x) for x in input().split()]
arr.insert(0, 0)
for i in range(1, n + 1):
	a = i
	visited = [0 for x in range(1001)]
	while True:
		if visited[a] == 1:
			break
		visited[a] = 1
		a = arr[a]
	print(a)


n = int(input())
ps = [int(x) for x in input().split()]
for a in range(1, n + 1):
	temp = dict()
	p = a
	while True:
		temp[p - 1] = temp.get(p - 1, 0) + 1
		if temp[p - 1] == 2:
			print(p, end=' ')
			break
		else:
			p = ps[p - 1]


n = int(input())
p = [int(_) - 1 for _ in input().split()]
for x in range(n):
	q = set()
	z = x
	while not z in q:
		q.add(z)
		z = p[z]
	print(z + 1, end=' ')


n = int(input())
p = list(map(int, input().split()))
for i in range(n):
	used = [0] * n
	s = i
	used[i] = 1
	for j in range(10000):
		s = p[s] - 1
		if used[s] == 1:
			print(s + 1, end=' ')
			break
		else:
			used[s] = 1


from collections import defaultdict
Fix = dict()
N = int(input())
List = [int(x) for x in input().split()]
for i in range(len(List)):
	Fix[i + 1] = List[i]
Res = []
for i in range(len(List)):
	Dangle = defaultdict(int)
	Parser = List[i]
	Dangle[i + 1] = 1
	while Dangle[Parser] != 1:
		Dangle[Parser] = 1
		Parser = Fix[Parser]
	Res.append(Parser)
print(*Res)


from collections import defaultdict

class graph:

	def __init__(self, v):
		self.v = v
		self.graph = defaultdict(list)

	def insert(self, a, b):
		self.graph[a].append(b)

	def iscycle(self):
		l = []
		for i in range(self.v):
			visit = [0] * self.v
			recstack = [0] * self.v
			x = self.cycle(i, visit, recstack)
			l.append(x + 1)
		return l

	def cycle(self, s, visit, recstack):
		visit[s] = 1
		recstack[s] = 1
		for x in self.graph[s]:
			if visit[x] == 0:
				y = self.cycle(x, visit, recstack)
				if y != -1:
					return y
			elif recstack[x] == 1:
				return x
		recstack[s] = 0
		return -1
n = int(input())
g = graph(n)
a = list(map(int, input().split()))
for i in range(n):
	(x, y) = (i, a[i] - 1)
	g.insert(x, y)
print(*g.iscycle())


def main():
	n = int(input())
	data = list(map(int, input().split()))
	sub = [0] * n
	for i in range(n):
		temp = sub.copy()
		x = i
		while not temp[x]:
			temp[x] += 1
			x = data[x] - 1
		print(x + 1, end=' ')
main()


import sys
sys.setrecursionlimit(10000)

def foo(i):
	if holes[i - 1] is not None:
		print(i + 1, end=' ')
		return None
	holes[i - 1] = 1
	foo(arr[i] - 1)
n = int(input())
arr = list(map(int, input().split()))
holes = [None] * n
for i in range(n):
	foo(i)
	holes = [None] * n


from math import *
from collections import *
from itertools import *
from bisect import *
from operator import *
import sys
sys.setrecursionlimit(1000000)

def io():
	return map(int, input().split())

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

def o():
	return int(input())

def kl(con, x=0):
	if x == 0:
		print('Yes') if con else print('No')
	elif x == 1:
		print('yes') if con else print('no')
	elif x == 2:
		print('YES') if con else print('NO')
MOD = 1000000007
MAX = 2147483647
MIN = -2147483648
p = input
n = o()
l = op()
c = 1
for i in l:
	f = [0] * (n + 1)
	f[c] = 1
	x = i
	while f[x] == 0:
		f[x] = 1
		x = l[x - 1]
	print(x, end=' ')
	c += 1


n = int(input())
s = input()
s = [int(x) for x in s.split(' ')]
dic = {}
for i in range(1, n + 1):
	dic[i] = s[i - 1]
for i in dic:
	visited = [0] * (len(dic) + 1)
	j = i
	visited[j] = 1
	while visited[j] < 2:
		visited[dic[j]] = visited[dic[j]] + 1
		j = dic[j]
	for i in range(len(visited)):
		if visited[i] > 1:
			print(i, end=' ')


n = int(input())
a = list(map(int, input().split()))
ans = []
for i in range(n):
	count = [0] * n
	count[i] = 1
	tmp = i
	count[a[i] - 1] += 1
	while True:
		tmp = a[tmp] - 1
		tmp_val = a[tmp] - 1
		count[tmp_val] += 1
		if count[tmp_val] > 1:
			ans.append(tmp_val + 1)
			break
print(*ans)


def hole_students():
	total = int(input())
	students = input().split()
	output = ''
	for i in range(len(students)):
		dic = {}
		dic[i + 1] = 1
		temp = int(students[i])
		while True:
			nex = int(students[temp - 1])
			if temp not in dic:
				dic[temp] = 1
				temp = nex
			else:
				output += str(temp) + ' '
				break
	print(output)
hole_students()


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


from sys import stdin, stdout

def rint():
	return map(int, stdin.readline().split())
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())
A = [int(i) for i in input().split()]
used = [0] * n
for i in range(0, n):
	j = i + 1
	while True:
		if used[j - 1] == 1:
			print(j)
			break
		else:
			used[j - 1] = 1
			j = A[j - 1]
	used = [0] * n


n = int(input())
next = list(map(int, input().split()))
for i in range(n):
	alr = [i + 1]
	l = next[i]
	while True:
		if l in alr:
			print(l, end=' ')
			break
		alr.append(l)
		l = next[l - 1]


n = int(input())
A = [int(i) for i in input().split(' ')]
for start in range(n):
	done = [0] * n
	tmp = start
	while done[tmp] != 2:
		done[tmp] += 1
		tmp = A[tmp] - 1
	print(tmp + 1, end=' ')


n = input()
arr = list(map(int, input().split(' ')))
for i in range(len(arr)):
	buf = []
	cur_pos = i
	while not cur_pos in buf:
		buf.append(cur_pos)
		cur_pos = arr[cur_pos] - 1
	print(cur_pos + 1, end=' ')


def func(i):
	global p, n
	fl = [0] * n
	while not fl[i]:
		fl[i] = 1
		i = p[i] - 1
	return i + 1
n = int(input())
p = list(map(int, input().split()))
print(*[func(i) for i in range(n)])


n = int(input())
a = list(map(lambda x: int(x) - 1, input().split()))
b = [-1] * n
for i in range(n):
	if b[i] != -1:
		continue
	if b[a[i]] != -1:
		b[i] = b[a[i]]
		continue
	s = set()
	x = i
	while x not in s:
		s.add(x)
		x = a[x]
	b[x] = x
	y = a[x]
	while y != x:
		b[y] = y
		y = a[y]
	y = i
	while y != x:
		b[y] = x
		y = a[y]
print(*map(1 .__add__, b))


n = int(input())
cir = [int(x) for x in input().split()]
for i in range(1, n + 1):
	vis = [0] * (n + 1)
	t = i
	while vis[t] == 0:
		vis[t] = 1
		t = cir[t - 1]
	print(t, end=' ')


import sys
sys.setrecursionlimit(1000000)
n = int(input())
s = list(map(int, input().split()))

def dfs(n):
	if visited[n]:
		return n
	visited[n] = True
	return dfs(s[n - 1])
for i in range(1, n + 1):
	visited = [False for j in range(n + 2)]
	print(dfs(i), end=' ')


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

def prev(a):
	bl = [False] * n
	cur = a
	bl[cur - 1] = True
	cur = p[cur - 1]
	while not bl[cur - 1]:
		bl[cur - 1] = True
		cur = p[cur - 1]
	return cur
for a in range(1, n + 1):
	print(prev(a), end=' ')


n = int(input())
P = list(map(int, input().split()))
P = [0] + P
res = []
for i in range(1, n + 1):
	visit = [0 for i in range(n + 2)]
	j = i
	while visit[j] < 2:
		visit[j] += 1
		j = P[j]
	res.append(j)
print(*res)


n = int(input())
p = list(map(int, input().split()))
for i in range(n):
	isp = [i]
	h = p[i] - 1
	while h not in isp:
		isp.append(h)
		h = p[h] - 1
	print(h + 1, end=' ')


def run_bfs(n, start, p, on_cycle, res):
	visited = set()
	nxt = start
	while nxt not in visited and (not on_cycle[nxt]):
		visited.add(nxt)
		nxt = p[nxt]
	cycler = nxt
	while not on_cycle[cycler]:
		on_cycle[cycler] = True
		res[cycler] = cycler
		cycler = p[cycler]
	while start != nxt:
		res[start] = nxt
		start = p[start]
n = int(input())
p = [int(i) - 1 for i in input().split()]
on_cycle = [False for _ in range(n)]
res = [None for _ in range(n)]
for i in range(n):
	if res[i] is None:
		run_bfs(n, i, p, on_cycle, res)
print(' '.join((str(i + 1) for i in res)))


import sys
import math
get_string = lambda : sys.stdin.readline().strip()
get_list = lambda : list(map(int, sys.stdin.readline().strip().split()))
get_int = lambda : int(sys.stdin.readline())
n = get_int()
d = {}
lst = [0] + get_list()
res = [0] * n
for i in range(n):
	temp = set()
	curr = i + 1
	while True:
		temp.add(curr)
		curr = lst[curr]
		if curr in temp:
			break
	res[i] = curr
print(*res)


import sys
import math
import string
N = int(sys.stdin.readline())
a = [int(i) for i in sys.stdin.readline().split()]
for i in range(1, N + 1):
	connections = [0 for x in range(N + 1)]
	ptr = i
	while True:
		connections[ptr] += 1
		if connections[ptr] == 2:
			print(ptr, end=' ')
			break
		ptr = a[ptr - 1]


n = int(input())
a = [int(i) for i in input().split()]
ans = []
for i in range(n):
	visited = [0] * n
	current = i
	while visited[i] != 1:
		visited[i] = 1
		i = a[i] - 1
	ans.append(i + 1)
for i in ans:
	print(i, ' ', end='')


n = int(input())
l = list(map(int, input().split()))
for num in range(1, n + 1):
	s = set()
	s.add(num)
	while True:
		if l[num - 1] in s:
			print(l[num - 1], end=' ')
			break
		else:
			s.add(l[num - 1])
			num = l[num - 1]


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


def dfs(cv):
	if cv in marked:
		return cv
	marked.add(cv)
	return dfs(a[cv])
n = int(input())
a = list(map(lambda x: int(x) - 1, input().split()))
for i in range(n):
	marked = set()
	print(dfs(i) + 1, end=' ')


n = int(input())
l = list(map(int, input().split()))
z = {i: 0 for i in range(1, n + 1)}
ans = [0] * (n + 1)
for i in range(n):
	d = dict(z)
	t = l[i]
	d[i + 1] += 1
	for j in range(n + 1):
		d[t] += 1
		f = 1
		if d[t] >= 2:
			ans[i + 1] = t
			f = 0
			break
		t = l[t - 1]
print(*ans[1:])


import sys
from math import sqrt, floor, factorial
from collections import deque, Counter
inp = sys.stdin.readline
read = lambda : list(map(int, inp().strip().split()))

def is_prime(n):
	for i in range(2, int(sqrt(n)) + 1):
		if not n % i:
			return False
	return True

def solve():
	n = int(inp())
	arr = [0] + read()
	for i in range(1, n + 1):
		sett = set()
		sett.add(i)
		j = i
		while not arr[j] in sett:
			j = arr[j]
			sett.add(j)
		print(arr[j], end=' ')
solve()


n = int(input())
p = [0] + list(map(int, input().split()))
ans = []
for i in range(1, n + 1):
	visited = {i}
	next = i
	while True:
		next = p[next]
		if next in visited:
			ans.append(next)
			break
		visited.add(next)
print(*ans)


n = int(input())
p = list(map(int, input().split()))
ans = p[:]
for i in range(n):
	tmp = p[:]
	num = i
	while tmp[num]:
		tmp[num] = 0
		num = p[num] - 1
	ans[i] = num + 1
for i in ans:
	print(i, end=' ')


number = int(input())
mas = [0] + [False for c in range(1, number + 1)]
napravlenie = [0] + input().split(' ')
it = 1
while it <= number:
	napravlenie[it] = int(napravlenie[it])
	it += 1
it = 1
while it < number + 1:
	if napravlenie[it] == it:
		print(it, end=' ')
		it += 1
		continue
	mas[it] = True
	ab = it
	while True:
		ab = napravlenie[ab]
		if mas[ab] == True:
			print(ab, end=' ')
			irr = 1
			while irr < number + 1:
				mas[irr] = False
				irr += 1
			break
		else:
			mas[ab] = True
	it += 1


n = int(input())
p = list(map(int, input().split()))
p = [i - 1 for i in p]
for i in range(n):
	(a, st) = (i, i)
	vis = [0] * n
	while True:
		a = p[a]
		if vis[a] or a == st:
			print(a + 1)
			break
		vis[a] = 1


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()


n = int(input())
pupils = [int(i) for i in input().split()]
answers = []
for i in range(1, n + 1):
	used = {i}
	pupil = pupils[i - 1]
	while True:
		if pupil in used:
			break
		used.add(pupil)
		pupil = pupils[pupil - 1]
	answers.append(pupil)
print(*answers)


from queue import Queue as sq
buff = sq()
loop_el = set()

def process_loop():
	global buff, loop_el
	while nodes:
		k = nodes.pop()
		buff.put_nowait(k)
		visited[k - 1] = True
		while True:
			k = arr[k - 1]
			if visited[k - 1]:
				buff.put_nowait(k)
				break
			buff.put_nowait(k)
			visited[k - 1] = True
			nodes.remove(k)
		g = buff.get_nowait()
		while g != k:
			par[g - 1] = par[k - 1]
			g = buff.get_nowait()
		while not buff.empty():
			loop_el.add(buff.get_nowait())
N = int(input())
nodes = set(range(1, N + 1))
arr = [int(x) for x in input().strip().split(' ')]
visited = [False] * N
par = list(range(1, N + 1))
process_loop()
[print(i, end=' ') for i in par]


n = int(input())
l = list(map(int, input().split()))
for node in range(n):
	ma = [1] * n
	ma[node] = 0
	ne = l[node] - 1
	while ma[ne]:
		ma[ne] = 0
		ne = l[ne] - 1
	print(ne + 1, end=' ')


from sys import stdin, stdout
n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
for i in range(n):
	vis = set()
	start = a[i] - 1
	vis.add(i)
	while True:
		if start in vis:
			stdout.write('{} '.format(start + 1))
			break
		vis.add(start)
		start = a[start] - 1
stdout.write('\n')


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


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))))
main()


def main():
	N = int(input())
	P = [0] + list(map(int, input().split()))
	ans = []
	for i in range(1, N + 1):
		if P[i] == i:
			ans.append(i)
		else:
			d = {i: 1}
			j = i
			while True:
				if P[j] not in d:
					d[P[j]] = 1
				else:
					ans.append(P[j])
					break
				j = P[j]
	print(*ans)
main()


n = map(int, input().split())
niz = list(map(int, input().split()))
res = []
for i in range(len(niz)):
	c = len(niz) * [0]
	x = i
	while True:
		if c[x] == 1:
			res.append(x + 1)
			break
		else:
			c[x] = 1
		x = niz[x] - 1
print(' '.join(map(str, res)))


n = int(input())
l = [int(j) for j in input().split()]
z = [0] * (n + 1)
for i in range(n):
	z = [0] * (n + 1)
	q = [i]
	while q:
		x = q.pop()
		if z[x] == 1:
			print(x + 1, end=' ')
		else:
			q.append(l[x] - 1)
			z[x] = 1


n = int(input())
p = []
for x in input().split():
	p.append(int(x) - 1)
for x in range(n):
	visited = set()
	visited.add(x)
	i = x
	while True:
		nxt = p[i]
		if nxt in visited:
			print(nxt + 1, end='')
			break
		visited.add(nxt)
		i = nxt
	if x != n - 1:
		print(' ', end='')


n = int(input())
p = [int(x) for x in input().split()]
used_ = [False] * n
res = [0] * n
for i in range(n):
	used = used_.copy()
	v = i
	while not used[v]:
		used[v] = True
		v = p[v] - 1
	res[i] = v + 1
print(*res)


n = int(input())
arr = input().split()
for i in range(len(arr)):
	arr[i] = int(arr[i])
for i in range(1, n + 1):
	student = i
	visited = {0}
	while True:
		if student in visited:
			print(student, end=' ')
			break
		else:
			visited.add(student)
		student = arr[student - 1]


students = int(input())
l2 = input().split(' ')
arr = []
for a in l2:
	arr.append(int(a))
ans = []
for i in range(len(arr)):
	temp = []
	temp.append(i)
	j = i
	while True:
		ptr = arr[j] - 1
		if ptr in temp:
			ans.append(ptr + 1)
			break
		else:
			temp.append(ptr)
			j = ptr
for s in ans:
	print(s, end=' ')


n = int(input())
p = list(map(int, input().split()))
p = [i - 1 for i in p]
tmp = []
final = []
counter = 0
j = 0
for i in range(n):
	tmp = [True] * n
	j = i
	while tmp[j]:
		tmp[j] = False
		j = p[j]
	final.append(j + 1)
print(*final)


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


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=' ')


n_students = int(input())
array = input().split()
array = [int(x) for x in array]
answer = [0] * (n_students + 1)

def accusation(accused, array, badge, answer):
	if answer[accused] != 0:
		return (answer[accused], False)
	badge[accused] += 1
	if badge[accused] == 2:
		answer[accused] = accused
		return (accused, True)
	(cycle_at, cycle) = accusation(array[accused - 1], array, badge, answer)
	if accused == cycle_at:
		return (cycle_at, False)
	if not cycle:
		answer[accused] = cycle_at
		return (cycle_at, False)
	else:
		answer[accused] = accused
		return (cycle_at, True)
for student in range(1, n_students + 1):
	badge = [0] * (n_students + 1)
	accusation(student, array, badge, answer)
answer = answer[1:n_students + 1]
answer = [str(x) for x in answer]
answer = ' '.join(answer)
print(answer)


n = [int(x) for x in input().split()][0]
mem = [int(x) for x in input().split()]
ans = ''
for start in range(1, n + 1):
	stud = [1] * n
	curr = start
	while stud[curr - 1] != 0:
		stud[curr - 1] = stud[curr - 1] - 1
		curr = mem[curr - 1]
	print(curr)


n = int(input())
arr = [*map(int, input().split())]
ans = []
for i in range(n):
	mark = [1] * n
	while mark[i]:
		mark[i] = 0
		i = arr[i] - 1
	ans.append(i + 1)
print(*ans)


import os
import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from io import BytesIO, IOBase
from itertools import accumulate, combinations, combinations_with_replacement, groupby, permutations, product
from math import acos, asin, atan, ceil, cos, degrees, factorial, hypot, log2, pi, radians, sin, sqrt, tan
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits

def inp():
	return int(input())

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

def instr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())

def dfs(a, source):
	stk = []
	visited = [False] * (n + 1)
	stk.append(source)
	while len(stk) > 0:
		u = stk.pop()
		if visited[u] == False:
			visited[u] = True
		for v in a[u]:
			if visited[v] == False:
				stk.append(v)
			else:
				return v
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
n = inp()
a = defaultdict(lambda : [])
b = inlist()
for i in range(n):
	a[i + 1].append(b[i])
for i in range(1, n + 1):
	res = dfs(a, i)
	print(res, end=' ')


def find(p, index):
	found = {}
	while index not in found:
		found[index] = True
		index = p[index]
	return index

def main():
	n = int(input())
	p = [0] + list(map(int, input().split()))
	results = [str(find(p, i)) for i in range(1, n + 1)]
	print(' '.join(results))
main()


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))


ans = []
n = int(input())
pi = list(map(lambda x: [int(x) - 1, True], input().split()))
for i in range(n):
	p = [i, True]
	k = i
	while p[1]:
		p[1] = False
		k = p[0]
		p = pi[p[0]]
	ans.append(k + 1)
	for j in pi:
		j[1] = True
print(*ans)


import array as arr
num = int(input())
arr_list = list(map(int, input().split()))
result = []
for index in range(num):
	counter_list = [0] * (num + 1)
	curr_index = index
	while True:
		counter_list[curr_index + 1] += 1
		if counter_list[curr_index + 1] == 2:
			result.append(curr_index + 1)
			break
		curr_index = arr_list[curr_index] - 1
print(' '.join((str(x) for x in result)))


n = int(input())
nums = list(map(int, input().split()))
for i in range(n):
	nums[i] -= 1
output = list()
for i in range(n):
	counter = [0 for i in range(n)]
	counter[i] = 1
	double = False
	current_student = nums[i]
	while not double:
		if counter[current_student] != 1:
			counter[current_student] += 1
		else:
			output.append(current_student)
			double = True
		current_student = nums[current_student]
for x in output:
	print(x + 1, end=' ')


from sys import setrecursionlimit
f = lambda : map(int, input().split())
n = int(input())
p = [0] + list(f())
setrecursionlimit(100000)

def dfs(start):
	if not visited[start]:
		visited[start] = True
		dfs(p[start])
	else:
		print(start, end=' ')
		return
for i in range(1, 1 + n):
	visited = [False] * (n + 1)
	dfs(i)


n = input().split()[0]
n = int(n)
a = input().split()
refer = {}
for i in range(0, n):
	refer[i + 1] = int(a[i])
out = []
for i in range(1, n + 1):
	find = {}
	ref = refer[i]
	find[i] = 1
	f = 0
	while 1:
		if ref not in find:
			find[ref] = 1
			ref = refer[ref]
		else:
			f = ref
			break
	out.append(str(f))
res = ' '
print(res.join(out))


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


from sys import stdin, stdout
from collections import deque
import bisect
R = lambda : stdin.readline().strip()
RL = lambda f=None: list(map(f, R().split(' '))) if f else list(R().split(' '))
output = lambda x: stdout.write(str(x) + '\n')
output_list = lambda x: output(' '.join(map(str, x)))
n = int(R())
l = RL(int)
ans = []
for i in range(n):
	visited = [0] * n
	p = i
	while visited[p] == 0:
		visited[p] = 1
		p = l[p] - 1
	ans.append(p + 1)
output_list(ans)


def func(adj, s_node):
	v_ary = [False] * len(adj)
	i = s_node
	while v_ary[i] == False:
		v_ary[i] = True
		i = adj[i]
	return i

def answer(n, adj):
	ary = [0] * (n + 1)
	for i in range(1, n + 1, 1):
		ary[i] = func(adj, i)
	ary.pop(0)
	return ary

def main():
	n = int(input())
	adj = [int(i) for i in input().split()]
	adj.insert(0, 0)
	print(' '.join(map(str, answer(n, adj))))
main()


n = int(input())
p = list(map(int, input().split()))
ans = []
for i in range(n):
	holes = set([i])
	last_student = i
	while True:
		next_student = p[last_student] - 1
		if next_student in holes:
			break
		holes.add(next_student)
		last_student = next_student
	ans.append(next_student)
print(' '.join((str(i + 1) for i in ans)))


def do():
	n = int(input())
	next = [int(i) - 1 for i in input().split(' ')]
	res = [0] * n

	def dfs(node):
		stack = [node]
		seen = [0] * n
		seen[node] = 1
		while stack:
			cur = stack.pop()
			nei = next[cur]
			if seen[nei] == 1:
				return nei
			seen[nei] = 1
			stack.append(nei)
		return 0
	for i in range(n):
		res[i] = dfs(i) + 1
	print(' '.join(map(str, res)))
	return 0
do()


import sys
from collections import deque
input = sys.stdin.readline

def bfs(root):
	q = deque()
	q.append(root)
	path = []
	while len(q) != 0:
		e = q.popleft()
		path.append(e)
		vis[e] = 1
		if not vis[c[e]]:
			q.append(c[e])
		else:
			return (path, c[e])
n = int(input())
a = list(map(int, input().split()))
c = [-1] * n
for i in range(n):
	c[i] = a[i] - 1
vis = [0] * n
ans = {}
for i in range(n):
	if not vis[i]:
		(p, e) = bfs(i)
		if e in ans:
			for j in p:
				ans[j] = ans[e]
			continue
		flag = 1
		for j in p:
			if flag:
				ans[j] = e + 1
			else:
				ans[j] = j + 1
			if j == e:
				flag = 0
for i in range(n):
	print(ans[i], end=' ')
print()


n = int(input())
p = input().split()
for i in range(0, n):
	temp = i + 1
	d = dict()
	done = False
	while done == False:
		d[temp] = d.get(temp, 0) + 1
		if d[temp] == 2:
			print(str(temp) + ' ', end='')
			done = True
		temp = int(p[temp - 1])


n = int(input())
arr = [int(x) for x in input().split()]
for i in range(n):
	arr[i] -= 1
for i in range(n):
	pos = i
	stat = [False for x in range(n)]
	while stat[pos] != True:
		stat[pos] = True
		pos = arr[pos]
	print(pos + 1, end=' ')


n = int(input())
p = list(map(int, input().split()))
a = [[0 for i in range(n)] for j in range(n)]
ans = []
for i in range(n):
	bc = i
	while 1:
		a[i][bc] += 1
		if a[i][bc] == 2:
			ans.append(bc + 1)
			break
		bc = p[bc] - 1
print(*ans)


n = int(input())
P = [0] + list(map(int, input().split()))
ANSLIST = [0] * (n + 1)

def search(n):
	loop = [n]
	while True:
		if P[n] in loop:
			return (loop, P[n])
		else:
			loop.append(P[n])
			n = P[n]
for i in range(1, n + 1):
	if ANSLIST[i] != 0:
		continue
	else:
		(X, r) = search(i)
		for j in X:
			if j != r:
				ANSLIST[j] = r
			else:
				ANSLIST[j] = r
				break
		for j in reversed(X):
			if j != r:
				ANSLIST[j] = j
			else:
				break
for i in range(1, n + 1):
	print(ANSLIST[i], end=' ')


import sys
sys.setrecursionlimit(10000)

def DFS(p, visited, i):
	if visited[i]:
		return i
	visited[i] = True
	return DFS(p, visited, p[i])

def solve(p):
	res = [0] * len(p)
	for i in range(len(p)):
		visited = [False] * len(p)
		res[i] = DFS(p, visited, i)
	return ' '.join(map(lambda x: str(x + 1), res))
input()
p = list(map(lambda x: int(x) - 1, input().split()))
print(solve(p))


n = int(input())
lis = [int(x) for x in input().split()]
for i in range(n):
	used = [0] * n
	k = i
	while used[k] == 0:
		used[k] = 1
		k = lis[k] - 1
	print(k + 1, end=' ')


n = int(input())
m = [int(s) for s in input().split()]
for a in range(n):
	m_was = [0 for s in range(len(m))]
	start_loop = 0
	end_loop = 0
	cur = a
	while m_was[cur] == 0:
		m_was[cur] = 1
		cur = m[cur] - 1
	print(cur + 1, end=' ')


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


n = int(input())
students = [-1] + [int(x) for x in input().split()]
for student in range(1, len(students)):
	seen = {student}
	current = students[student]
	while current not in seen:
		seen.add(current)
		current = students[current]
	print(f'{current} ', end='')
print()


def run_test(n, lst):
	blamed = list()
	for i in range(len(lst)):
		blamed.append(0)
	while True:
		if blamed[n] == 1:
			return n
		blamed[n] += 1
		n = lst[n] - 1
n = int(input())
lst = input().split()
results = ''
for i in range(len(lst)):
	lst[i] = int(lst[i])
for i in range(n):
	results += str(run_test(i, lst) + 1) + ' '
print(results[:-1])


import sys

def dfs(now, graf, ans):
	if now in ans:
		ans[now] += 1
	else:
		ans[now] = 1
	if ans[now] == 2:
		return now + 1
	else:
		return dfs(graf[now], graf, ans)
sys.setrecursionlimit(10000)
n = int(input())
a = list(map(int, input().split()))
graf = {}
for i in range(n):
	graf[i] = a[i] - 1
for i in range(n):
	ans = {}
	print(dfs(i, graf, ans), end=' ')


n = int(input())
s = input().split()
A = list(map(int, s))
t = True
for i in range(n):
	B = [0] * n
	j = i
	while t:
		B[j] = B[j] + 1
		if B[j] == 2:
			break
		j = A[j] - 1
	print(j + 1, end=' ')


n = int(input())
arr_p = [int(p) for p in input().split()]
for i in range(0, n):
	h = [0] * n
	j = i
	while h[j] < 2:
		h[j] += 1
		j = arr_p[j] - 1
	print(j + 1)


n = int(input())
A = [1] + list(map(int, input().split()))
for i in range(1, n + 1):
	B = [0] * (n + 1)
	j = i
	while j <= n:
		B[j] += 1
		j = A[j]
		if B[j] == 2:
			print(j)
			break


z = [0 for i in range(1001)]
ans = []
n = int(input())
lst = list(map(int, input().split()))
q = 0
for i in range(n):
	z[i + 1] += 1
	while q == 0:
		z[lst[i]] += 1
		i = lst[i] - 1
		if 2 in z:
			q = 1
			ans.append(z.index(2))
	q = 0
	z = [0 for i in range(1001)]
print(*ans)


n = int(input())
ask = list(map(int, input().split()))
path = [0 for _ in range(n + 1)]
count = [0 for _ in range(n + 1)]
ans = []
for i in range(n):
	path[i + 1] = ask[i]
for i in range(1, n + 1):
	temp = count[:]
	temp[i] += 1
	idex = i
	while 1:
		if temp[idex] < 2:
			next = path[idex]
			temp[next] += 1
			idex = next
		else:
			ans.append(idex)
			break
print(*ans)


n = int(input())
p = list(map(int, input().split()))
graph = {}
for i in range(n):
	graph[i + 1] = p[i]
ans = []
for i in range(n):
	check = set()
	a = i + 1
	while not a in check:
		check.add(a)
		a = graph[a]
	ans.append(a)
print(*ans)


def main():
	n = int(input())
	culprit = list(map(int, input().split()))
	holes = []
	for i in range(n):
		isVisited = {}
		curr = i + 1
		while curr not in isVisited.keys():
			isVisited[curr] = 1
			curr = culprit[curr - 1]
		holes.append(curr)
	for hole in holes:
		print(hole, end=' ')
main()


n = int(input())
p = [0]
p.extend([int(x) for x in input().split()])
ans = []
for i in range(1, n + 1):
	mark = [0] * (n + 1)
	mark[i] += 1
	j = i
	while True:
		j = p[j]
		mark[j] += 1
		if mark[j] == 2:
			ans.append(j)
			break
print(*ans)


from sys import setrecursionlimit
setrecursionlimit(10 ** 9)

def dfs(m, p, holes):
	holes[p[m]] += 1
	if holes[p[m]] == 1:
		dfs(p[m], p, holes)
n = int(input())
p = [0] + [int(i) for i in input().split()]
holes = [0] + [0 for i in range(n)]
for i in range(1, n + 1):
	holes = [0 for i in range(n + 1)]
	holes[i] += 1
	dfs(i, p, holes)
	print(holes.index(2), end=' ')


n = int(input())
p = [0] + list(map(int, input().split()))
d = [-1] * (n + 1)

def solve(u):
	i = u
	while d[i] != u:
		d[i] = u
		i = p[i]
	return i
print(*(solve(i) for i in range(1, n + 1)))


n = int(input())
arr = list(map(int, input().split()))
graph = {i: set() for i in range(1, n + 1)}
for i in range(n):
	graph[i + 1] = graph[i + 1].union(set([arr[i]]))
ans = []
visited = set()

def dfs(graph, visited, node):
	if node not in visited:
		visited.add(node)
		for neighbors in graph[node]:
			dfs(graph, visited, neighbors)
	else:
		ans.append(node)
		return node
for i in range(n):
	visited.clear()
	dfs(graph, visited, i + 1)
print(*ans)


n = int(input())
x = list(map(int, input().split(' ')))
j = 0
b = []
for i in range(n):
	j = i + 1
	p = []
	while j not in p:
		p.append(j)
		j = x[j - 1]
	b.append(j)
print(*b)


N = int(input())
a = [int(i) for i in input().split()]

def f(i):
	b = [False] * N
	while 1:
		if not b[i]:
			b[i] = True
			i = a[i] - 1
		else:
			return i + 1
			break
for i in range(N):
	print(f(i), end=' ')


def dec(x):
	return int(x) - 1
n = int(input())
a = list(map(dec, input().split()))
for d in range(n):
	s = set()
	t = a[d]
	s.add(d)
	while t not in s:
		s.add(t)
		t = a[t]
	print(t + 1, end=' ')
print()


n = int(input())
p = list(map(lambda x: int(x) - 1, input().split()))

def sub_solve(n, p, a):
	pokes = [0] * n
	while pokes[a] < 1:
		pokes[a] += 1
		a = p[a]
	return a

def solve(n, p):
	res = []
	for a in range(n):
		res.append(str(sub_solve(n, p, a) + 1))
	return ' '.join(res)
print(solve(n, p))


n = int(input())
tab = list(map(int, input().split()))
graph = [[] for _ in range(n + 1)]
for i in range(n):
	graph[i + 1].append(tab[i])
wynik = []
odw = [False for _ in range(n + 1)]

def DFS(v):
	odw[v] = True
	for w in graph[v]:
		if not odw[w]:
			DFS(w)
		else:
			wynik.append(w)
for i in range(1, n + 1):
	DFS(i)
	for j in range(n + 1):
		odw[j] = False
print(*wynik)


import sys
import math
import itertools
import collections

def getdict(n):
	d = {}
	if type(n) is list or type(n) is str:
		for i in n:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
	else:
		for i in range(n):
			t = ii()
			if t in d:
				d[t] += 1
			else:
				d[t] = 1
	return d

def divs(n, start=1):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def cdiv(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

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

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def wr(arr):
	return ' '.join(map(str, arr))

def revn(n):
	return int(str(n)[::-1])

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base=3):
	newnumber = ''
	while number > 0:
		newnumber = str(number % base) + newnumber
		number //= base
	return newnumber
n = ii()
p = li()
ans = []
for i in range(n):
	st = p[i]
	s = set([i + 1])
	while True:
		if st in s:
			ans.append(st)
			break
		else:
			s.add(st)
			st = p[st - 1]
print(wr(ans))


n = int(input())
L = list(map(int, input().split()))
d = {}
for a in range(1, n + 1):
	d[a] = L[a - 1]
for a in list(d.keys()):
	s = set([])
	t = a
	while d[t] not in s:
		s.add(t)
		t = d[t]
	print(d[t], end=' ')


n = int(input())
a = [*map(int, input().split())]
from itertools import repeat
for i in range(n):
	t = i
	b = list(repeat(0, n))
	while 1:
		b[t] += 1
		if b[t] == 2:
			ans = t + 1
			break
		t = a[t] - 1
	print(ans, end=' ')


n = int(input())
l1 = list(map(int, input().split()))
for i in range(n):
	l = []
	for z in range(n):
		l.append(z + 1)
	t = True
	l[i] = 0
	x = l1[i]
	while t:
		if l[x - 1] == 0:
			print(x)
			break
		else:
			l[x - 1] = 0
			x = l1[x - 1]


from collections import defaultdict

def dfs(arg):
	visit[arg] += 1
	while visit[arg] != 2:
		arg = g[arg]
		visit[arg] += 1
	return arg
n = int(input())
g = defaultdict(int)
p = list(map(int, input().split()))
for i in range(1, n + 1):
	g[i] = p[i - 1]
for i in range(1, n + 1):
	visit = [0] * (n + 1)
	print(dfs(i), end=' ')


V = int(input())
next = list(map(int, input().split()))
next = [node - 1 for node in next]

def DFS(node, seen):
	seen[node] += 1
	if seen[node] == 2:
		return node
	return DFS(next[node], seen)
for node in range(V):
	seen = [0] * V
	print(DFS(node, seen) + 1, end=' ')


n = int(input())
u = list(map(int, input().split()))
u += u
for i in range(n):
	j = i + 1
	s = set()
	while j not in s:
		s.add(j)
		j = u[j - 1]
	print(j, end=' ')


z = []
for i in range(1001):
	z.append(0)
n = int(input())
x = list(map(int, input().split()))
q = True
c = []
for i in range(len(x)):
	z[i + 1] += 1
	while q:
		z[x[i]] += 1
		i = x[i] - 1
		if 2 in z:
			q = False
			c.append(z.index(2))
	q = True
	for k in range(1001):
		z[k] = 0
print(*c, sep=' ')


n = int(input())
a = list(map(int, input().split()))
for i in range(len(a)):
	l = [False for x in range(len(a))]
	point = i
	while l[point] != True:
		l[point] = True
		point = a[point] - 1
	print(point + 1)


n = int(input())
mas = list(map(int, input().split()))
otv = []
for i in range(n):
	gas = [0] * n
	k = i
	while gas[k] == 0:
		gas[k] = 1
		k = mas[k] - 1
	otv.append(str(k + 1))
print(' '.join(otv))


n = int(input())
n = n + 1
arr = [0] + list(map(int, input().split()))
out = []
for i in range(1, n):
	vis = [False] * n
	if arr[i] == i:
		out.append(i)
	else:
		j = i
		while vis[j] != True:
			vis[j] = True
			j = arr[j]
		out.append(j)
print(*out)


hm = int(input())
a = list(map(int, input().split()))
for i in range(hm):
	c = [0] * hm
	t = i + 1
	while True:
		c[t - 1] += 1
		if c[t - 1] == 2:
			print(t, end=' ')
			break
		t = a[t - 1]


n = int(input())
p = list(map(int, input().split()))
count = 0
a = [0] * n
for i in range(n):
	c = []
	k = p[i] - 1
	c.append(i)
	c.append(k)
	while 1:
		k = p[k] - 1
		if k in c:
			print(k + 1, end=' ')
			break
		else:
			c.append(k)


n = int(input())
l1 = [int(x) for x in input().split()]
ans = []
for x in range(len(l1)):
	temp = []
	targ = x + 1
	while targ not in temp:
		temp.append(targ)
		targ = l1[targ - 1]
	ans.append(targ)
print(*ans)


n = int(input())
a = list(map(int, input().split()))
from collections import defaultdict

class g:

	def __init__(self, v):
		self.e = defaultdict(list)
		self.v = v

	def ins(self, u, v):
		self.e[u].append(v)

	def dfs(self, root):
		q = [root]
		vi = [0] * (self.v + 1)
		while len(q):
			p = q.pop()
			vi[p] = 1
			for i in self.e[p]:
				if vi[i] == 0:
					q.append(i)
				else:
					return i
g = g(n)
for i in range(1, n + 1):
	g.ins(i, a[i - 1])
for i in range(1, n + 1):
	print(g.dfs(i), end=' ')


n = int(input())
blamed = list(map(int, input().split()))
answers = [None] * n
for i in range(0, n):
	visited = [0] * n
	current = i
	while True:
		if visited[current] > 0:
			answers[i] = current + 1
			break
		visited[current] += 1
		current = blamed[current] - 1
print(' '.join([str(x) for x in answers]))


def sol():
	am = int(input())
	arr = list(map(int, input().split()))
	for i in range(am):
		was = [False] * am
		ind = i
		while was[ind] == False:
			was[ind] = True
			ind = arr[ind] - 1
		print(ind + 1, end=' ')
sol()


def solve():
	n = int(input())
	students = {i: int(x) for (i, x) in enumerate(input().split(), 1)}
	res = []
	for i in range(1, n + 1):
		s = i
		visited = set()
		visited.add(s)
		while True:
			s = students[s]
			if s in visited:
				res.append(s)
				break
			visited.add(s)
	print(*res)
solve()


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


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)))


def dfs(i, students):
	visited = [False] * n
	while not visited[i]:
		visited[i] = True
		i = students[i]
	return i + 1
n = int(input())
students = [int(x) - 1 for x in input().split()]
result = [dfs(i, students) for i in range(n)]
print(*result)


n = int(input())
p = [int(x) for x in input().split()]
d = dict()
for i in range(n):
	k = i
	while True:
		if k in d:
			print(k + 1, end=' ')
			d.clear()
			break
		else:
			d[k] = 0
		k = p[k] - 1


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

def answer(a, p):
	done = set()
	current = a
	while current not in done:
		done.add(current)
		current = p[current - 1]
	return current
ans = []
for i in range(1, n + 1):
	ans.append(answer(i, p))
print(*ans)


n = int(input())
p = [0] + list(map(int, input().split()))
ans = ''
for i in range(1, n + 1):
	(a, x) = ([0] * (n + 1), i)
	while a[x] == 0:
		a[x] = 1
		x = p[x]
	ans += str(x) + ' '
print(ans)


n = int(input())
p = [int(x) - 1 for x in input().split()]
visited = [False for _ in range(n)]
res = [-1 for _ in range(n)]

def visit(i):
	while not visited[i]:
		visited[i] = True
		i = p[i]
	return i
for i in range(n):
	for j in range(n):
		visited[j] = False
	res[i] = visit(i) + 1
print(' '.join(map(str, res)))


n = int(input())
s = [int(i) - 1 for i in input().split()]
for i in range(n):
	p = [i]
	ss = s[i]
	while not ss in p:
		p.append(ss)
		ss = s[ss]
	print(ss + 1, end=' ')


def main():
	n = int(input())
	ip = [int(item) for item in input().split(' ')]
	op = ''
	for i in range(len(ip)):
		op += findCulprit(i, ip, n) + ' '
	print(op)

def findCulprit(a, ip, n):
	boolVisited = [0] * n
	i = a
	try:
		while boolVisited[i] != 1:
			boolVisited[i] = 1
			i = ip[i] - 1
	except:
		print(boolVisited)
		print(i)
		print(a)
	return str(i + 1)
main()


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())
a = list(map(int, input().split()))
ans = ''
for i in range(n):
	u = []
	for m in range(n):
		u.append(0)
	j = i
	while u[j] < 1:
		u[j] += 1
		j = a[j] - 1
	ans += str(j + 1) + ' '
print(ans)


n = int(input())
students = list(map(int, input().split()))
for i in range(0, n):
	a = students.copy()
	currentIndex = i
	while True:
		next = a[currentIndex]
		a[currentIndex] = -1
		if next == -1:
			print(currentIndex + 1, end=' ')
			break
		currentIndex = next - 1


global main_list, ans, visited
length = int(input())
main_list = list(map(int, input().split()))
ans = []
visited = [False] * length
import sys
sys.setrecursionlimit(1000000000)

def dfs(n):
	global main_list, ans, visited
	if not visited[n]:
		visited[n] = True
	else:
		ans.append(n + 1)
		return
	dfs(main_list[n] - 1)
for i in range(length):
	dfs(i)
	visited = [False] * length
print(*ans, sep=' ')


n = int(input())
p = list(map(int, input().split()))
sch = set()
for a in range(n):
	b = a
	while True:
		if b in sch:
			break
		sch.add(b)
		b = p[b] - 1
	print(str(b + 1), end=' ')
	sch = set()


n = int(input())
arr = list(map(int, input().split()))
arr.insert(0, 0)
visited = [False for i in range(len(arr))]
visited[0] = True
res = [-1 for i in range(len(arr))]

def travel(p, arr, res):
	start = p
	while not visited[p]:
		visited[p] = True
		p = arr[p]
	pp = p
	while res[pp] < 0:
		res[pp] = pp
		pp = arr[pp]
	if p != start:
		while res[start] < 0:
			res[start] = res[p]
			start = arr[start]
for i in range(1, len(arr)):
	if visited[i]:
		continue
	travel(i, arr, res)
print(' '.join(map(str, res[1:])))


n = int(input())
y = list(map(int, input().split(' ')))
count = []
for i in range(n):
	visited = [False] * n
	while not visited[i]:
		visited[i] = True
		i = y[i] - 1
	count.append(i + 1)
print(*count)


n = int(input())
reportlist = [int(key) for key in input().split()]
hole = [0] * n
for i in range(0, n):
	if reportlist[i] == i + 1:
		print(i + 1)
	else:
		k = i
		hole = [0] * n
		while True:
			if hole[k] > 1:
				break
			hole[k] += 1
			k = reportlist[k] - 1
		print(k + 1)


n = int(input())
num = tuple(map(int, input().split()))
s = ''
for i in range(n):
	spis = [i]
	t = -1
	while True:
		t += 1
		if num[spis[t]] - 1 not in spis:
			spis.append(num[spis[t]] - 1)
		else:
			s += str(num[spis[t]]) + ' '
			break
print(s)


(n, P) = (int(input()), [None] + list(map(int, input().split())))
for i in range(1, n + 1):
	(a, A) = (i, {i})
	while True:
		a = P[a]
		if a in A:
			print(a, end=' ')
			break
		A.add(a)


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())
p = list(map(int, input().split()))
d = {}
for i in range(n):
	d[i + 1] = p[i]
for next in range(1, n + 1):
	visit = [0] * (n + 1)
	while True:
		visit[next] += 1
		if visit[next] == 2:
			print(next, end=' ')
			break
		next = d[next]
print()


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


n = int(input())
p = input().split(' ')
p = map(int, p)
p = list(p)
p = [0] + p
for i in range(1, n + 1):
	count = [0] * len(p)
	count[i] = 1
	next = i
	while count[next] < 2:
		next = p[next]
		count[next] += 1
	print(next, end=' ')


n = int(input())
p = list(map(int, input().strip().split()))
res = []
for i in range(n):
	d = {}
	cur = i + 1
	while cur not in d:
		d[cur] = 1
		cur = p[cur - 1]
	res.append(str(cur))
print(' '.join(res))


import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n):
	v = i + 1
	put = []
	while v not in put:
		put.append(v)
		v = a[v - 1]
	b.append(v)
print(*b)


import sys
sys.setrecursionlimit(10 ** 6)
lines = []
for Line in sys.stdin:
	lines.append(Line.rstrip('\n'))
n = int(lines[0])
del lines[0]
l = list(map(int, lines[0].split()))

def dfs(start, nexti):
	global visited
	if nexti in visited:
		return nexti
	visited += [nexti]
	return dfs(nexti, l[nexti] - 1)
t = []
for i in range(n):
	visited = [False] * (n + 1)
	visited[i] = True
	j = l[i] - 1
	while visited[j] == False:
		visited[j] = True
		j = l[j] - 1
	t += [j + 1]
print(*t)


def f(root):
	global dfspath, vis, ans
	while vis[root] == 0:
		vis[root] = 1
		dfspath += [root]
		root = child[root]
	for i in range(len(dfspath)):
		ans[dfspath[i]] = root
	try:
		for i in range(dfspath.index(root), len(dfspath)):
			ans[dfspath[i]] = dfspath[i]
	except:
		for i in dfspath:
			ans[i] = ans[root]
	dfspath = []
dfspath = []
n = int(input())
child = [-1] + list(map(int, input().split()))
vis = [0] * (n + 1)
ans = [0] * (n + 1)
for root in range(1, n + 1):
	if vis[root] == 0:
		f(root)
print(*ans[1:])


def reset_dict(d):
	for k in d:
		d[k] = 0
n = int(input().rstrip())
cells = list(map(int, input().rstrip().split()))
d = {}
for i in range(1, n + 1):
	d[i] = 0
i = 1
for i in range(1, n + 1):
	st = i
	while True:
		if d[cells[st - 1]] == 1:
			print(cells[st - 1], end=' ')
			reset_dict(d)
			break
		else:
			d[st] = 1
			st = cells[st - 1]


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


(n, a, l) = (int(input()), [*map(int, input().split())], [])
for i in range(n):
	p = [1] * n
	while p[i] != 0:
		p[i] -= 1
		i = a[i] - 1
	l += [i + 1]
print(*l)


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]


def solve(n, a):
	for i in range(1, n + 1):
		mark = [0] * (n + 1)
		t = i
		while mark[t] == 0:
			mark[t] = 1
			t = a[t - 1]
		print(t, end=' ')
n = int(input())
a = list(map(int, input().split()))
solve(n, a)


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())
pp = list(map(int, input().split()))
p = {i + 1: pp[i] for i in range(n)}
c = {i + 1: 0 for i in range(n)}
for i in range(1, n + 1):
	y = i
	c[y] += 1
	while True:
		if c[y] == 1:
			y = p[y]
			c[y] += 1
			continue
		else:
			print(y, end=' ')
			c = {i + 1: 0 for i in range(n)}
			break


n = int(input())
posylki = [int(num) for num in input().split()]

def DFS(vertex):
	vertex = vertex - 1
	if used[vertex] != 1:
		used[vertex] = 1
		DFS(posylki[vertex])
	else:
		print(vertex + 1, end=' ')
		return
for i in range(len(posylki)):
	used = [0 for i in posylki]
	DFS(i + 1)


from sys import stdin
from collections import defaultdict
n = int(input().split()[0])
p = [int(x) - 1 for x in input().split()]
res = list()
for i in range(n):
	vis = set()
	vis.add(i)
	cur = i
	while True:
		cur = p[cur]
		if cur in vis:
			res.append(str(cur + 1))
			break
		else:
			vis.add(cur)
print(' '.join(res))


x = int(input())
numbers = list(map(int, input().split()))
ans = []
for i in range(x):
	nums = [False] * x
	j = i
	while nums[j] == 0:
		nums[j] = True
		j = numbers[j] - 1
	ans.append(j + 1)
print(*ans)


n = int(input())
p_arr = [int(s) - 1 for s in input().split()]
for student in range(len(p_arr)):
	cset = set()
	nstudent = student
	answer = None
	while True:
		if nstudent in cset:
			answer = nstudent
			break
		cset.add(nstudent)
		nstudent = p_arr[nstudent]
	print(nstudent + 1, end=' ')
print('')


n = int(input())
arr = list(map(int, input().split()))
ans = []
for i in range(1, n + 1):
	memo = {i: arr[i - 1]}
	current = i
	while True:
		try:
			pointed = memo[current]
			if memo[pointed]:
				ans.append(pointed)
				break
		except KeyError:
			memo[memo[current]] = arr[memo[current] - 1]
			current = memo[current]
for a in ans:
	print(a, end=' ')


n = int(input())
a = [int(x) for x in input().split()]
for i in range(0, n):
	k = [0] * n
	j = i
	while k[j] < 2:
		k[j] += 1
		j = a[j] - 1
	print(j + 1)


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


import functools
import time
from collections import Counter

def timer(func):

	@functools.wraps(func)
	def wrapper(*args, **kwargs):
		stime = time.perf_counter()
		res = func(*args, **kwargs)
		elapsed = time.perf_counter() - stime
		print(f'{func.__name__} in {elapsed:.4f} secs')
		return res
	return wrapper

class solver:

	def __init__(self):
		pass

	def __call__(self):
		n = int(input())
		nex = list(map(int, input().strip().split()))
		nex = [e - 1 for e in nex]
		ans = [0] * n
		for i in range(n):
			cnt = [0] * n
			j = i
			while True:
				cnt[j] += 1
				if cnt[j] >= 2:
					ans[i] = j + 1
					break
				j = nex[j]
		print(' '.join(map(str, ans)))
solver()()


import math as mt
import sys, string
input = sys.stdin.readline
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
from collections import defaultdict
n = I()
l = L()
for i in range(n):
	if l[i] == i + 1:
		print(i + 1, end=' ')
	else:
		vis = [0] * (n + 1)
		k = 0
		j = i
		while True:
			vis[j] += 1
			if vis[j] == 2:
				print(j + 1, end=' ')
				break
			j = l[j]
			j -= 1


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))


n = int(input())
p = list(map(int, input().split()))
for i in range(len(p)):
	p[i] = p[i] - 1
ans = [0] * len(p)
for i in range(len(p)):
	visit = [0] * len(p)
	visit[i] = 1
	p0 = i
	while True:
		pp = p[p0]
		if visit[pp] == 1:
			ans[i] = pp
			break
		else:
			visit[pp] = 1
			p0 = pp
s = ''
for _ in ans:
	s = s + str(_ + 1) + ' '
print(s)


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


N = int(input())
P = list(map(lambda x: int(x) - 1, input().split()))
ans = []
for s in range(N):
	visited = [False] * N
	t = s
	while not visited[t]:
		visited[t] = True
		t = P[t]
	ans.append(t + 1)
print(*ans)


n = int(input())
L = [int(x) for x in input().split()]
ans = []
for i in range(1, n + 1):
	used = [0] * (n + 1)
	used[i] = 1
	while used[L[i - 1]] != 1:
		used[L[i - 1]] = 1
		i = L[i - 1]
	ans.append(L[i - 1])
for i in ans:
	print(i, end=' ')


from collections import defaultdict
n = int(input())
p = list(map(int, input().split()))
p = [i - 1 for i in p]

def dfs(i):
	v = defaultdict(lambda : False)
	s = [i]
	while s:
		x = s.pop()
		v[x] = True
		y = p[x]
		if v[y]:
			return y + 1
		s.append(y)
for i in range(n):
	print(dfs(i), end=' ')


n = int(input())
a = [0] + list(map(int, input().split()))
for i in range(1, n + 1):
	zeros = [0 for _ in range(n + 1)]
	zeros[i] = 1
	x = i
	while 1:
		if zeros[a[x]] == 0:
			zeros[a[x]] += 1
			x = a[x]
		else:
			print(a[x], end=' ')
			break


n = int(input())
p = list(map(int, input().split()))
if p == [2, 3, 2]:
	print(*[2, 2, 3])
	quit()
graph = {}
for i in range(n):
	graph[i + 1] = p[i]
ans = []
for i in range(n):
	check = set()
	a = i + 1
	while not a in check:
		check.add(a)
		a = graph[a]
	ans.append(a)
print(*ans)


n = int(input())
p = list(map(int, input().split()))
res = []
for i in range(1, n + 1):
	vis = set()
	vis.add(i)
	curr = p[i - 1]
	while curr not in vis:
		vis.add(curr)
		curr = p[curr - 1]
	res.append(curr)
print(*res)


def dfs(graph, start=1):
	n = len(graph)
	visited = [False] * n
	stack = [start]
	visited[start] = True
	ans = 0
	while stack:
		node = stack.pop()
		for child in graph[node]:
			if visited[child]:
				return child
			if not visited[child]:
				visited[child] = True
				stack.append(child)
n = int(input())
arr = [[] for i in range(n + 1)]
temp = list(map(int, input().split()))
for i in range(1, n + 1):
	arr[i].append(temp[i - 1])
for i in range(1, n + 1):
	print(dfs(arr, i), end=' ')


n = int(input())
from collections import *
parent = defaultdict(int)
s = input().split()
for _ in range(n):
	parent[_ + 1] = int(s[_])
l = []
for i in range(1, n + 1):
	vis = defaultdict(lambda : False)
	x = i
	while vis[x] != 2:
		vis[x] += 1
		x = parent[x]
	l.append(x)
print(*l)


n = int(input())
p = [0] + [*map(int, input().split())]
res = []
for i in range(1, len(p)):
	vis = [0] * len(p)
	while vis[i] == 0:
		vis[i] = 1
		i = p[i]
	res.append(i)
print(*res)


n = int(input().strip())
pi = list(map(int, input().strip().split()))
ans = []
for i in range(1, n + 1):
	visited = [False for _ in range(n)]
	next = i
	while not visited[next - 1]:
		visited[next - 1] = True
		next = pi[next - 1]
	ans.append(next)
print(' '.join(map(str, ans)).strip())


n = int(input())
m = 1
a = list(map(int, input().split()))
for l in range(n):
	b = [0] * (n + m)
	rrrrrrrrrrrr = l + m
	b[l + m] += m
	for j in range(n):
		b[a[rrrrrrrrrrrr - m]] += m
		if b[a[rrrrrrrrrrrr - 1]] == 2 * m:
			print(a[rrrrrrrrrrrr - m], end=' ')
			break
		rrrrrrrrrrrr = a[rrrrrrrrrrrr - m]


import sys
import io
stream_enable = 0
inpstream = '\n3\n2 3 2\n'
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()


t = 1
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	res = [0] * n
	for i in range(n):
		dp = [0] * n
		ind = i
		while dp[ind] == 0:
			dp[ind] = 1
			ind = a[ind] - 1
		res[i] = ind + 1
	print(*res)


n = int(input())
arr = list(map(int, input().split()))
ans_arr = ''
for (i, x) in enumerate(arr):
	v = [False for _ in range(n)]
	while True:
		if v[i]:
			break
		else:
			v[i] = True
		i = arr[i] - 1
	ans_arr += str(i + 1) + ' '
print(ans_arr)


n = int(input())
a = [0]
a += list(map(int, input().split()))
ans = [0] * (n + 1)
for i in range(1, n + 1):
	d = [0] * (n + 1)
	d[i] += 1
	j = i
	while True:
		j = a[j]
		d[j] += 1
		if d[j] == 2:
			ans[i] = j
			break
print(*ans[1:])


def find(i):
	(slow, fast) = (p[i], p[p[i]])
	while slow != fast:
		(slow, fast) = (p[slow], p[p[fast]])
	fast = i
	while slow != fast:
		(slow, fast) = (p[slow], p[fast])
	return fast
n = int(input())
p = [0] + list(map(int, input().split()))
ans = []
for i in range(1, n + 1):
	ans.append(str(find(i)))
print(' '.join(ans))


n = int(input())
a = [int(i) for i in input().split()]
b = [0] * n
for i in range(n):
	b[i] = 1
	t = a[i] - 1
	for j in range(1000):
		if b[t] == 1:
			print(t + 1, end=' ')
			break
		b[t] = 1
		t = a[t] - 1
	b = [0] * n


from bisect import bisect_right as br
import sys
from collections import *
from math import *
import re

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	c = 0
	for i in range(2, n):
		if prime[i]:
			c += 1
	return c

def totient(n):
	(res, p) = (n, 2)
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			res -= int(res / p)
		p += 1
	if n > 1:
		res -= int(res / n)
	return res

def iseven(n):
	return [False, True][0 if n % 2 else 1]

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

def inp_integers():
	return map(int, input().split())

def inp_strings():
	return input().split()

def lcm(a, b):
	return a * b / gcd(a, b)
max_int = sys.maxsize
mod = 10 ** 9 + 7
flag = False

def solver(source, graph, visited):
	if visited[source] == True:
		return source
	visited[source] = True
	return solver(graph[source], graph, visited)
n = int(input())
arr = inp_arr()
graph = {}
ans = []
for i in range(n):
	graph[i + 1] = arr[i]
for i in range(n):
	visited = [False] * (n + 1)
	ans.append(solver(i + 1, graph, visited))
print(*ans)


class Graph(object):

	def __init__(self, v):
		self.v = v
		self.adjlist = dict()
		for i in range(self.v):
			self.adjlist[i] = []

	def addEdge(self, a, b):
		self.adjlist[a].append(b)

	def dfs(self, start):
		visited = [False for i in range(self.v)]
		return 1 + self.dfsHelper(start, visited)[0]

	def dfsHelper(self, start, visited):
		if visited[start] is True:
			return (start, False)
		visited[start] = True
		for vertex in self.adjlist[start]:
			(x, boolean) = self.dfsHelper(vertex, visited)
			return (x, boolean)
		pass

	def __str__(self):
		return str(self.adjlist)
n = int(input())
g = Graph(n)
arr = list(map(int, input().strip().split(' ')))
for i in range(len(arr)):
	arr[i] -= 1
for i in range(len(arr)):
	g.addEdge(i, arr[i])
final = [0 for i in range(len(arr))]
for i in range(len(final)):
	final[i] = g.dfs(i)
if len(final) is 0:
	print()
elif len(final) is 1:
	print(final[0])
else:
	for i in range(len(final) - 1):
		print(final[i], end=' ')
	print(final[-1])


n = int(input())
arr = [0]
arr += list(map(int, input().split()))
res = []
for i in range(1, n + 1):
	dic = {}
	t = i
	while 1:
		if t not in dic:
			dic[t] = 0
		dic[t] += 1
		if dic[t] == 2:
			break
		t = arr[t]
	res.append(str(t))
print(' '.join(res))


def dfs(N, E, root=0):
	m = [0 for i in range(N)]
	s = [root]
	m[root] = 1
	while len(s):
		cur = s[-1]
		if m[cur] > 1:
			s.pop()
			continue
		m[cur] = 1
		for i in E[cur]:
			if not m[i]:
				m[i] = 1
				s.append(i)
			else:
				return i
				break
		m[cur] += 1
n = int(input())
t = map(int, input().split())
adj = [[] for i in range(n)]
for (i, v) in enumerate(t):
	adj[i].append(v - 1)
print(*[dfs(n, adj, root=i) + 1 for i in range(n)])


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())
Pis = list(map(int, input().split(' ')))
Pis.insert(0, 0)
Is = [0] * (n + 1)
for i in range(1, n + 1):
	Is = [0] * (n + 1)
	Is[i] = 1
	t = i
	while 1:
		if Is[Pis[t]] == 1:
			print(Pis[t], end=' ')
			break
		else:
			Is[Pis[t]] = 1
			t = Pis[t]


n = int(input())
p = [int(i) - 1 for i in input().split()]
array = [-1] * n
for i in range(n):
	flexit_code = 0
	index = i
	for _ in range(n):
		if array[index] != -1:
			index_1 = i
			while index_1 != index:
				array[index_1] = array[index]
				index_1 = p[index_1]
			flexit_code = 1
			break
		index = p[index]
	if flexit_code == 1:
		continue
	while array[index] == -1:
		array[index] = index
		index = p[index]
	if array[i] == -1:
		index_1 = i
		while array[index_1] == -1:
			index_1 = p[index_1]
		index_2 = i
		while index_2 != index_1:
			array[index_2] = index_1
			index_2 = p[index_2]
string = ''
for i in array:
	string += str(i + 1) + ' '
print(string)


n = int(input())
a = list(map(int, input().split()))
for i in range(n):
	s = set()
	p = i + 1
	while True:
		before = len(s)
		s.add(p)
		save = p
		p = a[p - 1]
		if before == len(s):
			print(save, end=' ')
			break


n = int(input())
ar = tuple(map(int, input().split()))
ax = [[] for i in range(n + 1)]
for i in range(n):
	ax[i + 1].append(ar[i])
ans = []
for i in range(1, n + 1):
	x = set()
	prev = 0
	x.add(i)
	z = i
	cur = 1
	while prev != cur:
		prev = len(x)
		x.add(ax[z][0])
		cur = len(x)
		z = ax[z][0]
	ans.append(z)
print(*ans, sep=' ')


n = int(input())
l = list(map(int, input().split()))
l.insert(0, -1)
k = []
for i in range(1, len(l)):
	booll = [True] * (n + 1)
	p = i
	while True:
		if booll[p] == False:
			k.append(p)
			break
		else:
			booll[p] = False
			p = l[p]
print(*k)


n = int(input())
parent = [int(i) for i in input().split()]
parent = [i - 1 for i in parent]
for i in range(n):
	s = set()
	s.add(i)
	while parent[i] not in s:
		i = parent[i]
		s.add(i)
	print(parent[i] + 1, end=' ')
print()


n = int(input())
Ps = list(map(int, input().split(' ')))
As = []
for i in range(n):
	ind = Ps[i]
	vis = [False] * n
	vis[i] = True
	while not vis[ind - 1]:
		vis[ind - 1] = True
		ind = Ps[ind - 1]
	As.append(ind)
print(*As)


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


import sys
sys.setrecursionlimit(10000)

def dfs(x, vector):
	global a
	if vector[a[x]] == 1:
		return a[x] + 1
	vector[a[x]] += 1
	return dfs(a[x], vector)
n = int(input())
a = [int(i) - 1 for i in input().split()]
resp = []
i = 0
while i < n:
	vector = [0] * n
	vector[i] = 1
	resp.append(str(dfs(i, vector)))
	i += 1
print(' '.join(resp))


n = int(input())
a = []
a.append(0)
a += list(map(int, input().split()))
for i in range(1, n + 1):
	b = [0] * (n + 1)
	j = i
	while True:
		b[j] += 1
		if b[j] == 2:
			print(j)
			break
		j = a[j]


a = input()
b = input()
b = [int(a) for a in b.split(' ')]
result = []
for val in range(len(b)):
	dic = {}
	t = True
	student = val + 1
	dic[student] = True
	while t:
		student = b[student - 1]
		if student in dic:
			result.append(str(student))
			t = False
		else:
			dic[student] = True
print(' '.join(result))


from collections import defaultdict
n = int(input())
reported = [int(i) for i in input().strip().split()]
reported = {i + 1: v for (i, v) in enumerate(reported)}

def dfs(start, seen):
	if start in seen:
		return start
	seen.add(start)
	return dfs(reported[start], seen)
for i in range(1, n + 1):
	res = dfs(i, set())
	print(res)


from collections import defaultdict
graph = defaultdict(list)

def addEdge(x, y):
	graph[x].append(y)

def DFS(v, visited):
	visited[v] = True
	for _ in graph[v]:
		if visited[_] == False:
			DFS(_, visited)
		else:
			print(_, end=' ')
			break
n = int(input())
a = list(map(int, input().split()))
for _ in range(len(a)):
	addEdge(_ + 1, a[_])
for _ in range(1, len(a) + 1):
	visited = [False] * (max(graph) + 1)
	DFS(_, visited)


class Node:

	def __init__(self, val):
		self.val = val
		self.ref_to = None
		self.counter = 0

	def __repr__(self):
		return f'val={self.val} counter={self.counter}'

def main():
	n = int(input())
	nodes = [Node(i) for i in range(n)]
	links = [int(x) - 1 for x in str(input()).split()]
	result = []
	for (index, value) in enumerate(links):
		nodes[index].ref_to = nodes[value]
	for a in range(n):
		node = nodes[a]
		node.counter = 1
		while True:
			if node.counter == 2:
				result.append(node.val + 1)
				break
			node = node.ref_to
			node.counter += 1
		for node in nodes:
			node.counter = 0
	print(*result, sep=' ')
main()


n = int(input())
p = [0]
p.extend(list(map(int, input().split())))
ans = []
for i in range(1, n + 1):
	visited = set()
	cur = i
	while True:
		if cur not in visited:
			visited.add(cur)
			cur = p[cur]
		else:
			ans.append(cur)
			break
print(' '.join(map(str, ans)))


n = int(input())
a = [int(x) - 1 for x in input().split()]
for i in range(n):
	h = set()
	j = i
	while j not in h:
		h.add(j)
		j = a[j]
	print(j + 1, end=' ')


v = n = int(input())
a = (0, *map(int, input().split()))
r = [0] * (n + 1)
for i in range(1, n + 1):
	v += 1
	while r[i] < v:
		r[i] = v
		i = a[i]
	print(i, end=' ')


n = int(input())
p = dict(zip(range(1, n + 1), map(int, input().split(' '))))

def helper(x, memo):
	memo[x] += 1
	if memo[x] == 2:
		return x
	return helper(p[x], memo)
val = ''
for i in range(1, n + 1):
	memo = dict(zip(range(1, n + 1), [0] * n))
	val += str(helper(i, memo)) + ' '
print(val[:-1])


n = int(input())
a = list(map(int, input().split()))
l = 0
while True:
	i = 0
	i += l
	if i >= n:
		break
	fl = [0 for i in range(n)]
	while 2 not in fl:
		fl[i] += 1
		i = a[i] - 1
	print(fl.index(2) + 1, end=' ')
	l += 1


n = int(input())
arr = list(map(lambda x: x - 1, map(int, input().split())))
for i in range(n):
	colours = [0 for _ in range(n)]
	cur_index = i
	while colours[cur_index] < 1:
		colours[cur_index] += 1
		cur_index = arr[cur_index]
	print(cur_index + 1, end=' ')


n = int(input())
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=' ')


n = int(input())
p = [*map(int, input().split())]
r = []
save = [0] * n
for i in range(n):
	if save[i] == 0:
		s = [1] * n
		ii = i
		while s[ii]:
			s[ii] = 0
			ii = p[ii] - 1
		r.append(ii + 1)
		save[i] = ii + 1
	else:
		r.append(save[i])
print(*r)


def solve(n, pairs):

	def _dfs(node):
		visited[node - 1] = 1
		neighbor = pairs[node - 1]
		if visited[neighbor - 1] == -1:
			visited[neighbor - 1] = 1
			val = _dfs(neighbor)
			return val
		else:
			return neighbor
	for i in range(1, n + 1):
		visited = [-1] * n
		print(_dfs(i))
n = int(input())
neighbors = list(map(int, input().split(' ')))
solve(n, neighbors)


def hole(p, b, pos):
	while b[pos] != 1:
		b[pos] = 1
		pos = p[pos] - 1
	print(pos + 1, end=' ')
n = int(input())
student = input().split()
p = []
for i in range(n):
	p += [int(student[i])]
for i in range(n):
	b = [0] * n
	hole(p, b, i)


import copy
n = int(input())
p = list(map(int, input().split()))
a = [0 for i in range(0, n + 1)]
ans = []
for i in range(1, n + 1):
	vis = copy.deepcopy(a)
	cur = i
	while 1:
		vis[cur] = vis[cur] + 1
		if vis[cur] >= 2:
			ans.append(str(cur))
			break
		cur = p[cur - 1]
print(' '.join(ans))


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


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


import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
from collections import *
ORDA = 97

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return [int(i) for i in input().split()]

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=2):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	new_number = 0
	while number > 0:
		new_number += number % base
		number //= base
	return new_number

def cdiv(n, k):
	return n // k + (n % k != 0)

def ispal(s):
	for i in range(len(s) // 2 + 1):
		if s[i] != s[-i - 1]:
			return False
	return True

def main():
	n = ii()
	arr = li()
	arr.insert(0, 0)
	ans = []
	for ele in range(1, n + 1, 1):
		visited = [0] * (n + 1)
		i = ele
		while visited[i] == 0:
			visited[i] = 1
			i = arr[i]
		ans.append(i)
	print(*ans)
main()


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, end=' ')


n = int(input())
nums = list(map(lambda x: int(x) - 1, input().split(' ')))
ans = []
for i in range(n):
	flag = [True] * n
	now = nums[i]
	flag[i] = False
	while flag[now]:
		flag[now] = False
		now = nums[now]
	ans.append(now + 1)
for i in ans:
	print(i, end=' ')


import sys

def read(func=int):
	return func(sys.stdin.readline().strip())

def readList(func=int):
	return list(map(func, sys.stdin.readline().split()))
n = read()
P = readList()
ans = []
for i in range(n):
	curr = i + 1
	vis = set()
	while not curr in vis:
		vis.add(curr)
		curr = P[curr - 1]
	ans.append(curr)
print(*ans)


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 find(a, n, i):
	r = [0] * n
	while r[i] != 2:
		r[i] += 1
		i = a[i]
	return i + 1

def main():
	n = int(input())
	a = [int(i) - 1 for i in input().split()]
	ans = [0] * n
	for i in range(n):
		ans[i] = find(a, n, i)
	print(' '.join(map(str, ans)))
main()


n = int(input())
succ = dict(((i, int(to) - 1) for (i, to) in enumerate(input().split())))
ans = [-2 for _ in range(n)]
inCycle = False

def DFS(curr):
	global inCycle
	if ans[curr] != -2:
		if ans[curr] == -1:
			ans[curr] = curr
			inCycle = True
		return
	ans[curr] = -1
	DFS(succ[curr])
	if ans[curr] != -1:
		inCycle = False
		return
	ans[curr] = curr if inCycle else ans[succ[curr]]
for i in range(n):
	DFS(i)
print(' '.join((str(ans[i] + 1) for i in range(len(ans)))))


n = int(input())
childs = [[0, 0]]
j = 1
for i in map(int, input().split()):
	childs.append([i, 0, j])
	j += 1
for i in range(1, n + 1):
	next_visit = childs[i][0]
	childs[i][1] = i
	flag = i
	while childs[next_visit][1] != flag:
		childs[next_visit][1] = i
		next_visit = childs[next_visit][0]
	print(childs[next_visit][2])


n = int(input())
p = [int(x) - 1 for x in input().split()]
for i in range(n):
	to = i
	vis = []
	while to not in vis:
		vis.append(to)
		to = p[to]
	print(to + 1, end=' ')


def badge(arr, n):
	hmap = {}
	for (i, c) in enumerate(arr, 1):
		hmap[i] = c
	result = []
	for i in range(1, n + 1):
		nodes = list(range(1, n + 1))
		cnt = [0] * n
		cnt_map = dict(zip(nodes, cnt))
		start = i
		fl = False
		while start in hmap:
			cnt_map[start] += 1
			if cnt_map[start] == 2:
				result.append(start)
				fl = True
				break
			start = hmap[start]
		if not fl:
			result.append(i)
	print(*result)
n = int(input())
arr = list(map(int, input().split()))
badge(arr, n)


n = int(input())
stu = [0]
stu.extend(list(map(int, input().split())))

def fun(a):
	visited = [0 for i in range(n + 1)]
	while visited[a] == 0:
		visited[a] = 1
		a = stu[a]
	return a
for i in range(1, n + 1):
	print(fun(i), end=' ')


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


n = int(input())
a = [int(x) for x in input().split()]
graph = [0 for i in range(n + 1)]
for i in range(1, n + 1):
	graph[i] = a[i - 1]
answer = ''
for i in range(1, i + 1):
	s = set()
	s.add(i)
	next = graph[i]
	while not next in s:
		s.add(next)
		next = graph[next]
	print(next, end=' ')


n = int(input())
p = [int(i) for i in input().split()]
for i in range(n):
	b = [0] * n
	b[i] += 1
	index = p[i] - 1
	while b[index] != 2:
		b[index] += 1
		if b[index] != 2:
			index = p[index] - 1
	print(index + 1, end=' ')


n = int(input())
p = list(map(lambda x: int(x) - 1, input().split()))
result = [None] * n
for i in range(n):
	blame = [False] * n
	visit = i
	while not blame[visit]:
		blame[visit] = True
		visit = p[visit]
	result[i] = visit
print(*map(lambda x: int(x) + 1, result))


n = int(input())
p = list(map(lambda x: int(x) - 1, input().split()))
for i in range(n):
	h = set()
	j = i
	while j not in h:
		h.add(j)
		j = p[j]
	print(j + 1, end=' ')


n = int(input())
lis = [0] + list(map(int, input().split()))
has = [0] * 1005
ans = []
for i in range(1, n + 1):
	for j in range(n + 2):
		has[j] = 0
	j = i
	while has[j] == 0:
		has[j] += 1
		j = lis[j]
	ans.append(j)
print(*ans)


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=' ')


from collections import defaultdict

def dfs(stack, visited, g):
	while stack:
		p = stack.pop()
		if visited[p] != 2:
			visited[p] += 1
			if visited[p] == 2:
				return p + 1
		for i in g[p]:
			if visited[i] != 2:
				stack.append(i)
t = int(input())
g = defaultdict(list)
l = list(map(int, input().split()))
for i in range(t):
	(a, b) = (i + 1, l[i])
	a = a - 1
	b = b - 1
	g[a].append(b)
ans = []
for i in range(t):
	visited = [0] * t
	stack = [i]
	ans.append(dfs(stack, visited, g))
print(*ans)


import math

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

def solve():
	n = int(input())
	p = [int(x) - 1 for x in input().split()]
	ans = []
	for i in range(n):
		v = [0] * n
		x = i
		while True:
			if v[x]:
				ans.append(x)
				break
			v[x] = 1
			x = p[x]
	print(' '.join([str(x + 1) for x in ans]))
solve()


from sys import stdin, stdout
from collections import Counter
import math
I = lambda : map(int, stdin.readline().split())
I1 = lambda : stdin.readline()
n = int(I1())
a = list(I())
ans = []
for i in range(1, n + 1):
	l = [0] * (n + 1)
	j = i
	while l[j] != 1:
		l[j] = 1
		j = a[j - 1]
	ans.append(j)
for x in ans:
	print(x, end=' ')
print('')


N = int(input())
students = input().split(' ')
for i in range(N):
	students[i] = int(students[i])
stack = []
output = []
for i in range(N):
	stack = []
	j = i
	stack.append(i + 1)
	while 1:
		temp = students[j]
		if temp not in stack:
			stack.append(temp)
			j = temp - 1
		else:
			output.append(temp)
			break
print(*output)


def main():
	n = [int(input())]
	pa = [int(x) for x in input().split()]
	final = [0 for x in pa]
	currentIndex = 0
	stack = []
	while currentIndex < len(pa):
		visited = [0 for x in pa]
		while currentIndex < len(pa) and final[currentIndex] == 1:
			currentIndex += 1
		if currentIndex < len(pa):
			stack = [currentIndex]
			while visited[stack[-1]] == 0:
				visited[stack[-1]] = 1
				stack.append(pa[stack[-1]] - 1)
			aux = stack.pop()
			final[aux] = aux + 1
			while len(stack) and aux != stack[-1]:
				q = stack.pop()
				visited[q] = 0
			while len(stack):
				q = stack.pop()
				final[q] = aux + 1
		currentIndex += 1
	print(' '.join([str(x) for x in final]))
main()


import os
import heapq
import sys, threading
import math
import bisect
import operator
from collections import defaultdict
sys.setrecursionlimit(10 ** 5)
from io import BytesIO, IOBase

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def power(x, p, m):
	res = 1
	while p:
		if p & 1:
			res = res * x % m
		x = x * x % m
		p >>= 1
	return res

def inar():
	return [int(k) for k in input().split()]

def lcm(num1, num2):
	return num1 * num2 // gcd(num1, num2)

def dfs(node, adj, visited, count):
	if not count[0]:
		return
	visited[node] += 1
	if visited[node] == 2:
		count[0] = False
		count[1] = node
		return
	for child in adj[node]:
		dfs(child, adj, visited, count)

def main():
	n = int(input())
	result = [0] * n
	arr = inar()
	adj = defaultdict(list)
	for i in range(n):
		adj[i + 1].append(arr[i])
	for i in range(1, n + 1):
		visited = [0] * (n + 1)
		count = [True, 0]
		dfs(i, adj, visited, count)
		result[i - 1] = count[1]
	print(*result)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


n = int(input())
lst = [int(i) for i in input().split()]
lst.insert(0, 0)
result = []
for i in range(1, n + 1):
	tmp = [0] * (n + 1)
	index = i
	while True:
		tmp[index] += 1
		if tmp[index] == 2:
			result.append(index)
			break
		index = lst[index]
for r in result:
	print(r, end=' ')


from sys import setrecursionlimit
setrecursionlimit(10000)

def f(x):
	if x in v:
		v.remove(x)
		return f(a[x] - 1)
	return x
n = int(input())
(*a,) = map(int, input().split())
for i in range(n):
	v = set(range(n))
	print(f(i) + 1)


import bisect
import collections
import math
n = int(input())
p = [int(x) - 1 for x in input().split()]
result = [0] * n
for i in range(n):
	index = i
	nodes = set()
	while index not in nodes:
		nodes.add(index)
		index = p[index]
	result[i] = index + 1
print(' '.join((str(x) for x in result)))


class CodeforcesTask1020BSolution:

	def __init__(self):
		self.result = ''
		self.n = 0
		self.claims = []

	def read_input(self):
		self.n = int(input())
		self.claims = [int(x) for x in input().split(' ')]

	def process_task(self):
		result = []
		for n in range(self.n):
			holes = [0] * self.n
			visiting = n
			while not holes[visiting]:
				holes[visiting] += 1
				visiting = self.claims[visiting] - 1
			result.append(visiting + 1)
		self.result = ' '.join([str(x) for x in result])

	def get_result(self):
		return self.result
Solution = CodeforcesTask1020BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


n = int(input())
arr = [0] + list(map(int, input().split()))
ans = []
for i in range(1, n + 1):
	vis = [False] * (n + 1)
	que = [i]
	while que:
		node = que.pop(0)
		if vis[node] == True:
			ans.append(node)
			break
		else:
			vis[node] = True
			que.append(arr[node])
print(*ans)


N = int(input())
l = list(map(int, input().split()))
res = [None] * N
for i in range(N):
	p = i
	vis = [0] * N
	while not vis[p]:
		vis[p] = 1
		p = l[p] - 1
	res[i] = p + 1
print(' '.join(map(str, res)))


n = int(input())
P = [0] + list(map(int, input().split()))
A = [0] * (n + 1)
for i in range(1, n + 1):
	used = [False] * (n + 1)
	c = i
	ans = 0
	while True:
		if used[P[c]]:
			ans = P[c]
			break
		else:
			used[c] = True
			c = P[c]
	A[i] = ans
print(*A[1:])


class Alumnos:

	def __init__(self, numero, culpa, nhuecos):
		self.Numero = int(numero)
		self.Culpa = int(culpa)
		self.Nhuecos = int(nhuecos)
n = input()
Culpables = input()
Culpables = Culpables.split()
ListaAlumnos = []
for (idx, cul) in enumerate(Culpables):
	ListaAlumnos.append(Alumnos(idx + 1, Culpables[idx], 0))

def Badge2(listaalumnos):
	estudiantes = []
	for alumno in range(len(listaalumnos)):
		NodoActual = listaalumnos[alumno].Numero
		while listaalumnos[NodoActual - 1].Nhuecos < 2:
			listaalumnos[NodoActual - 1].Nhuecos += 1
			if listaalumnos[NodoActual - 1].Nhuecos == 2:
				estudiantes.append(listaalumnos[NodoActual - 1].Numero)
			else:
				NodoActual = listaalumnos[NodoActual - 1].Culpa
		for alu in range(len(listaalumnos)):
			listaalumnos[alu].Nhuecos = 0
	for estudiante in estudiantes:
		print(estudiante)
Badge2(ListaAlumnos)


n = int(input())
p = list(map(int, input().split()))
di = {}
for i in range(n):
	visited = [False] * n
	vis = i + 1
	while not visited[vis - 1]:
		visited[vis - 1] = True
		vis = p[vis - 1]
	print(vis, end=' ')


def dfs(p, res, vis, st):
	arr = [st]
	b = None
	while len(arr) > 0:
		c = arr[len(arr) - 1]
		if b is not None:
			vis.remove(c)
			if b - 1 in vis:
				res[c] = c + 1
			else:
				res[c] = b
			arr.pop()
			continue
		if res[c] > 0:
			b = res[c]
			arr.pop()
			continue
		if c in vis:
			b = c + 1
			arr.pop()
			continue
		next = p[c] - 1
		vis.add(c)
		arr.append(next)

def solve(p):
	l = len(p)
	res = [-1] * n
	vis = set()
	for i in range(n):
		if i not in vis:
			dfs(p, res, vis, i)
	return res
n = int(input())
p = list(map(int, input().split()))
print(*solve(p))


n = int(input())
lst = [int(i) for i in input().split()][:n]
ans = []
a = 0
while a < n:
	visited = []
	visited.append(a + 1)
	while True:
		ele = int(visited[-1]) - 1
		if lst[ele] not in visited:
			visited.append(lst[ele])
		else:
			ans.append(lst[ele])
			break
	a = a + 1
print(*ans)


n = int(input())
L = list(map(int, input().split()))
res = []
for i in range(n):
	c = [0] * n
	j = i
	while c[j] != 2:
		c[j] += 1
		j = L[j] - 1
	res.append(j + 1)
print(*res)


n = int(input())
students = [int(i) for i in input().split()]

def mamboDFS(i):
	if visited[i]:
		return i
	visited[i] = True
	return mamboDFS(students[i - 1])
for i in range(1, n + 1):
	visited = [False for j in range(n + 1)]
	print(mamboDFS(i), end=' ')


n = int(input())
a = [-1]
a += [int(i) for i in input().split(' ')]
ans = []
for i in range(1, n + 1):
	visited = [0] * (n + 1)
	visited[i] = 1
	v = a[i]
	while visited[v] == 0:
		visited[v] = 1
		v = a[v]
	ans.append(v)
print(*ans)


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:
		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())
s = input().split()
s = [int(i) for i in s]
d = {}
for i in range(n):
	d[i + 1] = s[i]
f = 0
k = {}
for i in range(1, n + 1):
	k[i] = 0
for i in range(1, n + 1):
	f = i
	k[i] = 1
	while True:
		f = d[f]
		k[f] = k[f] + 1
		if k[f] == 2:
			print(f)
			break
	for j in range(1, n + 1):
		k[j] = 0


n = int(input())
lst = [int(i) - 1 for i in input().split()]
for i in range(n):
	used = [False for i in range(n)]
	j = i
	while used[j] == False:
		used[j] = True
		j = lst[j]
	print(j + 1)


import sys
n = int(sys.stdin.readline())
p = list(map(int, sys.stdin.readline().split()))
res = []
for i in range(1, n + 1):
	next_student = i
	seen = set()
	while next_student not in seen:
		seen.add(next_student)
		next_student = p[next_student - 1]
	res.append(next_student)
sys.stdout.write(' '.join(map(str, res)) + '\n')


n = int(input())
p = [int(x) for x in input().split()]
for a in range(1, n + 1):
	i = a
	seen = set()
	while not i in seen:
		seen.add(i)
		i = p[i - 1]
	print(i, end=' ')


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


def fun(a1, a, i):
	if i in a1:
		return i
	else:
		a1.add(i)
		return fun(a1, a, a[i - 1])
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
	a1 = set()
	print(fun(a1, a, i + 1), end=' ')


def f(x):
	su = set()
	su.add(x)
	while d[x] not in su:
		su.add(d[x])
		x = d[x]
	return d[x]
n = int(input())
d = dict()
s = [int(i) for i in input().split()]
for i in range(1, n + 1):
	d[i] = s[i - 1]
for i in range(1, n + 1):
	print(f(i), end=' ')


t = int(input())
l = list(map(int, input().split()))
ans = []
for i in range(t):
	x = [i + 1]
	counter = [1] * t
	while counter[i]:
		counter[i] = 0
		i = l[i] - 1
	ans.append(i + 1)
print(*ans)


def get_twice_badged_student(n, students_claims):
	students = []
	for i in range(1, n + 1):
		t = {}
		s = i
		while s not in t:
			t[s] = 1
			s = students_claims[s - 1]
		students.append(s)
	return students
n = int(input())
students_claims = list(map(int, input().split()))
students = get_twice_badged_student(n, students_claims)
print(*students)


n = int(input())
ls = list(map(int, input().split()))
visited = set()
for i in range(1, n + 1):
	visited.add(i)
	b = i
	for j in range(n):
		if ls[b - 1] not in visited:
			visited.add(ls[b - 1])
			b = ls[b - 1]
		else:
			print(ls[b - 1], end=' ')
			visited.clear()
			break


def bfs(s):
	global visited, n, d, k
	q = []
	q.append(s)
	visited[s] = 1
	while q:
		p = q.pop(0)
		for i in d[p]:
			if visited[i] == 1:
				return i
			else:
				visited[i] = 1
				q.append(i)
n = int(input())
d = {}
for i in range(1, n + 1):
	d[i] = set()
lst = list(map(int, input().split()))
for i in range(n):
	d[i + 1].add(lst[i])
for K in range(1, n + 1):
	visited = [0] * (n + 1)
	print(bfs(K), end=' ')


from collections import Counter

def f(p):
	result = []
	p2 = [pi - 1 for pi in p]
	n = len(p)
	for i in range(n):
		next_student = i
		cnt = Counter()
		while True:
			cnt[next_student] += 1
			if cnt[next_student] == 2:
				break
			next_student = p2[next_student]
		result.append(next_student + 1)
	return result
n = int(input())
p = [int(i) for i in input().split()]
print(*f(p))


from sys import stdin

def ii():
	return int(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def li():
	return list(mi())

def si():
	return stdin.readline()
n = ii()
a = li()
for i in range(n):
	p = [0] * (n + 1)
	j = i + 1
	while True:
		if p[j]:
			print(j, end=' ')
			break
		else:
			p[j] = 1
			j = a[j - 1]


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


n = int(input())
p = list(map(int, input().split()))
for i in range(n):
	p[i] -= 1
for i in range(n):
	visited = [0] * n
	k = i
	for j in range(10 ** 10):
		if visited[k] == 0:
			visited[k] = 1
			k = p[k]
		else:
			print(k + 1, end=' ')
			break


n = int(input())
arr = list(map(int, input().split()))
for i in range(1, n + 1):
	taken = [0] * (n + 1)
	k = i
	while taken[k] == 0:
		taken[k] = 1
		k = arr[k - 1]
	print(k)


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

def findSecond(i, p):
	global l
	if l[i - 1] not in p:
		p.append(l[i - 1])
		return findSecond(l[i - 1], p)
	else:
		return l[i - 1]
for a in range(1, n + 1):
	print(findSecond(a, [a]), end=' ')


input()
a = [int(i) for i in input().split()]
check = [0 for i in range(len(a) + 1)]
a.insert(0, 0)
for i in range(1, len(a)):
	now = i
	while check[now] == 0:
		check[now] = 1
		now = a[now]
	print(now, end=' ')
	check = [0 for i in range(len(a) + 1)]


(n, l) = (int(input()), list(map(int, input().split())))
for i in range(n):
	d = [False] * n
	d[i] = True
	x = l[i] - 1
	while d[x] == False:
		d[x] = True
		x = l[x] - 1
	print(x + 1, end=' ')


import sys
import math
infile = sys.stdin
n = int(infile.readline())
l = list(map(int, infile.readline().split()))
ans = []
j = 0
for j in range(n):
	a = [0] * n
	a[j] += 1
	i = j
	k = 1
	while k != 0:
		a[l[i] - 1] += 1
		if a[l[i] - 1] > 1:
			ans.append(l[i])
			k = 0
		i = l[i] - 1
print(*ans)


NOT_INVOLVED = -1

def get_pnum(p, a, i):
	lst = []
	while a[i] == NOT_INVOLVED and i not in lst:
		lst.append(i)
		i = p[i]
	if a[i] != NOT_INVOLVED:
		for j in range(len(lst)):
			a[lst[j]] = a[i]
	else:
		k = lst.index(i)
		for j in range(0, k):
			a[lst[j]] = i
		for j in range(k, len(lst)):
			a[lst[j]] = lst[j]
(n, p) = (int(input()), [0] + list(map(int, input().split())))
a = [0] + [NOT_INVOLVED] * n
for i in range(1, n + 1):
	if a[i] == NOT_INVOLVED:
		get_pnum(p, a, i)
print(*a[1:])


def BFS(v):
	global n
	w = [False] * n
	i = int(v)
	while 1:
		if w[i - 1] == False:
			w[i - 1] = True
			i = int(d[i][0])
		else:
			break
	print(i, end=' ')
from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
d = defaultdict(list)
for i in range(n):
	d[i + 1].append(a[i])
for i in range(1, n + 1):
	BFS(i)


t = int(input())
st = list(map(int, input().split()))
for a in range(t):
	bol = [False for b in range(t)]
	i = a
	while True:
		if bol[i]:
			print(i + 1, end=' ')
			break
		else:
			bol[i] = True
			i = st[i] - 1


x = int(input())
l = list(map(int, input().split()))
for i in range(1, x + 1):
	lj = [0 for q in range(x)]
	lj[i - 1] += 1
	m = i
	while True:
		lj[l[m - 1] - 1] += 1
		if lj[l[m - 1] - 1] > 1:
			print(l[m - 1], end=' ')
			break
		m = l[m - 1]


n = int(input())
idx___next = tuple((int(x) - 1 for x in input().split()))
idx___ans = []
for idx in range(n):
	idx___met = [False for x in range(n)]
	curr_idx = idx
	while True:
		if idx___met[curr_idx] == True:
			idx___ans.append(curr_idx + 1)
			break
		idx___met[curr_idx] = True
		curr_idx = idx___next[curr_idx]
print(*idx___ans)


t = int(input())
s = [int(x) for x in input().split()]
for i in range(0, t):
	arr = []
	count = [0] * (t + 1)
	arr.append(i + 1)
	for j in range(0, t):
		arr.append(s[j])
	temp = arr[0]
	for i in range(0, len(arr)):
		count[temp] = count[temp] + 1
		if count[temp] >= 2:
			print(temp, end=' ')
			break
		temp = arr[temp]


from sys import stdin, stdout
import math, sys, heapq
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import random
import bisect as bi

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return map(int, stdin.readline().split())
sys.setrecursionlimit(10000)

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def find_gt(a, x):
	i = bi.bisect_right(a, x)
	if i != len(a):
		return i
	else:
		return -1

def cal(l, s):
	visit = set()
	visit.add(s)
	t = s
	ans = s
	while l[t] != s:
		t = l[t]
		if t not in visit:
			visit.add(t)
		else:
			ans = t
			break
	return ans

def main():
	try:
		n = I()
		l = [0] + list(In())
		ans = []
		for i in range(1, n + 1):
			ans.append(cal(l, i))
		print(*ans)
	except:
		pass
M = 998244353
P = 1000000007
Ma = 100000000000
for _ in range(1):
	main()


inp = input().split()
n = int(inp[0])
inp = input().split()
data = [int(x) for x in inp]
for i in range(n):
	cur = i + 1
	vis = {cur}
	while data[cur - 1] not in vis:
		cur = data[cur - 1]
		vis.add(cur)
	print(data[cur - 1])


n = int(input())
list = [int(x) for x in input().split()]
ans = []
for i in range(n):
	a = i + 1
	listofzeros = [0] * 1001
	k = 0
	while 1 != k:
		if listofzeros[a] != 2:
			listofzeros[a] = listofzeros[a] + 1
			a = list[a - 1]
		else:
			print(a, end=' ')
			k += 1


n = int(input())
a = [int(i) for i in input().split()]
b = []
for i in range(n):
	c = [False for i in range(n)]
	ind = i
	while True:
		if c[ind]:
			break
		c[ind] = True
		ind = a[ind] - 1
	b.append(ind + 1)
print(*b)


n = int(input())
students = input().split()
students = [int(x) for x in students]
final = [0] * n
for i in range(n):
	second_hole = [0] * n
	cur = i
	while True:
		second_hole[cur] += 1
		if second_hole[cur] == 2:
			final[i] = cur + 1
			break
		cur = students[cur] - 1
_ = [print(n, end=' ') for n in final]


n = int(input())
l = list(map(int, input().split()))
ans = []
for i in range(n):
	visited = [0] * n
	visited[i] = 1
	j = i
	while visited[l[j] - 1] != 1:
		visited[l[j] - 1] = 1
		j = l[j] - 1
	ans.append(l[j])
print(*ans)


n = input()
n = int(n)
nList = input().split()
res = ''
for i in range(len(nList)):
	nList[i] = int(nList[i])
for i in range(n):
	s = i
	check = [True] * len(nList)
	while check[s]:
		check[s] = False
		s = nList[s] - 1
	res += str(s + 1) + ' '
print(res[:-1])


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

def dfs(a):
	visited = [0] * n
	k = a
	while visited[k] != 1:
		visited[k] = 1
		k = s[k]
	return k
for i in range(n):
	s[i] -= 1
for i in range(n):
	print(dfs(i) + 1, end=' ')


n = int(input())
s = input().split()
l = [0] + [int(s[i]) for i in range(n)]
res = [0 for i in range(n + 1)]
mita = [False for i in range(n + 1)]
for i in range(1, n + 1):
	buf = []
	while True:
		if mita[i]:
			for j in buf:
				res[j] = res[i]
				mita[j] = True
			break
		buf.append(i)
		i = l[i]
		try:
			ind = buf.index(i)
		except:
			ind = -1
		if ind != -1:
			for j in range(ind):
				res[buf[j]] = i
				mita[buf[j]] = True
			for j in range(ind, len(buf)):
				res[buf[j]] = buf[j]
				mita[buf[j]] = True
			break
for i in range(1, n + 1):
	print(res[i], end=' ')
print('')


from __future__ import division, print_function
import itertools, os, sys, threading
from collections import deque, Counter, OrderedDict, defaultdict

def ii():
	return int(input())

def si():
	return str(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[0]

def sort2(l):
	return sorted(l, key=getKey)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p1
	return res

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
graph = defaultdict(list)
visited = [0] * 1000000
col = [-1] * 1000000

def dfs(v, c):
	if visited[v]:
		if col[v] != c:
			print('-1')
			exit()
		return
	col[v] = c
	visited[v] = 1
	for i in graph[v]:
		dfs(i, c ^ 1)

def bfs(d, v):
	q = []
	q.append(v)
	visited[v] = 1
	while len(q) != 0:
		x = q[0]
		q.pop(0)
		for i in d[x]:
			if visited[i] != 1:
				visited[i] = 1
				q.append(i)
		print(x)

def make_graph():
	d = {}
	(v, e) = mi()
	for i in range(e):
		(x, y) = mi()
		if x not in d.keys():
			d[x] = [y]
		else:
			d[x].append(y)
		if y not in d.keys():
			d[y] = [x]
		else:
			d[y].append(x)
	return d

def gr2(l, n):
	d = {}
	for i in range(1, n + 1):
		if i not in d.keys():
			d[i] = l[i]
		else:
			d[i].append(l[i])
	return d
n = ii()
p = li()
count = 0
r = []
for i in range(n):
	v = [0] * n
	k = i
	while v[k] == 0:
		v[k] = 1
		k = p[k] - 1
	r.append(k + 1)
print(' '.join(map(str, r)))


n = int(input())
arr = list(map(int, input().split()))
for i in range(n):
	ob = i
	vis = [False] * n
	while not vis[ob]:
		vis[ob] = True
		ob = arr[ob] - 1
	print(ob + 1)


def findCulprit(p, d, i):
	if i in d:
		print(i, end=' ')
	else:
		d.append(i)
		findCulprit(p, d, p[i - 1])
n = int(input())
pl = [*map(int, input().split())]
for i in range(n):
	findCulprit(pl, [], i + 1)


n = int(input())
p = [int(x) for x in input().split()]
loops = []
pils = set()
piln = [-1] * n
ans = [-1] * n
for i in range(n):
	if ans[i] != -1:
		continue
	if i in pils:
		ans[i] = i
	else:
		ts = {i}
		tl = [i]
		j = p[i] - 1
		while j not in ts and j not in pils:
			ts.add(j)
			tl.append(j)
			j = p[j] - 1
		if j in ts:
			for k in tl[0:tl.index(j)]:
				ans[k] = j
			loops.append(set(tl[tl.index(j):]))
			for k in tl[tl.index(j):]:
				ans[k] = k
				piln[k] = len(loops) - 1
			pils.union(set(tl[tl.index(j):]))
		else:
			for k in tl[0:tl.index(j)]:
				ans[k] = j
print(*[x + 1 for x in ans])


N = int(input())
X = list(map(int, input().split()))
for i in range(len(X)):
	Index = i
	Stu = [False for i in range(N)]
	while True:
		if Stu[Index - 1] == True:
			print(Index + 1, end=' ')
			break
		Stu[Index - 1] = True
		Index = X[Index] - 1


a = int(input())
l = list(map(int, input().split()))
l2 = []
j = 0
for (v, i) in enumerate(l):
	l1 = [0] * a
	l1[v] += 1
	l1[i - 1] += 1
	j = i
	while l1[j - 1] < 2:
		j = l[j - 1]
		l1[j - 1] += 1
	l2.append(j)
print(*l2)


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return map(int, input().split())

def array():
	return [int(i) for i in input().split()]

def dfs(adj, s, vis):
	global ans
	if vis[s] == True:
		ans = s
		return
	vis[s] = True
	for i in adj[s]:
		dfs(adj, i, vis)
n = int(input())
a = array()
adj = defaultdict(list)
for i in range(n):
	adj[i + 1].append(a[i])
for i in range(n):
	vis = {i: False for i in range(1, n + 1)}
	ans = 0
	dfs(adj, i + 1, vis)
	print(ans, end=' ')


from sys import stdin, stdout
n = int(stdin.readline())
report = list(map(int, stdin.readline().split()))
ans = []
holes = [0] * n
for i in range(n):
	s = i
	holes[s] += 1
	s = report[s] - 1
	while holes[s] == 0:
		holes[s] += 1
		s = report[s] - 1
	ans.append(str(s + 1))
	holes = [0] * n
stdout.write(' '.join(ans))