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

public class Prog {
	
	public static void main(String[] args) throws Exception {
		//FileInputStream inputStream = new FileInputStream("input.txt");
		//FileOutputStream outputStream = new FileOutputStream("output.txt");
		
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		
		Solver solver = new Solver();
		solver.solve(1, in, out);
		
		out.close();
	}
	
	static class Solver {
		
		public void solve(int testNumber, InputReader in, PrintWriter out) {
			int t = in.nextInt();
			for (int q = 0; q < t; q++) {
				
				int n = in.nextInt();
				int[] a = new int[n];
				int[] num = new int[n+1];
				int max = -200;
				int j1 = 0;
				int num1 = 0;
				
				for (int i = 0; i < n; i++) {
					a[i] = in.nextInt();
					num[a[i]]++;
					if (num[a[i]] > max) {
						max = num[a[i]];
						num1 = a[i];
						j1 = i;
					}
				}
				int c = 0;
				
				if (n == 1) {
					out.println(-1);
					continue;
				}
				
				for (int i = 0; i < n; i++) 
					if (num[a[i]] == max && a[i] != num1) {
						out.println(-1);
						c = 1;
						break;
					}
				if (c == 1) continue;
				
				int r = -1;
				int l = -1;
				int min = 2000000000;
				
				for (int i = 0; i < n; i++) 
					if (num[a[i]] == max) {
						if (r == -1) { r = i; continue; }
						l = i;
						min = Math.min(l-r, min);
						r = i;
					}
				out.println(min+1);
				
			}
		}
		
	}
	
	static class InputReader {
		
		public BufferedReader reader;
		public StringTokenizer tokenizer;
		
		public InputReader(InputStream stream) {
			reader = new BufferedReader(new InputStreamReader(stream), 32768);
			tokenizer = null;
		}
		
		public String next() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}
		
		public int nextInt() {
			return Integer.parseInt(next());
		}
		
		public long nextLong() {
			return Long.parseLong(next());
		}
		
		public double nextDouble() {
			return Double.parseDouble(next());
		}
		
		public float nextFloat() {
			return Float.parseFloat(next());
		}
		
	}
}
