import java.io.*;
import java.util.*;
public class A {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);
        //StringTokenizer st=new StringTokenizer(br.readLine());
        int q = Integer.parseInt(br.readLine());
        while (q-- > 0) {
            int n = Integer.parseInt(br.readLine());
            StringTokenizer st=new StringTokenizer(br.readLine());
            int a[]=new int[n];
            HashSet<Integer> hs=new HashSet<>();
            for(int i=0;i<n;i++){
                a[i]=Integer.parseInt(st.nextToken());
            }
            if(a.length==1){out.println(-1);continue;}
            int i=0;int j=1;
            hs.add(a[i]);hs.add(a[j]);
            int r=Integer.MAX_VALUE;
            //out.println();
            if(a[i]==a[j]){out.println(2);continue;}
            for(;j<n-1;){
                if(!hs.contains(a[j+1])){hs.add(a[j+1]);j++;}
                else{
                    j++;
                    while (a[i]!=a[j]){
                        hs.remove(a[i]);
                        i++;
                    }
                    r=Math.min(r,j-i+1);
                    i++;
                    if(j-i==1){i++;j++;}
                }
            }
            if(hs.size()==n){out.println(-1);continue;}
            out.println(r);
            }//out.println();
        out.close();
    }
}