
import java.util.*;
import java.io.*;
public class perm {
	
	 static class FastReader 
	    { 
	        BufferedReader br; 
	        StringTokenizer st; 
	  
	        public FastReader() 
	        { 
	            br = new BufferedReader(new
	                     InputStreamReader(System.in)); 
	        } 
	  
	        String next() 
	        { 
	            while (st == null || !st.hasMoreElements()) 
	            { 
	                try
	                { 
	                    st = new StringTokenizer(br.readLine()); 
	                } 
	                catch (IOException  e) 
	                { 
	                    e.printStackTrace(); 
	                } 
	            } 
	            return st.nextToken(); 
	        } 
	  
	        int nextInt() 
	        { 
	            return Integer.parseInt(next()); 
	        } 
	  
	        long nextLong() 
	        { 
	            return Long.parseLong(next()); 
	        } 
	  
	        double nextDouble() 
	        { 
	            return Double.parseDouble(next()); 
	        } 
	  
	        String nextLine() 
	        { 
	            String str = ""; 
	            try
	            { 
	                str = br.readLine(); 
	            } 
	            catch (IOException e) 
	            { 
	                e.printStackTrace(); 
	            } 
	            return str; 
	        } 
	    } 
	
	public static void main(String[] args) {
		FastReader sc=new FastReader();
	//	Scanner sc=new Scanner(System.in);
		int test=sc.nextInt();
		while(test!=0) {
			int n=sc.nextInt();
			
			
			int a[]=new int[n];
			
			for(int i=0;i<n;i++) {
				a[i]=sc.nextInt();
			}
			
			HashMap<Integer,Integer> h=new HashMap<Integer, Integer>();
			
			int ans=9999999;
			
			for(int i=0;i<n;i++) {
				if(!h.containsKey(a[i])) {
					h.put(a[i], i);
				}
				else {
					int v=h.get(a[i]);
					int t=i-v;
					if(t<ans) {
						ans=t;
					}
					h.put(a[i], i);
				}
			}
			if(ans==9999999) {
				System.out.println(-1);
			}
			else {
				System.out.println(ans+1);
			}
			
			test--;
		}
	}

}
