import javax.print.attribute.HashAttributeSet;
import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.attribute.FileAttribute;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.*;
import java.util.Arrays;

public class Main {


    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

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

    public static void main(String[] args) {
        FastReader sc = new FastReader();
        int t = sc.nextInt();
        for (int i = 0; i < t; i++) {
            int n = sc.nextInt();
            int[] arr = new int[n];

            for (int j = 0; j < n; j++) {
                arr[j] = sc.nextInt();
            }
            if (n == 1) {
                System.out.println(-1);
                continue;
            }
            HashMap<Integer,Integer> map = new HashMap<>();
            int min = Integer.MAX_VALUE;
            int flag = 0;
            for (int j = 0; j < n; j++) {
                int x = arr[j];
               if(!map.containsKey(x)){
                   map.put(x,j);
               }
               else {
                   flag = 1;
                   int index = map.get(x);
                   map.put(x,j);
                   int diff = j - index + 1;
                   if (diff < min) {
                       min = diff;

                   }
               }

            }


            if(flag==0){
                System.out.println(-1);
            }
            else {
                System.out.println(min);
            }

        }

    }
}
