import java.util.*;

/**
 * @Author: Harry
 * @Date: 2019-11-12
 * @Version 1.0
 */
public class Main {

    static int arr[],t,n,arr2[];

//    static int a,b,S;


    static int ans = 0;

    public Main() {
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);


        while(input.hasNext()){
            t = input.nextInt();
            while(t-- > 0){
                n = input.nextInt();
                Set<Integer> set = new HashSet<>();
                Map<Integer, List<Integer>> map = new HashMap<>();


                for(int i = 0;i < n;i++){
                    int x = input.nextInt();
                    set.add(x);

                    if(map.containsKey(x)){
                        List<Integer> list = map.get(x);
                        list.add(i);
                    }
                    else{
                        List<Integer> list = new ArrayList<>();
                        list.add(i);
                        map.put(x,list);
                    }
                }

                if(n == 1){
                    System.out.println(-1);
                }
                else{
                    if(!judge(map)){
                        System.out.println(-1);
                        continue;
                    }

                    int ans = 999999999;

                    for(int i : map.keySet()){
                        List<Integer> list = map.get(i);

                        for(int j = 1;j < list.size();j++){
                            ans = Math.min(ans,list.get(j) - list.get(j - 1) + 1);
                        }
                    }

                    System.out.println(ans);
                }
            }
        }



    }

    public static boolean judge(Map<Integer, List<Integer>> map){
        int counter = 0;
        for(int i : map.keySet()){
            List<Integer> list = map.get(i);

            if(list.size() == 1){
                counter++;
            }
        }

        return map.size() != counter;
    }













}