import java.io.BufferedReader;
// import java.io.FileInputStream;
// import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.stream.Collectors;

import static java.lang.Math.abs;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.round;
import static java.lang.Math.sqrt;
import static java.util.Arrays.copyOf;
import static java.util.Arrays.fill;
import static java.util.Arrays.sort;
import static java.util.Collections.reverse;
import static java.util.Collections.reverseOrder;
import static java.util.Collections.sort;
import static java.util.Comparator.comparing;
import static java.util.Comparator.comparingInt;
import static java.util.Comparator.comparingLong;

public class Main {
    FastScanner in;
    PrintWriter out;

    private void solve() throws IOException {
        //solveC();
       solveD();
        // solveE();
    }

    private void solveC() throws IOException {
    	int n, x, a, b;
    	n = in.nextInt();
    	x = in.nextInt();
    	a = in.nextInt();
    	b = in.nextInt();
    	 
    	 int l = min(a,b);
    	 int r = max(a,b);
    	 int sol;
    	 int newr = r + min(x, n-r);
    	 if(x < n-r) {
    		 sol = newr - l;
    	 } else {
    		 x =x - (n -r);
    		 int newl = l - min(x, l-1);
    		 sol = newr - newl;
    	 }
        out.println(sol);
    }
    
    private int mod(int n) {
    	if(n < 0)
    		return -n;
    	return n;
    }
    private void dfs(char[][] grid, boolean[][] vis,int row,int col,int rows,int cols) {
        vis[row][col] = true;
        
        if(row-1 >= 0 && grid[row-1][col] == '1' && !vis[row-1][col])dfs(grid,vis,row-1,col,rows,cols);
        if(row+1 < rows && grid[row+1][col] == '1' && !vis[row+1][col])dfs(grid,vis,row+1,col,rows,cols);
        if(col-1 >= 0 && grid[row][col-1] == '1' && !vis[row][col-1])dfs(grid,vis,row,col-1,rows,cols);
        if(col+1 < cols && grid[row][col+1] == '1' && !vis[row][col+1])dfs(grid,vis,row,col+1,rows,cols);
    }
    
    private void solveD() throws IOException {
        int n = in.nextInt();
        int[] arr = new int[n+1];
        
        for(int i=1; i<=n; i++) {
        	arr[i] = in.nextInt();
        }
        
        Map<Integer, Integer> map = new HashMap<>();
        int sol = Integer.MAX_VALUE;
        for(int i=1; i<=n; i++) {
        	if(map.containsKey(arr[i])) {
        		int lastIndex = map.get(arr[i]);
        		if(i - lastIndex < sol) {
        			sol = i - lastIndex;
        		}
        	}
        	map.put(arr[i], i);
        }
        if(sol == Integer.MAX_VALUE)
        	out.println(-1);
        else
        	out.println(sol + 1);
    }

    private void solveE() throws IOException {
        
    }

    private void solveF() throws IOException {

    }

    private void solveF2() throws IOException {

    }

    private void solveE3() throws IOException {

    }

    private void solveF4() throws IOException {

    }

    class FastScanner {
        StringTokenizer st;
        BufferedReader br;

        FastScanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        boolean hasNext() throws IOException {
            return br.ready() || (st != null && st.hasMoreTokens());
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        String nextLine() throws IOException {
            return br.readLine();
        }

        boolean hasNextLine() throws IOException {
            return br.ready();
        }
    }

    private void run() throws IOException {
        in = new FastScanner(System.in); // new FastScanner(new FileInputStream('.in'));
        out = new PrintWriter(System.out); // new PrintWriter(new FileOutputStream('.out'));
        int t = in.nextInt();
        while(t-- > 0) {
            solve();
        }

        out.flush();
        out.close();
    }

    public static void main(String[] args) throws IOException {
        new Main().run();
    }
}