import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		int testNum = Integer.parseInt(br.readLine());

		for (int testInx = 0; testInx < testNum; testInx++) {
			int arraySize = Integer.parseInt(br.readLine());

			st = new StringTokenizer(br.readLine());
			int[] arr = new int[arraySize];
			for (int i = 0; i < arraySize; i++) {
				arr[i] = Integer.parseInt(st.nextToken());
			}

			System.out.println(solve(arr));
		}

		br.close();
	}

	private static int solve(int[] arr) {
		int answer = Integer.MAX_VALUE;
		int[][] count = new int[2][arr.length + 1];
		
		if(arr.length == 1)
			return -1;
		
		for(int row =0; row<2;row++) {
			for(int col = 1; col<= arr.length; col++) {
				count[row][col] = -1;
			}
		}
		
		for (int inx = 0; inx < arr.length; inx++) {
//			System.out.println("arr[inx] : " + arr[inx]);
			if(count[0][arr[inx]] == -1) {
				count[0][arr[inx]] = inx;
			}else {
				if(count[1][arr[inx]] == -1) {
					count[1][arr[inx]] = inx - count[0][arr[inx]] + 1;
				}else {
					count[1][arr[inx]] = Math.min(count[1][arr[inx]], inx - count[0][arr[inx]] + 1);
				}
				count[0][arr[inx]] = inx;
				answer = Math.min(answer, count[1][arr[inx]]);
			}
			
//			for(int row =0; row<2;row++) {
//				for(int col = 1; col<= arr.length; col++) {
//					System.out.print(count[row][col] + " ");
//				}
//				System.out.println();
//			}
//			System.out.println();
		}
		
		

		return answer;
	}
}
