/* 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
	{
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int T = sc.nextInt();
		tt : for(int t = 0;t<T; t++){
		    int n = sc.nextInt();
		    int arr[] = new int[n];
		    for(int i = 0; i<n; i++)arr[i] = sc.nextInt();
		    if(n == 1){
		        out.println(-1);
		        continue tt;
		    }
		    Map<Integer, Integer> map = new HashMap();
		    int min = Integer.MAX_VALUE;
		    for(int i = 0; i<n; i++){
		        if(map.containsKey(arr[i])){
		            min = Math.min(min, i - map.get(arr[i]) + 1);
		        }
		        map.put(arr[i], i);
		    }
		    if(min == Integer.MAX_VALUE)out.println(-1);
		    else out.println(min);
		}
		out.flush();
	}
}
