
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

// Attempt for a more efficient solution for C !!

public class CC {
    public static void main(String[] args) {
        FastReader s = new FastReader();

        int T = s.nextInt();
        StringBuilder res = new StringBuilder();

        for (int i = 0; i < T; i++) {

            int n = s.nextInt();
            int[] arr = new int[n];
            for (int j = 0; j < n; j++) {
                arr[j]=s.nextInt();
            }

            //special case //
            if(n==1){ res.append(-1).append("\n"); continue;}

            // else ... //
            // search all possibilities using method below
            // starting from length 2 , up until length n !
            int answer = shortestDominatedSubArr(arr,n);

            res.append(answer);
            res.append("\n");
        }
        System.out.println(res);
    }

    private static int shortestDominatedSubArr(int[] arr, int n)
    {
        if(arr.length==1) return -1;
        else if(arr.length==2 && arr[0]==arr[1]) return 2;
        else if(arr.length==2) return -1;

        int mid = (arr.length)/2;
        int[] left = copyArr(0,mid,arr);
        int[] right = copyArr(mid+1,arr.length-1,arr);
        int lenLeft = shortestDominatedSubArr(left,n);
        int lenRight = shortestDominatedSubArr(right,n);

        if(lenLeft!=-1 && lenRight!=-1) return Math.min(lenLeft,lenRight);
        else
        {
            int tmp = shortestDominatedSubArrLen(0,arr.length-1,arr,n);
            if(tmp==-1)
            {
                if(lenLeft==-1 && lenRight==-1) return -1;
                else if(lenLeft==-1) return lenRight;
                else return lenLeft;
            }
            else
            {
                if(lenLeft==-1 && lenRight==-1) return tmp;
                else if(lenLeft==-1) return Math.min(lenRight,tmp);
                else return Math.min(lenLeft,tmp);
            }
        }
    }

    private static int[] copyArr(int l, int r, int[] arr) {
        int[] res = new int[r-l+1];
        for (int i = l,k=0; i <= r; i++,k++) {
            res[k] = arr[i];
        }
        return res;
    }

    private static int shortestDominatedSubArrLen(int l, int r, int[] arr,int n)
    {
        // if dominated >> returns length
        // if not >> returns -1

        int[] counters = new int[n+1];
        for (int i = l; i <=r ; i++) counters[arr[i]]++;

        int maxCounterValue = getMax(counters);

        if(maxCounterValue==-1) return -1;
        else
        {
            // find shortest one !
            ArrayList<Integer> indices = new ArrayList<>();
            for (int i = l; i <=r ; i++) {
                if(arr[i]==maxCounterValue) indices.add(i);
            }

            int minDistance = indices.get(indices.size()-1)-indices.get(0)+1;
            for (int i = 0; i < indices.size()-1; i++) {
                for (int j = i+1; j < indices.size(); j++) {
                    int dist=indices.get(j)-indices.get(i)+1;
                    if(dist<minDistance) minDistance=dist;
                }
            }
            return minDistance;
        }
    }

    private static int getMax(int[] counters) {
        int maxCounter=-1,maxCounterValue=-1;
        for (int i = 0; i < counters.length; i++) {
            if(maxCounter<counters[i])
            {
                maxCounter=counters[i];
                maxCounterValue=i;
            }
        }

        // check for second max !
        for (int i = 0; i < counters.length; i++) {
            if(i!=maxCounterValue)
            {
                if(counters[i]==maxCounter) return -1;
            }
        }

        return maxCounterValue;
    }


    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;
        }
    }
}
