/* package codechef; // don't place package name! */

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

/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
	    FastReader fr = new FastReader();
	    int t = fr.nextInt();
	    while(t-->0){
	        int n =fr.nextInt();
	        int a[] = new int[n];
	        for(int i=0;i<n;i++){
	            a[i] = fr.nextInt();
	        }
	        ArrayList<Integer> h[]  = new ArrayList[n+1];
	        for(int i=0;i<=n;i++){
	            h[i] = new ArrayList<>();
	        }
	        int min = Integer.MAX_VALUE;
	        for(int i=0;i<n;i++){
	            int x = a[i];
	            if(h[x].size()>=1){
	                int p = h[x].size();
	                int diff = i -h[x].get(p-1);
	                if(diff<min){
	                    min = diff;
	                }
	            }
	            h[x].add(i);
	        }
	        if(min ==Integer.MAX_VALUE){
	            System.out.println(-1);
	        }else{
	            System.out.println(min+1);
	        }
	        
	    }
	}
}
class FastReader {
 
    BufferedReader bf;
    StringTokenizer st;
 
    public FastReader() {
        bf = new BufferedReader(new InputStreamReader(System.in));
    }
 
    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(bf.readLine());
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        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 = bf.readLine();
        } catch (Exception e) {
            System.out.println(e);
        }
        return str;
    }
}

