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];
            Map<Integer,Integer> map = new HashMap<Integer,Integer>();
            for(int i = 0;i < n;i++){
                arr[i] = input.nextInt();
                map.put(arr[i],i);
            }
            int ans = Integer.MAX_VALUE;
            int res = 0;
            boolean con = false;
            for(int i = 0;i < n;i++){
                res = map.get(arr[i]) - i + 1;
               // System.out.println("res : " + res);
                if(res > 1){
                    con = true;
                    ans = Math.min(ans,res);
                }
            }
            if(con)
            System.out.println(ans);
            else{
                System.out.println(-1);
            }
            t--;
        }
    }
    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;
        }
    }
}
