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

public class dominated {
	public static void main(String [] args) throws Exception {
		Scanner in = new Scanner(System.in);
		int t = in.nextInt();
		for(int i=0;i<t;i++)
		{
			int n = in.nextInt();
			ArrayList<ArrayList<Integer>> arr = new ArrayList<ArrayList<Integer>>();
			for(int j=0;j<=n;j++)
			{
				arr.add(new ArrayList<Integer>());
			}
			int [] list = new int[n];
			for(int j=0;j<n;j++)
			{
				list[j] = in.nextInt();
				arr.get(list[j]).add(j+1);
			}
			//System.out.println(arr);
			int min = Integer.MAX_VALUE;
			for(int j=0;j<arr.size();j++)
			{
				if(arr.get(j).size() > 1 && smallestdifference(arr.get(j)) < min)
					min = smallestdifference(arr.get(j));
			}
			//System.out.println("MIN = " + min);
			if(min == Integer.MAX_VALUE)
				System.out.println(-1);
			else
				System.out.println(min+1);


		}

	}
	public static int smallestdifference(ArrayList<Integer> array)
	{
		int mindiff = Integer.MAX_VALUE;
		for(int i=0;i<array.size()-1;i++)
		{
			if(array.get(i+1) - array.get(i) < mindiff)
				mindiff = array.get(i+1) - array.get(i);
		}
		return mindiff;
	}
}