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

public class HelloWorld
{
  public static void main(String[] args) throws Exception
  {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter writer = new PrintWriter(System.out);
    int T = Integer.parseInt(br.readLine());
    while(T-- > 0)
    {
      int n = Integer.parseInt(br.readLine());
      StringTokenizer st = new StringTokenizer(br.readLine());
      if(n == 1)
      {
        st.nextToken();
        writer.println(-1);
        continue;
      }
      int[] A = new int[n];
      HashMap<Integer, Integer> hm = new HashMap<>();
      for(int i = 0; i < n; i++) A[i] = Integer.parseInt(st.nextToken());
      int ans = Integer.MAX_VALUE;
      int i;
      for(i = 0; i < n; i++)
      {
        if(hm.containsKey(A[i]))
        {
          if(ans > i-hm.get(A[i])+1) ans = i-hm.get(A[i])+1;
          hm = new HashMap<>();
          hm.put(A[i],i);
        }
        else hm.put(A[i],i);
      }
      if(ans == Integer.MAX_VALUE) writer.println(-1);
      else writer.println(ans);
    }
    br.close();
    writer.close();
  }
}
