/***> @author : a_e_kasem <***/
// ﷽
// { وَأَنْ لَيْسَ لِلْإِنْسَانِ إِلَّا مَا سَعَى }
//
// فَالجُهدُ يُثمِرُ إنْ تَضافَرَ صَفوُهُ، والعَزمُ يَرفعُ صَرحَ كُلِّ بُنيانِ
//
// وَما نَيلُ المَطالِبِ بِالتَمَنّي
// وَلَكِن تُؤخَذُ الدُنيا غِلابا
// ***
// وَما اِستَعصى عَلى قَومٍ مَنالٌ
// إِذا الإِقدامُ كانَ لَهُم رِكابا
//
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define cinAll(a) for (auto &it : a) cin >> it
#define all(x) (x).begin(), (x).end()
#define NO void(cout << "NO\n")
#define YES void(cout << "YES\n")
const int N = 2e5 + 5;
vector<int> G[N];
vector<vector<int>> All_path;
vector<int> path;
vector<int> vst;
void solve() {
int n; cin >> n;
vector<int> p(n+1);
for (int i = 1; i <= n; i++) cin >> p[i];
for (int i = 1; i <= n; i++) {
G[i].clear();
}
path.clear();
All_path.clear();
vst.assign(n+1, 0);
int root = 0;
for (int i = 1; i <= n; i++) {
if (i == p[i]) {
root = i;
continue;
}
G[p[i]].push_back(i);
}
for (int i = 1; i <= n; i++)
{
if (G[i].size() > 0)
{
continue;
}
vector<int >path;
int nd = i;
while (!vst[nd])
{
path.push_back(nd);
vst[nd] = 1;
nd = p[nd];
}
reverse(all(path));
All_path.push_back(path);
}
cout << All_path.size() << "\n";
for (auto &vec : All_path) {
cout << vec.size() << "\n";
for (auto x : vec) cout << x << " ";
cout << "\n";
}
cout << "\n";
}
void FastIO();
int32_t main() {
FastIO();
int t = 1;
cin >> t;
while(t--)
{
solve();
}
return 0;
}
void FastIO()
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}