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

public class A
{
    static int n;
    static int[] arr;
    static char[] s;
    public static void main(String[] args) throws IOException
    {
        Flash f = new Flash();
        int T = f.ni();
        for(int tc = 1; tc <= T; tc++){
            n = f.ni(); arr = f.arr(n); sop(fn());
        }
    }
    
    static int fn()
    {
        if(n == 1) return -1;
        
        int l = 1, r = n;
        while(l < r){
            int mid = l + (r - l) / 2;
            //sop("MID : " + mid);
            if(ok(mid)) r = mid;
            else l = mid + 1;
        }
        
        return l == 1 ? -1 : l;
    }
    
    static boolean ok(int len){
        if(len <= 1) return false;
        
        int[] map = new int[n+1];
        for(int i = 0; i <= n-len; i++){
            map[arr[i]]++;
            if(i >= len-1){
                if(i-len >= 0) map[arr[i-len]]--;
                if(ok2(map)) return true;
            }
        }
        
        return false;
    }
    
    static boolean ok2(int[] map){
        int max = 0;
        for(int a : map) max = Math.max(max, a);
        int cnt = 0;
        for(int a : map) if(a == max) cnt++;
        return cnt == 1;
    }
    
    static void sort(int[] a){
        List<Integer> A = new ArrayList<>();
        for(int i : a) A.add(i);
        Collections.sort(A);
        for(int i = 0; i < A.size(); i++) a[i] = A.get(i);
    }
    
    static int swap(int itself, int dummy){
        return itself;
    }
    
    static void sop(Object o){ System.out.println(o); }
    
    static void print(int[] a){
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < a.length; i++) sb.append(a[i] + " ");
        System.out.println(sb);
    }
    
    static class Flash
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");
        
        String next(){
            while(!st.hasMoreTokens()){
                try{
                    st = new StringTokenizer(br.readLine());
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
            
            return st.nextToken();
        }
        
        String ns(){
            String s = new String();
            try{
                s = br.readLine().trim();
            }catch(IOException e){
                e.printStackTrace();
            }
            
            return s;
        }
        
        int ni(){
            return Integer.parseInt(next());
        }
        
        int[] arr(int n){
            int[] a = new int[n];
            for(int i = 0; i < n; i++) a[i] = ni();
            return a;
        }

        long nl(){
            return Long.parseLong(next());
        }
        
        double nd(){
            return Double.parseDouble(next());
        }
    }
}