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

public class problem2 {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		for(int x=0;x<t;x++) {
			int n = Integer.parseInt(br.readLine());
			StringTokenizer st = new StringTokenizer(br.readLine());
			int arr[] = new int[n];
			for(int i=0;i<n;i++) {
				arr[i] = Integer.parseInt(st.nextToken());
			}
			int ans = Integer.MAX_VALUE;
			int last[] = new int[n+1];
			for(int i=0;i<=n;i++)last[i] = -1;
			for(int i=0;i<n;i++) {
				if(last[arr[i]]!= -1) {
					ans = Math.min(ans, i-last[arr[i]]+1);
				}
				last[arr[i]] = i;
			}
			if(ans>n)System.out.println(-1);
			else System.out.println(ans);
		}
	}
}