/* Rajkin Hossain */

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

public class B{
   
    FastInput k = new FastInput(System.in);
    //FastInput k = new FastInput("/home/rajkin/Desktop/input.txt");
    FastOutput z = new FastOutput();
    
    int n;
    int [] y;
    
	HashMap<Integer, Integer> count = new HashMap<Integer, Integer>();
	
	int addMap(int v) {
		if(count.containsKey(v)) {
			int prevCount = count.get(v);
			
			count.put(v, prevCount + 1);
			
			return prevCount + 1;
		}
		else{
			count.put(v, 1);
			
			return 1;
		}
	}
	
	int removeMap(int v) {
		if(!count.containsKey(v)) return 0;
		
		int prevCount = count.get(v);
		
		if(prevCount == 1) {
			count.remove(v);
			
			return 1;
		}
		else {
			count.put(v, prevCount - 1);
			
			return prevCount;
		}
	}
    
    boolean isPossible(long value) {
    	count.clear();
    	
    	for(int i = 0; i<(int)value; i++) {
    		addMap(y[i]);
    	}
    	
    	int max = 0;
    	int co = 0;
    	for(int key : count.keySet()) {
    		int mc = count.get(key);
    		if(mc > max) {
    			max = mc;
    			co = 1;
    		}
    		else if(max == mc){
    			co++;
    		}
    	}
    	
    	if(co == 1) return true;
    	
    	int start = 0;
    	for(int i = (int)value; i<n; i++) {
    		
    		removeMap(y[start]);
    		addMap(y[i]);
    		
        	for(int key : count.keySet()) {
        		int mc = count.get(key);
        		if(mc > max) {
        			max = mc;
        			co = 1;
        		}
        		else if(max == mc){
        			co++;
        		}
        	}
    		
    		
    		if(co == 1) return true;
    		
    		start++;
    	}
    	
    	return false;
    }
    
    void startAlgo() {
    	long low = 2;
    	long high = n;
    	long ans = -1;
    	
    	while(low <= high) {
    		if(low == high) {
        		if(isPossible(low)) {
        			ans = low;
        		}
    			
    			break;
    		}
    		
    		long mid = (high + low) / 2;
    		
    		if(isPossible(mid)) {
    			ans = mid;
    			high = mid;
    		}
    		else {
    			low = mid + 1;
    		}
    	}
    	
    	z.println(ans);
    }
    
	void startProgram() {
		int q = k.nextInt();
		
		while(q-->0) {
			n = k.nextInt();
			
			y = new int[n];
			
			for(int i = 0; i<n; i++) {
				y[i] = k.nextInt();
			}
			
			startAlgo();
		}
		
    	z.flush();
    	System.exit(0);
	}
    
    public static void main(String [] args) throws IOException {
        new Thread(null, new Runnable(){
            public void run(){
            	try{ 
            		new B().startProgram();
            	}
            	catch(Exception e){
            		e.printStackTrace();
            	}
            }
        },"Main",1<<28).start();
    }
    

    /* MARK: FastInput and FastOutput */
 
    class FastInput {
        BufferedReader reader;
        StringTokenizer tokenizer;
 
        FastInput(InputStream stream){
            reader = new BufferedReader(new InputStreamReader(stream));
        }
       
        FastInput(String path){
            try {
                reader = new BufferedReader(new FileReader(path));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            tokenizer = null;
        }
 
        String next() {
            return nextToken();
        }
       
        String nextLine() {
            try {
                return reader.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           
            return null;
        }
 
        boolean hasNext(){
            try {
                return reader.ready();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return false;
        }
 
        String nextToken() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                String line = null;
                try {
                    line = reader.readLine();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                if (line == null) {
                    return null;
                }
                tokenizer = new StringTokenizer(line);
            }
            return tokenizer.nextToken();
        }
 
        int nextInt() {
            return Integer.parseInt(nextToken());
        }
       
        long nextLong() {
            return Long.valueOf(nextToken());
        }
        
        double nextDouble() {
            return Double.valueOf(nextToken());
        }
    }
 
    class FastOutput extends PrintWriter {
		FastOutput() {
			super(new BufferedOutputStream(System.out)); 
		}
		
		public void debug(Object...obj) {
		    System.err.println(Arrays.deepToString(obj));
		}
    }
}
