import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
import java.util.StringTokenizer;

public class MAIN
{
    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            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;
        }
    }
    static class number implements Comparator<number> {
        int index;
        int value;

        @Override
        public int compare(number o1, number o2) {
            if(o1.value<o2.value){
                return -1;
            }
            return 0;
        }
    }

    public static void main(String[] args)
    {
        FastReader sc=new FastReader();
        int testcase = sc.nextInt();
        for(int t=0; t<testcase; t++) {
            int n = sc.nextInt();
            number a[] = new number[n];
            int minlength = Integer.MAX_VALUE;
            for (int i = 0; i < n; i++) {
                a[i] = new number();
                a[i].index = i;
                a[i].value = sc.nextInt();
            }
            Comparator<number> com = new number();
            Arrays.sort(a, com);
            if (n < 2) {
                System.out.println(-1);
            } else {
                for (int i = 1; i < n; i++) {
                    if (a[i].value == a[i - 1].value && Math.abs(a[i].index - a[i - 1].index+1) < minlength) {
                        minlength = Math.abs(a[i].index - a[i - 1].index)+1;
                    }
                }

            if (minlength != Integer.MAX_VALUE) {
                System.out.println(minlength);
            } else {
                System.out.println(-1);
            }
        }
          }
        }

    }

