import java.io.*;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class DominatedSubarray {
	public static void main(String[] args) throws IOException {
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int t = Integer.parseInt(br.readLine());
		
		for(int i = 0; i < t; i++) {
			int n = Integer.parseInt(br.readLine());
			int[] arr = new int[n];
			String[] line = br.readLine().split(" ");
			
			for(int j = 0; j < n; j++) { 
				arr[j] = Integer.parseInt(line[j]);
			}
			
			int ans = computeMinDominatedSubArray(arr);
			bw.write(String.valueOf(ans));
			
			bw.newLine();
		}
		
		bw.close();
		br.close();
	}

	private static int computeMinDominatedSubArray(int[] arr) {
		if(arr.length <= 1) {
			return -1;
		}
		int[] map = new int[arr.length + 1];
		int ptr1 = 0, ptr2 = 0;
		int max = 1;
		int maxValue = -1;
		int min = Integer.MAX_VALUE;
		while(ptr1 < arr.length && ptr2 < arr.length) {
			int i;
			boolean breaked = false;
			for(i = ptr2; i < arr.length; i++) {
				++map[arr[i]];
				if(map[arr[i]] > max) {
					if(min > i - ptr1 + 1) {
						min = i - ptr1 + 1;
					}
					max = map[arr[i]];
					maxValue = arr[i];
					breaked = true;
					break;
				}
			}
			if(!breaked) {
				break;
			}
			ptr2 = i + 1;
			
			for(i = ptr1; arr[i] != maxValue; i++) {
				--map[arr[i]];

			}
			--max;
			--map[arr[i]];
			ptr1 = i + 1;
			if(min > ptr2 - ptr1 + 1) {
				min = ptr2 - i;
			} 
		}
		return min == Integer.MAX_VALUE ? -1 : min;
	}
	
	private static int[][] calculateDominators(int[] arr) {
		HashMap<Integer, Integer> hashmap = new HashMap<Integer, Integer>();
		
		for(int x : arr) {
			if(hashmap.containsKey((Integer) x)) {
				hashmap.put(new Integer(x), new Integer(hashmap.get((Integer) x) + 1));
			}
			else {
				hashmap.put(new Integer(x), new Integer(1));
			}
		}
		
		int[][] map = new int[hashmap.size()][2];
		Set<Entry<Integer, Integer>> it = hashmap.entrySet();
		int i = 0;
		for(Entry<Integer, Integer> entry : it) {
			map[i][0] = entry.getKey();
			map[i++][1] = entry.getValue();
		}
		mergeSort(map);
		return map;
	}

	private static int[] calculateDominatorsMap(int[][] count, int n) {
		int[] map = new int[n];
		for(int i = 0; i < map.length; i++) {
			map[i] = -1;
		}
		for(int i = 0; i < count.length; i++) {
			map[count[i][0]] = i;
		}
		return map;
	}
	
	private static void mergeSort(int[][] arr){ 
		if(arr.length <= 1) {
			return;
		}
		
		int[][] l = new int[arr.length / 2][arr[0].length];
		int[][] r = new int[arr.length - (arr.length / 2)][arr[0].length];
		
		
		for(int i = 0; i < l.length; i++) {
			l[i] = arr[i];
		}
		
		for(int i = 0; i < r.length; i++) {
			r[i] = arr[i + l.length];
		}
		
		
		mergeSort(l);
		mergeSort(r);
		int lptr = 0;
		int rptr = 0;
		for(int i = 0; i < arr.length; i++) {
			if(lptr >= l.length) {
				arr[i] = r[rptr++]; 
			}
			else if(rptr >= r.length) {
				arr[i] = l[lptr++];
			}
			else {
				if(l[lptr][1] > r[rptr][1]) {
					arr[i] = l[lptr++];
				}
				else {
					arr[i] = r[rptr++];
				}
			}
		}
	}
	
}
