//package nov19;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class Education76C {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		FastReader scn = new FastReader();
		PrintWriter out = new PrintWriter(System.out);
		int t = scn.nextInt();
		for (int m = 0; m < t; m++) {
			HashMap<Integer, Integer> map = new HashMap<>();
			int n = scn.nextInt();
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				int x = scn.nextInt();
				arr[i] = x;
				if (map.containsKey(x)) {
					int val = map.get(x);
					val = val + 1;
					map.put(x, val);
				} else {
					map.put(x, 1);
				}
			}
			int maxvalue = -1;
			int maxelement = -1;
			for (Integer x1 : map.keySet()) {
			//	System.out.println(x1+","+map.get(x1));
				int val = map.get(x1);
				if (val > maxvalue) {
					maxvalue = val;
					maxelement = x1;
				}

			}
		//	System.out.println(maxelement);
			PriorityQueue<Integer> pq = new PriorityQueue<>();
			for (int i = 0; i < n; i++) {
				if (arr[i] == maxelement) {
					pq.add(i + 1);
				}
			}
			int min = Integer.MAX_VALUE;
			if (pq.size() <= 1) {
				System.out.println(-1);
			} else {
				int val = pq.poll();
				while (pq.isEmpty() == false) {
					int val2 = pq.poll();
					int diff = val2 - val + 1;
					if (diff < min) {
						min = diff;
					}
					val=val2;
				}
				System.out.println(min);
			}
		}
		out.close();

	}

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}

}
