//package nov19;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class Education76C {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		FastReader scn = new FastReader();
		PrintWriter out = new PrintWriter(System.out);
		int t = scn.nextInt();
		for (int m = 0; m < t; m++) {
			PriorityQueue<Integer> pq=new PriorityQueue<>();
			HashMap<Integer, PriorityQueue<Integer>> map = new HashMap<>();
			int n = scn.nextInt();
			int[] arr = new int[n];
			for(int i=0; i<n; i++)
			{
				arr[i]=scn.nextInt();
				
			}
			int min=Integer.MAX_VALUE;
			
			for(int i=0; i<n; i++)
			{
				if(map.containsKey(arr[i]))
				{
					PriorityQueue<Integer> p=map.get(arr[i]);
					p.add(i+1);
					map.put(arr[i], p);
				}
				else
				{
					pq=new PriorityQueue<>();
					pq.add(i+1);
					map.put(arr[i], pq);
				}
			}
			for(Integer x1: map.keySet())
			{
				PriorityQueue<Integer> q=map.get(x1);
				int val=q.poll();
				while(q.isEmpty()==false)
				{
					int val2=q.poll();
					int diff=val2-val+1;
					if(diff<min)
					{
						min=diff;
					}
					val=val2;
				}
			}
			if(min==Integer.MAX_VALUE)
			{
				
				System.out.println(-1);
			}
			else
			{
				System.out.println(min);
			}
	
		}
		out.close();

	}

	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;
		}
	}

}
