import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class C {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine().trim());
        while (T-- > 0) {
            int n = Integer.parseInt(br.readLine().trim());
            if (n == 1) {
                br.readLine();
                System.out.println("-1");
            } else {
                Number[] arr = new Number[n];
                StringTokenizer st = new StringTokenizer(br.readLine().trim());
                for (int i = 0; i < n; i++) {
                    arr[i] = new Number(Integer.parseInt(st.nextToken().trim()), i);
                }
                Arrays.sort(arr);
                int min = Integer.MAX_VALUE;
                for (int i = 0; i < n - 1; i++) {
                    if (arr[i].value == arr[i + 1].value) {
                        int dist = arr[i + 1].index - arr[i].index + 1;
                        min = Math.min(min, dist);
                    }
                }
                System.out.println(min == Integer.MAX_VALUE ? -1 : min);
            }
        }
    }

    static class Number implements Comparable<Number> {
        int value;
        int index;

        public Number(int value, int index) {
            this.value = value;
            this.index = index;
        }

        @Override
        public int compareTo(Number n) {
            int ret = value - n.value;
            if (ret == 0) {
                ret = index - n.index;
            }
            return ret;
        }
    }
}
