
import java.math.*;
import java.text.*;

import java.util.*;
import javafx.util.*;

public class Solver {

    public static Scanner in = new Scanner(System.in);

    public static int getMax(int[] z) {
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        int ans = -1;
        int max = 0;
        for (int val : z) {
            if (map.containsKey(val)) {
                map.put(val, map.get(val) + 1);
                max = Math.max(max, map.get(val));
            } else {
                map.put(val, 1);
            }
        }
        Iterator<Integer> ptr = map.keySet().iterator();
        while (ptr.hasNext()) {
            int key = ptr.next();
            int val = map.get(key);
            if (val == max) {
                if (ans != -1) {
                    ans = -1;
                    break;
                } else {
                    ans = key;
                }
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        int T = in.nextInt();
        while (T-- > 0) {
            int n = in.nextInt();
            int[] z = new int[n];
            for (int i = 0; i < n; i++) {
                z[i] = in.nextInt();
            }
            int ans = getMax(z);
            if (ans == -1) {
                System.out.println("-1");
            } else {
                ArrayList<Integer> pos = new ArrayList<Integer>();
                for (int i = 0; i < n; i++) {
                    if (z[i] == ans) {
                        pos.add((i + 1));
                    }
                }
                Collections.sort(pos);
                int min = Integer.MAX_VALUE;
                for (int i = 0; i < pos.size() - 1; i++) {
                    int dif = Math.abs(pos.get(i) - pos.get(i + 1)) + 1;
                    min = Math.min(min, dif);
                }
                System.out.println(min);
            }
        }
    }

}
