import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.StringTokenizer;

public class Test3 {
	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static void main(String args[]) {
		FastReader f = new FastReader();
		int T = f.nextInt();
		for(int k=0; k<T; k++) {
			int N = f.nextInt();
			int A[] = new int[N];
			for(int i=0; i<N; i++) {
				A[i] = f.nextInt();
			}
			int min = N;
			HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
			boolean flag = false;
			for(int i=0; i<N; i++) {
				if(map.containsKey(A[i])) {
					flag = true;
					int d = i - map.get(A[i]) + 1;
					if(d < min) {
						min = d;
					}
					map.put(A[i], i);
				} else {
					map.put(A[i], i);
				}
			}
			if(!flag)
				System.out.println(-1);
			else
				System.out.println(min);
		}
	}
}
