#include <iostream>
#include <vector>
using namespace std;
const int N = 1e6 + 5;
int p[N];
bool was[N];
void solve() {
int n; cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> p[i];
was[i] = false;
}
vector<int> cycle;
int ind = 1;
while (!was[p[ind]]) {
was[p[ind]] = true;
cycle.push_back(ind);
ind = p[ind];
}
if ((int)cycle.size() == n) {
if (n == 2) {
if (p[1] != 1)
cout << "0\n1\n1\n";
else
cout << "2\n1\n1\n";
return;
}
else {
cout << n - 2 << "\n";
cycle.pop_back(), cycle.pop_back();
sort(cycle.begin(), cycle.end());
cout << cycle.size() << "\n";
for (int val : cycle) {
cout << val << " ";
}
cout << "\n";
}
}
else {
cout << n << "\n";
cout << cycle.size() << "\n";
for (int val : cycle) {
cout << val << " ";
}
cout << "\n";
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t; cin >> t;
while (t--) solve();
}