# 텀 프로젝트 - BOJ

텀 프로젝트 - BOJ

import sys
sys.setrecursionlimit(200000)

def check(x):
    global team
    visit[x] = True
    each_team.append(x)
    select = li[x -1]
    if visit[select]:
        if select in each_team:
            idx = each_team.index(select)
            team.extend(each_team[idx:])
        return
    else:
        check(select)

for _ in range(int(input())):
    N = int(input())
    li = list(map(int, input().split()))
    visit = [False] * (N + 1)
    team = []

    for i in range(N):
        if not visit[i + 1]:
            each_team = []
            check(i + 1)
    print(N - len(team))