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

public class p1257C {

	public void realMain() throws Exception {

		BufferedReader fin = new BufferedReader(new InputStreamReader(System.in), 1000000);

		String in = fin.readLine();

		String[] ar = in.split(" ");

		int T = Integer.parseInt(ar[0]);
	for(int t = 0; t < T; t++) {
		

			int ret2 = 0;
			boolean dig2 = false;
			for (int ch = 0; (ch = fin.read()) != -1; ) {
        			if (ch >= '0' && ch <= '9') {
            				dig2 = true;
           				ret2 = ret2 * 10 + ch - '0';
        			} else if (dig2) break;
    			}

			int n = ret2;
			

		int[] s = new int[n];

		for(int i = 0; i < n; i++) {
			int ret = 0;
			boolean dig = false;
			for (int ch = 0; (ch = fin.read()) != -1; ) {
        			if (ch >= '0' && ch <= '9') {
            				dig = true;
           				ret = ret * 10 + ch - '0';
        			} else if (dig) break;
    			}

			s[i] = ret;
			
		}

		int[] pos = new int[n + 1];

		for(int i = 0; i < n + 1; i++) {
			pos[i] = -1;
		}

		int min = Integer.MAX_VALUE;

		for(int i = 0; i < n; i++) {
			if(pos[ s[i] ] != -1) {
				min = Math.min(min, i - pos[ s[i] ]);
			}
			pos[ s[i] ] = i;
		}

		if(min == Integer.MAX_VALUE) {
			System.out.println("-1");
		} else {
			System.out.println(min + 1);
		}
	}



	}


	public static void main(String[] args) throws Exception {
		p1257C a = new p1257C();
		a.realMain();
	}
}