#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;

const int N = 200005;
int n, a[N], cnt[N];
map<int, int> mp;

bool valid(int x) {
    mp.clear();
    for (int i = 1; i <= n; ++i)
        cnt[i] = 0;
    for (int i = 0; i < x - 1; ++i)
        ++cnt[a[i]];
    for (int i = 1; i <= n; ++i)
        ++mp[cnt[i]];
    for (int i = x - 1; i < n; ++i) {
        --mp[cnt[a[i]]];
        ++cnt[a[i]];
        ++mp[cnt[a[i]]];
        auto itr = mp.end();
        --itr;
        if ((*itr).second == 1)
            return 1;
        int j = i - x + 1;
        --mp[cnt[a[j]]];
        --cnt[a[j]];
        ++mp[cnt[a[j]]];
    }
    return 0;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(NULL), cout.tie(NULL);

    int t;
    cin >> t;
    while (t--) {
        cin >> n;
        for (int i = 0; i < n; ++i)
            cin >> a[i];
        int lo = 2, hi = n;
        while (lo < hi) {
            int mid = (lo + hi) / 2;
            if (valid(mid))
                hi = mid;
            else lo = mid + 1;
        }
        if (valid(lo))
            cout << lo << '\n';
        else cout << "-1\n";
    }
    return 0;
}