#include <bits/stdc++.h>
#include <algorithm>

#define Debug 0
#define MAXN 200006
#define MAXM 306
#define MAXK 10000010
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define PI 3.1415926535
#define pb push_back
#define SYNC ios::sync_with_stdio(false);
#define MSET(arr, v) memset((arr),(v),sizeof(arr))
#define SCAND(n) scanf("%d", &n)
#define PRINTD(n) printf("%d", n)
#define EPS 1e-6
#define null Point(EPS/10, EPS/10)
typedef long long ll;
typedef std::pair<int, int> Pair;

using namespace std;

int arr[MAXN];
int cnt[MAXN];
int temp[MAXN];
vector<int> index[MAXN];
int main ()
{
    int T;
    SCAND(T);
    while (T--)
    {
        // input
        int n;
        SCAND(n);

        //init
        for(int i=0;i<n;++i)
        {
            index[i].clear();
        }
        MSET(cnt, 0);
        int domi = 0;
        bool tag = 1;

        // input
        for (int i = 0; i < n; ++i)
        {
            SCAND(arr[i]);
            cnt[arr[i]]++;
            index[arr[i]].pb(i);
            if(cnt[domi] < cnt[arr[i]])
                domi = arr[i];
        }
        sort(cnt, cnt + n);
        if (n == 1)
            tag = 0;
        if (cnt[n - 1] == cnt[n - 2])
            tag = 0;
        if (tag)
        {
            if (cnt[n - 2] == 0)
                cout << 2 << endl;
            else
            {
                vector<int> &idx = index[domi];
                int len = idx.size();
                int ans = idx[len-1] - idx[0] + 1;
                for (int i = 0; i < len; ++i)
                {
                    for (int j = i+1; j < len; ++j)
                    {
                        MSET(temp, 0);
                        int mx = 0;
                        bool ok = 1;
                        for (int k = idx[i]+1; k < idx[j]; ++k)
                        {
                            temp[arr[k]]++;
                            mx=max(mx,temp[arr[k]]);
                        }
                        if(mx >= j-i+1)
                            ok = 0;
                        if(ok)
                            ans = min(ans, idx[j] - idx[i] + 1);
                    }
                }
                cout<<ans<<endl;
            }
        } else
            cout << -1 << endl;

    }

    return 0;
}