/* 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{
        Scanner input = new Scanner(System.in);
        int t = input.nextInt();
        for(int i=0;i<t;i++){
            input.nextLine();
            int n = input.nextInt();
            input.nextLine();
            int[] nums = new int[n+1];
            int max = 0;
            for(int j=1;j<=n;j++){
                nums[j] = input.nextInt();
                max = Math.max(max, nums[j]);
            }
            ArrayList<ArrayList<Integer>> f = new ArrayList<ArrayList<Integer>>();
            for(int j=0;j<=max;j++){
                ArrayList<Integer> e = new ArrayList<Integer>();
                f.add(e);
            }
            for(int j=1;j<=n;j++){
                f.get(nums[j]).add(j);
            }
            boolean res = false;
            int min = Integer.MAX_VALUE;
            for(int j=1;j<f.size();j++){
                if(f.get(j).size()<=1){
                    continue;
                }
                for(int k = 0;k<f.get(j).size()-1;k++){
                    res = true;
                    min = Math.min(min,f.get(j).get(k+1)-f.get(j).get(k)+1);
                }
            }
            if(res==false){
                System.out.println("-1");
            }else{
                System.out.println(min);
            }
        }
    }
}