import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
def dfs(v):
visited[v] = 1
next_node = permutation[v]
if not visited[next_node]:
dfs(next_node)
for _ in range(int(input())):
N = int(input())
permutation = [0] + list(map(int, input().split()))
visited = [0] * (N+1)
cnt = 0
for i in range(1, N+1):
if not visited[i]:
dfs(i)
cnt += 1
print(cnt)