/*
If you want to aim high, aim high
Don't let that studying and grades consume you
Just live life young
******************************
*/
import java.util.*;
import java.io.*;

   public class C2
   {
      public static void main(String args[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));  
         StringTokenizer st = new StringTokenizer(infile.readLine());
         int T = Integer.parseInt(st.nextToken());
         for(int qw=0; qw < T; qw++)
         {
            st = new StringTokenizer(infile.readLine());
            int N = Integer.parseInt(st.nextToken());
            int[] arr = new int[N];
            st = new StringTokenizer(infile.readLine());
            for(int i=0; i < N; i++)
               arr[i] = Integer.parseInt(st.nextToken());
            if(N == 1)
            {
               System.out.println(-1);
               continue;
            }
            boolean trivial = false;
            for(int i=0; i < N-1; i++)
               if(arr[i] == arr[i+1])
               {
                  System.out.println(2);
                  trivial = true;
                  break;
               }
            if(trivial)
               continue;
            //maps
            HashSet<Integer> set = new HashSet<Integer>();
            int left = 0;
            int right = 2;
            set.add(arr[0]);
            set.add(arr[1]);
            int res = N+1;
            while(right < N)
            {
               int valr = arr[right];
               if(set.contains(valr))
               {
                  while(arr[left] != valr)
                  {
                     set.remove(arr[left]);
                     left++;
                  }
                  res = Math.min(res, right-left+1);
                  
                  left++;
               }
               else
                  set.add(valr);
               right++;
            }
            if(res == N+1)
               res = -1;
            System.out.println(res);
         }
      }
      public static boolean works(int N, int[] arr, int K)
      {
         int[] count = new int[N+1];
         int max = 0;
         for(int x: arr)
         {
            count[x]++;
            max = Math.max(max, count[x]);
         }
         HashSet<Integer> set = new HashSet<Integer>();
         for(int x: arr)
            if(count[x] == max)
               set.add(x);
         if(set.size() == 1)
            return true;
         //caseworkish
         for(int i=K; i < N; i++)
         {
            int left = arr[i-K];
            if(set.size() == 1 && count[left] == max)
            {
               set.clear();
               max--;
            }
            count[left]--;
            int right = arr[i];
            if(count[right] == max)
            {
               set.clear();
               set.add(right);
               max++;
            }
            else if(count[right] == max-1)
               set.add(right);
            count[right]++;
            if(set.size() == 1)
               return true;
         } 
         return false;
      }
   }