import javax.sound.midi.Soundbank;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {

    static StringBuilder out;

    public static void main(String[] args) throws Exception {
        FastReader read = new FastReader();
        int t = read.nextInt();
        StringBuilder out = new StringBuilder();
        while (t-- > 0) {
            int n = read.nextInt();
            int arr[] = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = read.nextInt();
            }

            int ans = findTheAnswer(arr);
            out.append(ans).append('\n');
        }
        System.out.print(out);

    }


    public static int findTheAnswer(int arr[]) {
        int mn = 0, mx = arr.length, md = (mn + mx) / 2;
        int best = Integer.MAX_VALUE;
        while (mn <= mx) {
            md = (mn + mx) / 2;
//            System.out.println(md + " ");
            int max = 0;
            boolean dup = false;
            HashMap<Integer, Integer> mp = new HashMap<>();
            for (int i = 0; i + md <= arr.length; i++) {
//                System.out.println(i + " " + (i + md));
                mp.clear();
                max = 0;
                dup = false;
                for (int j = i; j < i + md; j++) {
                    if (mp.containsKey(arr[j])) {
                        int ocr = mp.get(arr[j]) + 1;
                        if (ocr == max) {
                            dup = true;
                        } else if (ocr > max) {
                            max = ocr;
                            dup = false;
                        }
                        mp.put(arr[j], ocr);
                    } else {
                        mp.put(arr[j], 1);
                    }
                }
//                System.out.println(max + " " + dup);
                if (max > 1 && !dup) {
                    break;
                }
            }
//            System.out.println("mx " + max + " " + dup);
            if (max > 1 && !dup) {
                best = Math.min(best, md);
                mx = md - 1;
            } else {
                mn = md + 1;
            }
        }
        return  best < Integer.MAX_VALUE ? best : -1;
    }

    public static boolean isPrime(long n) {
        for (long i = 2; i * i <= n; i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }


}

class Pair implements Comparable<Pair> {
    int x;
    int y;

    public Pair(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public int compareTo(Pair p) {
        if (this.x != p.x) {
            return this.x - p.x;
        }
        return this.y - p.y;
    }
}

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;
    }
}
