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

public class Main
{  
    public static void main(String[] args) throws IOException 
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());

        while(t-- > 0)
        {
            int n = Integer.parseInt(br.readLine());
            int[] arr = new int[n+1];
            int ans = Integer.MAX_VALUE;
            
            StringTokenizer st = new StringTokenizer(br.readLine());
            for(int i=1; i<=n; i++)
            {
                int num = Integer.parseInt(st.nextToken());
                if(arr[num] != 0) ans = Math.min(ans,i-arr[num]+1);
                arr[num] = i;
            }
            System.out.println(ans == Integer.MAX_VALUE? -1: ans);
        }
    }
}