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

import java.util.HashMap;
import java.util.ArrayList;

public class Arr {
    public static void main(String[] args) {
        FastIO sc = new FastIO();
        int num = sc.nextInt();
        int length;
        HashMap<Integer, ArrayList<Integer>> mp;
        int val;
        ArrayList<Integer> arr;
        int size;
        int kk;
        boolean eq;
        
        for (int i = 0; i < num; i++) {
            eq = false;
            length = sc.nextInt();
            mp = new HashMap<Integer, ArrayList<Integer>>();
            if (length == 1) {
                sc.println(-1);
                sc.nextInt();
                continue;
            }
            for (int j = 0; j < length; j++) {
                val = sc.nextInt();
                if (mp.containsKey(val)){
                    arr = mp.get(val);
                    arr.add(j);
                    mp.put(val, arr);
                } else {
                    arr = new ArrayList<Integer>();
                    arr.add(j);
                    mp.put(val, arr);
                }
            }
            size = 1;
            kk = 0;
            for (Integer x : mp.keySet()) {
                if (mp.get(x).size() > size) {
                    kk = x;
                    size = mp.get(x).size();
                    eq = false;
                } else if (mp.get(x).size() == size) {
                    eq = true;
                }
            }
            
            if (eq || size == 1) {
                sc.println(-1);
            } else {
                int diff = length;
                arr = mp.get(kk);
                for (int k = 0; k < arr.size() - 1; k++) {
                    diff = Math.min(diff, arr.get(k+1) - arr.get(k) + 1);
                }
                sc.println(diff);
            }
        }
        sc.close();
    }
}


class FastIO extends PrintWriter 
{ 
    BufferedReader br; 
    StringTokenizer st;

    public FastIO() 
    { 
        super(new BufferedOutputStream(System.out)); 
        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; 
    } 
}