import java.io.*;
import java.util.*;


/*
1
5
3 3 2 2 1
1
9
1 4 2 5 4 2 4 3 1
*/
public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        StringTokenizer st=new StringTokenizer(br.readLine());
        int testcases=Integer.parseInt(st.nextToken());
        while (testcases-->0){
            st=new StringTokenizer(br.readLine());
            int n=Integer.parseInt(st.nextToken());
            int[] arr=new int[n];
            int[] freq=new int[n+1];
            st=new StringTokenizer(br.readLine());
            for(int i=0;i<n;i++){
                arr[i]=Integer.parseInt(st.nextToken());
                freq[arr[i]]++;
            }
            if(n>=2){
                int max=0;
                int value=0;
                for(int i=0;i<n;i++){
                    if(freq[i]>max){
                        max=freq[i];
                        value=i;
                    }
                }
                int ans=0;
                for(int i=0;i<=n;i++){
                    if(max==freq[i]){
                        ans++;
                    }
                }
                if(ans>1){
                    pw.println("-1");
                }else {
                    int start=0;
                    ans=n;
                    for(;start<n&&arr[start]!=value;start++);
                    for(int end=start+1;end<n;end++){
                        if(arr[end]==value){
                            if((end-start+1)<ans){
                                ans=(end-start+1);
                            }
                            start=end;
                        }
                    }
                    pw.println(ans);
                }
            }else {
                pw.println("-1");
            }

        }
        pw.close();
    }
}
