import java.io.*;
import java.util.*;
import java.math.*;
     
public class Main{
	public static void main(String[] args) throws Exception {
	IO io = new IO();
	PrintWriter out = new PrintWriter(System.out);
	Solver sr = new Solver();
	sr.solve(io,out);
	out.flush();
	out.close();
    	}

	static class Solver
	{
	    void solve(IO io, PrintWriter out)
	    {
           int i, j;
           int t=io.nextInt();
           while(t-->0)
           {
               int n=io.nextInt();
               HashMap<Integer,ArrayList<Integer>> hp = new HashMap<>();
               for(i=0;i<n;i++)
               {
                   int tmp = io.nextInt();
                   if(hp.containsKey(tmp))
                    {
                        ArrayList<Integer> ar = hp.get(tmp);
                        ar.add(i);
                    }
                    else
                    {
                        ArrayList<Integer> ar = new ArrayList<>();
                        ar.add(i);
                        hp.put(tmp,ar);
                    }
                        
               }
               
               int dist = Integer.MAX_VALUE;
               for(int num: hp.keySet())
               {
                   if(hp.get(num).size()>1)
                   {
                       ArrayList<Integer> ar = hp.get(num);
                       for(i=0 ; i<ar.size()-1; i++)
                       {
                           dist = Math.min(dist, ar.get(i+1)-ar.get(i)+1);
                       }
                   }
               }
               if(dist==Integer.MAX_VALUE)
                out.println("-1");
                else
                    out.println(dist);
           }
           
	    }
    	
	}
	//Special thanks to Petr (on codeforces) who inspired me to implement IO class!
	static class IO
	{
    	BufferedReader reader;
        StringTokenizer tokenizer;
    	public IO() {
                reader = new BufferedReader(new InputStreamReader(System.in));
                tokenizer = null;
            }
     
        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        public String nextLine() {
            String s="";
            try {
                    s=reader.readLine();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            return s;
        }
        public int nextInt() {
            return Integer.parseInt(next());
        }
    
        public long nextLong() {
            return Long.parseLong(next());
        }
    	double nextDouble()
    	{
    		return Double.parseDouble(next());
    	}
	}
}