/* بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ */
//codeforces1257C
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.Math.*;
import java.math.*;

public class Main{

	static PrintWriter go = new PrintWriter(System.out);

	public static void main(String args[]) throws IOException,FileNotFoundException {
		BufferedReader gi = new BufferedReader(new InputStreamReader(System.in, "ISO-8859-1"));
		int t = Integer.parseInt(gi.readLine());
		for ( int j = 0; j < t; j++ ){
			int n = Integer.parseInt(gi.readLine());
			int[] a = parseArray(gi);
			Map<Integer, Integer> m = new HashMap<>();
			int ans = n + 1;
			for ( int k = 0; k < n; k++ ){
				if (m.containsKey(a[k])) 
					ans = min(ans, k - m.get(a[k]) + 1);
				m.put(a[k], k);
			}
			if (ans == n + 1) ans = -1;
			go.println(ans);
		}
		go.close();
	}

	static int[] parseArray(BufferedReader gi) throws IOException{
		String[] line = gi.readLine().split(" ");
		int[] rez = new int[line.length];
		for ( int k = 0; k < line.length; k++){
			rez[k] =  Integer.parseInt(line[k]);
		}
		return rez;
	}

}