import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    private int n;
    private ArrayList<Integer> a, newarr, otv;
    Scanner scanner;

    int solve(){
        int ret = n+5;
        for (int i = 0; i < n; i++) {
            if (newarr.get(a.get(i))!=-1){
                ret = Math.min(ret, i-newarr.get(a.get(i))+1);
                newarr.set(a.get(i),i);
            } else {
                newarr.set(a.get(i),i);
            }
        }
        if (ret>n)ret=-1;
        return ret;
    }




    void fill(){
        n = scanner.nextInt();
        newarr = new ArrayList<>(n+1);
        a = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            a.add(scanner.nextInt());
            newarr.add(-1);
        }
        newarr.add(-1);
    }

    public Main(){
        scanner = new Scanner(System.in);
        otv = new ArrayList<>();
        int t = scanner.nextInt();
        for (int i = 0; i < t; i++) {
            fill();
            otv.add(solve());
        }
        for (Integer inte: otv) {
            System.out.println(inte);
        }

    }
    public static void main(String[] args) {
     new Main();
    }
}
