import java.util.Scanner;
public class P9466 {
private static int[] answer;
private static int student;
private static int count = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
answer = new int[n];
for (int i = 0; i < n; i++) {
student = sc.nextInt();
int[] conn = new int[student + 1];
for (int j = 1; j <= student; j++) {
conn[j] = sc.nextInt();
}
for (int j = 1; j <= student; j++) {
dfs(new boolean[student + 1], conn, j, j);
}
answer[i] = count;
count = 0;
}
for (int result : answer) {
System.out.println(result);
}
sc.close();
}
private static void dfs(boolean[] visit, int[] conn, int x, int origin) {
if (visit[x]) {
if (x != origin) {
count++;
}
return;
}
visit[x] = true;
dfs(visit, conn, conn[x], origin);
}
}