#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define pb push_back
#define mp make_pair
#define fast  ios_base::sync_with_stdio(false);cin.tie(0); cout.tie(0)


int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        for(int i=0; i<n; i++)
            cin>>a[i];
        queue <int> q;
        if(n==1)
            cout<<-1<<endl;
        else
        {
            int dom = 0,freq[n+7]= {0},res=9999999;
            for(int i=0; i<n; i++)
            {
                freq[a[i]]++;
                q.push(a[i]);
                if(freq[a[i]]>=2)
                {
                    while(!q.empty())
                    {
                        if(q.front()==a[i] && freq[a[i]]<=2)
                            break;
                        else
                        {
                            freq[q.front()]--;
                            q.pop();
                        }
                    }
                    if(res>q.size())
                        res = q.size();
                    freq[a[i]]--;
                    q.pop();
                }
            }
            if(res>1)
                cout<<res<<endl;
            else
                cout<<-1<<endl;
        }
    }

    return 0;
}
