
import java.io.*;
import java.util.*;


public class Main {

    static StringBuilder data = new StringBuilder();
    final static FastReader in = new FastReader();


    public static void main(String[] args) {
        int t = in.inT(), n, g;
        int[] a;
        int[] b;
        int max = 0;
        int d;
        boolean dom;
        int s,e;
        int min;
        for (int i = 0; i < t; i++) {
            n = in.inT();
            a = new int[n ];
            b = new int[n+1];
            dom=false;
            for (int j = 0; j < n; j++) {
               a[j]=in.inT();
               b[a[j]]++;

            }
            s=-1;
            d=-1;
            max=0;
            if(n==1){
                data.append(-1).append('\n');
            }else{
                for (int j = 1; j <= n; j++) {
                    if(b[j]>max){
                        max=b[j];
                        d=j;
                        dom=false;
                    }else if(b[j]==max){
                        dom=true;
                    }
                }
                if(dom){
                    data.append(-1).append('\n');
                }else{
                    min=n;
                    for (int j = 0; j < n; j++) {
                        if(a[j]==d&&s==-1){
                            s=j+1;
                        }else if(a[j]==d){
                           min=Math.min(((j+1)-s)+1,min);
                            s=j+1;
                        }
                    }
                    data.append(min).append('\n');
                }
            }
        }
        System.out.println(data);
    }


    static void fileOut(String s) {
        File out = new File("output.txt");
        try {
            FileWriter fw = new FileWriter(out);
            fw.write(s);
            fw.flush();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        public FastReader(String path) {
            try {
                br = new BufferedReader(new
                        InputStreamReader(new FileInputStream(path)));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int inT() {
            return Integer.parseInt(next());
        }

        long lonG() {
            return Long.parseLong(next());
        }

        float floaT() {
            return Float.parseFloat(next());
        }

        double doublE() {
            return Double.parseDouble(next());
        }


        String line() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }

}