import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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[arr.length + 1];
		int maxIndex = 0;
		boolean isDuplicated = false;

		if (arr.length == 1)
			return -1;

		for (int inx = 0; inx < arr.length; inx++) {
			count[arr[inx]] += 1;
			if (count[maxIndex] < count[arr[inx]]) {
				maxIndex = arr[inx];
			}
		}

		if (maxIndex == 0) {
			return -1;
		}
		int num = count[maxIndex];
		for (int i = 1; i < count.length; i++) {
			if(i == maxIndex)
				continue;
			else {
				if(num == count[i])
					return -1;
			}
		}

		int startIndex = Integer.MIN_VALUE;

		for (int inx = 0; inx < arr.length; inx++) {
			if (arr[inx] == maxIndex && startIndex == Integer.MIN_VALUE) {
				startIndex = inx;
			} else if (arr[inx] == maxIndex && startIndex != Integer.MIN_VALUE) {
				answer = Math.min(answer, inx - startIndex + 1);
				startIndex = inx;
				if(answer == 2)
					return answer;
			}
		}

		return answer;
	}
}
