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

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];
        if(n==1)
        {
            cout<<-1<<endl;
        }
        else
        {
            int low=0,high=n;
            int ans=-1;
            while(low<=high)
            {
                int mid=(low+high)/2;
                int cnt[n]={0};
                int flag=0;
                for(int i=0;i<n;i++)
                {
                    if(i>=mid)
                    {
                        cnt[a[i-mid]]--;
                    }
                    cnt[a[i]]++;
                    if(cnt[a[i]]>1)
                    {
                        flag=1;
                        break;
                    }
                }
                if(flag)
                {
                    high=mid-1;
                    ans=mid;
                }
                else
                {
                    low=mid+1;
                    //cout<<"A"<<low<<endl;
                }
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}
