from math import inf

t = int(input())

for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    
    els = set()
    els.add(a[0])
    r = 0
    l = 0
    
    short = inf
    

    while l != len(a)-1:
        r = min(max(l+1, r), len(a)-1)
        
        #print(l, r, short, els)
        if a[r] in els:
            if a[l] != a[r]:
                l += 1
                els.remove(a[l-1])
            else:
                short = min(short, r-l+1)
                l += 1
                r += 1
        else:
            els.add(a[r])
            r += 1
        
        
    print(short)