import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class CF1257C {
    public static void main(String[] args) {
        FastReader input = new FastReader();
        int t = input.nextInt();
        while(t-->0) {
            int n = input.nextInt();
            int[] arr = new int[n+1];
            for(int i=0;i<arr.length;i++)
                arr[i]=-1;

            int ans = Integer.MAX_VALUE;
            for(int i=0;i<n;i++) {
                int x = input.nextInt();
                if(arr[x]!=-1) {
                    ans = Math.min(ans, i-arr[x]+1);
                }
                arr[x]=i;
            }

            if(ans==Integer.MAX_VALUE) {
                System.out.println(-1);
            }
            else {
                System.out.println(ans);
            }
        }
    }
    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;
        }
    }
}
