
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class DominatedSubarray {

    public static void main(String[] args) {
        Scanner scanner = new Scanner();

        int t = scanner.nextInt();

        while(t > 0) {
            int n = scanner.nextInt();
            int[] indexArr = new int[n+1];
            int minLength = n+1;

            for(int i=0; i<n; i++) {
                int element = scanner.nextInt();

                if(indexArr[element] > 0) {
                    int diff = i+1 - indexArr[element];
                    if(diff < minLength)
                        minLength = diff+1;
                    indexArr[element] = i+1;

                } else {
                    indexArr[element] = i+1;
                }

            }

            if(minLength > n)
                System.out.println("-1");
            else
                System.out.println(minLength);
            t--;
        }
    }

    private static class Scanner {
        private BufferedReader br;
        private StringTokenizer st;

        Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); }

        String next() {
            if(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;
        }
    }
}

