import java.util.*;
import java.io.*;

public class Main
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);
        int t = Integer.parseInt(input.readLine());
        while(t-->0)
        {
            int n = Integer.parseInt(input.readLine());
            ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
            for(int i = 0; i <= n; i++) list.add(new ArrayList<Integer>());
            StringTokenizer st = new StringTokenizer(input.readLine());
            for(int i = 1; i <= n; i++) list.get(Integer.parseInt(st.nextToken())).add(i);
            int min = Integer.MAX_VALUE;
            for(int i = 1; i <= n; i++)
                for(int j = 1; j < list.get(i).size(); j++)
                    min = Math.min(min,list.get(i).get(j)-list.get(i).get(j-1)+1);
            if(min==Integer.MAX_VALUE) out.println(-1);
            else out.println(min);        
        }
        out.flush();
        out.close();
    }
}