import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.*;
public class Main
{

    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;
        }
    }
    static long power(long x, long y, long p)
    {

        long res = 1;

        x = x % p;

        while (y > 0)
        {

            if((y & 1)==1)
                res = (res * x) % p;


            y = y >> 1;
            x = (x * x) % p;
        }
        return res;
    }

    public static void main(String[] args)
    {
        FastReader sc=new FastReader();
        int t=sc.nextInt();
        while(t-->0)
        {
          int n=sc.nextInt();
          int a[]=new int[n];
          int freq[]=new int[n+1];
          Arrays.fill(freq,-1);
          for( int i=0;i<n;i++)
          {
            a[i]=sc.nextInt();
          }
          if(n==1)
          System.out.println(-1);
          else
          {
            int min=n+1;int val=n;
            for( int i=0;i<n;i++)
            {

              if(freq[a[i]]!=-1)
              {
                val=i-freq[a[i]]+1;
              }
              if(val<min)
              {
                min=val;
              }
              freq[a[i]]=i;
            }
            if(min>n)
            System.out.println(-1);
            else
            System.out.println(min);
          }
        }

    }

}
class Pair
{
  long val; String str;
  Pair(long val, String str)
  {
    this.val=val;
    this.str=str;
  }
}
class myComparator implements Comparator<Pair>
{
  public int compare( Pair a, Pair b)
  {
    if(a.val>b.val)
    {
      return 1;
    }
    else if(b.val>a.val)
    {
      return -1;
    }
    else
    return 0;
  }
}
