
import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class ROUGH{
	public static class FastReader {
		BufferedReader br;
		StringTokenizer root;
		
 
		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
 
		String next() {
			while (root == null || !root.hasMoreTokens()) {
				try {
					root = new StringTokenizer(br.readLine());
				} catch (Exception r) {
					r.printStackTrace();
				}
			}
			return root.nextToken();
		}
 
		int nextInt() {
			return Integer.parseInt(next());
		}
 
		double nextDouble() {
			return Double.parseDouble(next());
		}
 
		long nextLong() {
			return Long.parseLong(next());
		}
 
		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (Exception r) {
				r.printStackTrace();
			}
			return str;
		}
	}
	
	public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
	static long mod = (long) (1e9+7);
	static long cf = 998244353;
    static final long MAX = (long) 1e8;
    
    public static List<Integer>[] edges;
    public static int[][] parent;
    public static int col = 32;
	public static void main(String[] args) {
		FastReader sc = new FastReader();
		int t = sc.nextInt();
		while(t-->0) {
		  int n = sc.nextInt();
		  int[] a = new int[n];
		  for(int i=0;i<n;++i) a[i] = sc.nextInt();
		  if(n == 1) out.println(-1);
		  else {
			  if(check(a,2)) out.println(2);
			  else if(check(a,3)) out.println(3);
			  else if(check(a,n)) out.println(n);
			  else out.println(-1);
		  }
		}
		
		
	out.close();
	}
	private static boolean check(int[] a, int k) {
		Map<Integer,Integer> map = new HashMap<>();
		for(int i=0;i<k;++i) {
			add(map,a[i]);
		}
		if(dom(map)) return true;
		for(int i=k;i<a.length;++i) {
			delete(map,a[i-k]);
			add(map,a[i]);
			if(dom(map)) return true;
		}
		
		return false;
	}
	
	private static boolean dom(Map<Integer, Integer> map) {
		int cnt = 0;
		for(int i : map.keySet()) cnt = Math.max(cnt, map.get(i));
		int freq = 0;
		for(int i : map.keySet()) if(map.get(i) == cnt) ++freq;
		return freq == 1;
	}
	private static void add(Map<Integer, Integer> map, int i) {
		// TODO Auto-generated method stub
		if(map.containsKey(i)) map.put(i, map.get(i)+1);
		else map.put(i, 1);
		
	}
	private static void delete(Map<Integer, Integer> map, int i) {
		if(map.containsKey(i)) {
			if(map.get(i) == 1) map.remove(i);
			else map.put(i, map.get(i)-1);
		}
		
	}
}