import java.io.*;
import java.util.*;
public class cp1 {
	public static void main(String args[]) {

		int t=II();
		test: while(--t>=0){
			int n=II();
			int a[]=IIA(n);
			if(n==1){
				System.out.println(-1);
				continue test;
			}
			HashMap<Integer, Integer> hmap=new HashMap<>();
			int i=0, j=1;
			int ans=0;
			hmap.put(a[i], 1);
			while(j<n){
				if(!hmap.containsKey(a[j])){
					hmap.put(a[j], 1);
					j++;
				}
				else{
					hmap.put(a[j], hmap.get(a[i])+1);
					while(a[i]!=a[j]){
						hmap.put(a[i], hmap.get(a[i])-1);
						if(hmap.get(a[i])==0)
							hmap.remove(a[i]);
						i++;
					}
					ans=j-i+1;
					j++;
					i++;
				}
				// System.out.println(j);
			}
			if(ans==0)
				System.out.println(-1);
			else
				System.out.println(ans);
		}
		
	}
	
	static scan in=new scan(System.in);
	static int II() {
		return in.nextInt();
	}
	static long IL() {
		return in.nextLong();
	}
	static int[] IIA(int n) {
		int a[]=new int[n];
		for(int i=0;i<n;i++) {
			a[i]=II();
		}
		return a;
	}
	static long[] ILA(int n) {
		long a[]=new long[n];
		for(int i=0;i<n;i++) {
			a[i]=IL();
		}
		return a;
	}
	static String IS() {
		return in.next();
	}
	static char IC(){
		return in.next().charAt(0);
	}
	static String[] ISA(int n) {
		String a[]=new String[n];
		for(int i=0;i<n;i++) {
			a[i]=IS();
		}
		return a;
	}
	static char[] ICA(int n) {
		char a[]=new char[n];
		for(int i=0;i<n;i++) {
			a[i]=IC();
		}
		return a;
	}
}
class scan
{
    public static BufferedReader reader;
    public static StringTokenizer token;

    scan(InputStream str)
    {
        reader=new BufferedReader(new InputStreamReader(str));
        token=null;
    }

    static int nextInt()
    {
        while(token==null||!token.hasMoreTokens())
        {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        }
        return Integer.parseInt(token.nextToken());
    }

    static long nextLong()
    {
        while(token==null||!token.hasMoreTokens())
        {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        }
        return Long.parseLong(token.nextToken());
    }

    static String next()
    {
        while(token==null||!token.hasMoreTokens())
        {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        }
        return token.nextToken();
    }

}