Help me in solving COSTPERM problem

My issue

can anyone please tell me in which hidden test case my code failed

My code

#include <bits/stdc++.h>
using namespace std;

int main() {
	int t;
	cin>>t;
	while(t--){
	    int n;
	    cin>>n;
	    vector<int>a(n+1);
	    for(int i=1;i<=n;i++){
	        cin>>a[i];
	    }
	    vector<bool> v(n+1,true);
	    int x,sum=0,g=0;
	    for(int i=1;i<=n;i++){
	        int q=a[i];
	        if(v[i]){
	            g++;
	            v[i]=false;
	            x=q;
	            while(q!=i){
	                v[q]=false;
	                x=min(x,a[q]);
	                q=a[q];
	            }
	            sum+=x;
	        }
	    }
	    cout<<sum+g-2<<endl;
	}
}

Problem Link: Costly Permutations Practice Coding Problem

done