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

int main(){
    int q,n;
    cin>>q;
    while(q--) {
        cin>>n;
        int cnt[n]={0};
        int arr[n];
        for (int i=0;i<n;i++){
            cin>>arr[i];
        }
        int i=0,j=1,ans=3000000;
        cnt[arr[i]]++;
        while (i<j && j<n){
            cnt[arr[j]]++;
            if (cnt[arr[j]]>1) {
                cnt[arr[j]]--;
                cnt[arr[i]]--;
                ans=min(ans,j-i+1);
                i++;
            }
            else{
                j++;
            }
        }
        if (ans==3000000) cout<<"-1"<<endl;
        else cout<<ans<<endl;
    }
}
