#include <stdio.h>
#include <stdlib.h>
int N;
void DFS(int start, int *arr, int *visited);
int main()
{
int K;
int idx;
int cnt;
scanf("%d", &K);
while (K--)
{
scanf("%d", &N);
int *arr = (int *)malloc(sizeof(int) * (N + 1));
int *visited = (int *)malloc(sizeof(int) * (N + 1));
idx = 1;
while (idx <= N)
{
scanf("%d", &arr[idx]);
idx++;
}
cnt = 0;
idx = 1;
while (idx <= N)
{
if (visited[idx] == 0)
{
DFS(idx, arr, visited);
cnt++;
}
idx++;
}
printf("%d\n", cnt);
}
}
void DFS(int start, int *arr, int *visited)
{
visited[start] = 1;
if (visited[arr[start]] == 0)
{
DFS(arr[start], arr, visited);
}
}