
if __name__ == '__main__':
    t = int(input())
    
    for _ in range(t):
        n = int(input())
        idx = {}
        mx = {}
        for i, a in enumerate(map(int, input().split())):
            if a not in idx:
                idx[a] = i
                mx[a] = 10**19
            else:
                mx[a] = min(mx[a], i - idx[a] + 1)
                idx[a] = i
        mn = min(mx.values())
        if mn < 10**19:
            print(mn)
        else:
            print(-1)
            
        
        
            
            
        
        
        
        
        