import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.*;

public class MainC {
    public static void main(String[] args) {
        MyScanner scanner = new MyScanner();

        int q = scanner.nextInt();
        for(int i = 0;i<q;i++){
            int n = scanner.nextInt();
            int[] array = new int[n];
            Map<Integer,List<Integer>> map = new HashMap<>();
            for(int j = 0;j<n;j++){
                array[j] = scanner.nextInt();
                List<Integer> list = map.getOrDefault(array[j], new ArrayList<>());
                list.add(j);
                if(list.size()==1){
                    map.put(array[j],list);
                }

            }

            int min = Integer.MAX_VALUE;
            for(Map.Entry<Integer,List<Integer>> entry : map.entrySet()){
                List<Integer> list = entry.getValue();
                for(int j = 0;j<list.size()-1;j++){
                    int dis = list.get(j+1)-list.get(j) + 1;
                    if(dis < min){
                        min = dis;
                    }
                }
            }

            if(min == Integer.MAX_VALUE){
                System.out.println("-1");
            }else{
                System.out.println(min);
            }
        }
    }

    // -----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        public MyScanner() {
            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;
        }

    }
    // --------------------------------------------------------
}
