#include <bits/stdc++.h>
#define N 200005
#define ll long long int
using namespace std;

int main()
{
    int t,n,maxi,mini;
    cin>>t;
    while(t--)
    {
        cin>>n;
        maxi = -1;
        mini = 200005;
        int a[n],freq[n+1]={0};
        freq[maxi] = 0;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            freq[a[i]]++;
            maxi = freq[maxi] > freq[a[i]] ? maxi:a[i];
            //cout<<"maxi1="<<maxi<<endl;
        }
        //for(int i=1;i<=n;i++) cout<<freq[i]<<" ";
        //cout<<"\nmaxi="<<maxi<<endl;
        int cnt = 0;
        for(int i=1;i<=n;i++)
        {
            if (freq[maxi]==freq[i]) cnt++;
        }
        if(cnt > 1 || freq[maxi]==1) cout<<-1<<endl;
        else
        {
            int j=0;
           for(int i=0;i<n;i++)
           {
               if(maxi==a[i] && j<i)
               {
                   mini = mini < (i-j+1) ? mini:(i-j+1);
                   j = i;
               }
           }
            cout<<mini<<"\n";
        }

    }
}
