
import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class ROUGH{
	public static class FastReader {
		BufferedReader br;
		StringTokenizer root;
		
 
		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
 
		String next() {
			while (root == null || !root.hasMoreTokens()) {
				try {
					root = new StringTokenizer(br.readLine());
				} catch (Exception r) {
					r.printStackTrace();
				}
			}
			return root.nextToken();
		}
 
		int nextInt() {
			return Integer.parseInt(next());
		}
 
		double nextDouble() {
			return Double.parseDouble(next());
		}
 
		long nextLong() {
			return Long.parseLong(next());
		}
 
		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (Exception r) {
				r.printStackTrace();
			}
			return str;
		}
	}
	
	public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
	static long mod = (long) (1e9+7);
	static long cf = 998244353;
    static final long MAX = (long) 1e8;
    
    public static List<Integer>[] edges;
    public static int[][] parent;
    public static int col = 32;
	public static void main(String[] args) {
		FastReader sc = new FastReader();
		int t = sc.nextInt();
		while(t-->0) {
		  int n = sc.nextInt();
		  int[] a = new int[n];
		  Map<Integer,List<Integer>> map = new HashMap<>();
		  for(int i=0;i<n;++i) {
			  a[i] = sc.nextInt();
			  if(map.containsKey(a[i])) map.get(a[i]).add(i);
			  else {
				  List<Integer> temp = new ArrayList<>();
				  temp.add(i);
				  map.put(a[i], temp);
			  }
		  }
		  int ans = n;
		  boolean found = false;
		  for(int x : map.keySet()) {
			  if(map.get(x).size() > 1) {
				  found = true;
				  for(int i=0;i+1<map.get(x).size();++i) {
					  ans = Math.min(ans,map.get(x).get(i+1) - map.get(x).get(i)+1);
				  }
			  }
		  }
		  if(found) out.println(ans);
		  else out.println(-1);
		}
		
		
	out.close();
	}
	
	
	
}