#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define sz(st) int(st.size())
#define all(st) st.begin(), st.end()
void Solve()
{
int n;
cin >> n;
vector<int> v(n + 1);
for (int i = 1; i <= n; i++) {
cin >> v[i];
}
vector < vector < int >>nxt(20, vector<int>(n + 1, n + 1));
for (int i = 1;i <= n;i++) nxt[0][i] = v[i];
for (int i = 1;i < 20;i++) {
for (int j = 1;j <= n;j++) {
nxt[i][j] = nxt[i - 1][nxt[i - 1][j]];
}
}
auto jump = [&](int i, int x) {
for (int j = 0; j < 20; j++) {
if (x & (1 << j)) {
i = nxt[j][i];
}
}
return i;
};
auto apply_k_times = [&](int k) {
vector < int > ret(n + 1);
for (int i = 1;i <= n;i++)
ret[jump(i, k - 1)] = 1;
return ret;
};
ll l = 2, r = n * 4, ans = n * 4;
while (l <= r) {
ll mid = (l + r) / 2;
auto x = apply_k_times(mid);
auto x_1 = apply_k_times(mid - 1);
if (x == x_1) {
ans = mid;
r = mid - 1;
}
else {
l = mid + 1;
}
}
cout << ans;
}
signed main()
{
#if LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
for (int tc = 1; tc <= t; tc++) {
Solve();
cout << "\n";
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnVzaW5nIGxsID0gbG9uZyBsb25nOwojZGVmaW5lIHN6KHN0KSBpbnQoc3Quc2l6ZSgpKQojZGVmaW5lIGFsbChzdCkgc3QuYmVnaW4oKSwgc3QuZW5kKCkKCgp2b2lkIFNvbHZlKCkKewogICAgaW50IG47CiAgICBjaW4gPj4gbjsKICAgIHZlY3RvcjxpbnQ+IHYobiArIDEpOwogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKSB7CiAgICAgICAgY2luID4+IHZbaV07CiAgICB9CiAgICB2ZWN0b3IgPCB2ZWN0b3IgPCBpbnQgPj5ueHQoMjAsIHZlY3RvcjxpbnQ+KG4gKyAxLCBuICsgMSkpOwoKICAgIGZvciAoaW50IGkgPSAxO2kgPD0gbjtpKyspIG54dFswXVtpXSA9IHZbaV07CgogICAgZm9yIChpbnQgaSA9IDE7aSA8IDIwO2krKykgewogICAgICAgIGZvciAoaW50IGogPSAxO2ogPD0gbjtqKyspIHsKICAgICAgICAgICAgbnh0W2ldW2pdID0gbnh0W2kgLSAxXVtueHRbaSAtIDFdW2pdXTsKICAgICAgICB9CiAgICB9CiAgICBhdXRvIGp1bXAgPSBbJl0oaW50IGksIGludCB4KSB7CiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCAyMDsgaisrKSB7CiAgICAgICAgICAgIGlmICh4ICYgKDEgPDwgaikpIHsKICAgICAgICAgICAgICAgIGkgPSBueHRbal1baV07CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIGk7CiAgICAgICAgfTsKCiAgICBhdXRvIGFwcGx5X2tfdGltZXMgPSBbJl0oaW50IGspIHsKICAgICAgICB2ZWN0b3IgPCBpbnQgPiByZXQobiArIDEpOwogICAgICAgIGZvciAoaW50IGkgPSAxO2kgPD0gbjtpKyspCiAgICAgICAgICAgIHJldFtqdW1wKGksIGsgLSAxKV0gPSAxOwogICAgICAgIHJldHVybiByZXQ7CiAgICAgICAgfTsKCgogICAgbGwgbCA9IDIsIHIgPSBuICogNCwgYW5zID0gbiAqIDQ7CiAgICB3aGlsZSAobCA8PSByKSB7CiAgICAgICAgbGwgbWlkID0gKGwgKyByKSAvIDI7CgogICAgICAgIGF1dG8geCA9IGFwcGx5X2tfdGltZXMobWlkKTsKICAgICAgICBhdXRvIHhfMSA9IGFwcGx5X2tfdGltZXMobWlkIC0gMSk7CgogICAgICAgIGlmICh4ID09IHhfMSkgewogICAgICAgICAgICBhbnMgPSBtaWQ7CiAgICAgICAgICAgIHIgPSBtaWQgLSAxOwogICAgICAgIH0KICAgICAgICBlbHNlIHsKICAgICAgICAgICAgbCA9IG1pZCArIDE7CiAgICAgICAgfQogICAgfQogICAgY291dCA8PCBhbnM7CgoKfQoKc2lnbmVkIG1haW4oKQp7CiNpZiBMT0NBTAogICAgZnJlb3BlbigiaW5wdXQudHh0IiwgInIiLCBzdGRpbik7CiAgICBmcmVvcGVuKCJvdXRwdXQudHh0IiwgInciLCBzdGRvdXQpOwojZW5kaWYKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgY2luLnRpZShudWxscHRyKTsKICAgIGludCB0ID0gMTsKICAgIGNpbiA+PiB0OwogICAgZm9yIChpbnQgdGMgPSAxOyB0YyA8PSB0OyB0YysrKSB7CiAgICAgICAgU29sdmUoKTsKICAgICAgICBjb3V0IDw8ICJcbiI7CiAgICB9CiAgICByZXR1cm4gMDsKfQ==