/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc = new Scanner(System.in);
		int t;
		t = sc.nextInt();
		while(t>0)
		{
			t--;
			int n = sc.nextInt();
			if(n == 1)
			{
				System.out.println(-1);
				int a = sc.nextInt();
			}
			else
			{
				ArrayList<Integer> a[] = new ArrayList[200001];
				for(int i = 0;i<=n;i++)
				{
					a[i] = new ArrayList();
				}
				for(int i = 0;i<n;i++)
				{
					int x = sc.nextInt();
					a[x].add(i);
				}
				int min = 2147483647;
				for(int i = 0;i<=n;i++)
				{
					if(a[i].size()>0)
					{
						for(int j = 0;j<a[i].size()-1;j++)
						{
							if(min>(a[i].get(j+1)-a[i].get(j)))
							{
								min = a[i].get(j+1) - a[i].get(j);
							}
						}
					}
				}
				if(min == 2147483647)
				{
					System.out.println("-1");
				}
				else
				{
					System.out.println(min+1);
				}
			}
		}
	}
}