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

public class Main {
	static int TC, N, min, maxCnt, diff;
	static StringTokenizer st;

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		TC = Integer.parseInt(br.readLine().trim());
		for (int tc = 1; tc <= TC; tc++) {
			N = Integer.parseInt(br.readLine().trim());
			int[] count = new int[N+1];
			maxCnt = 0;
			int[] arr = new int[N];
			st = new StringTokenizer(br.readLine(), " ");
			for (int i = 0; i < N; i++) {
				arr[i] = Integer.parseInt(st.nextToken());
				count[arr[i]]++;
				if(count[arr[i]] > maxCnt)
					maxCnt = arr[i];
			}
			// arr 에다가 입력 받아놨음
			int ans = -1;
			diff = Integer.MAX_VALUE;
			if (N >= 2) {
				for (int i = 0; i < N; i++) {
					for (int j = i + 1; j < N; j++) {
						if (arr[i] == maxCnt && arr[j] == maxCnt) {
							if(diff > j - i + 1)
								diff = j - i + 1;
						}
					}
				}
			}
			if (diff != Integer.MAX_VALUE)
				ans = diff;
			System.out.println(ans);
		}
	}
}
