t = int(input())
while t!=0:
    t-=1
    n = int(input())
    arr = list(map(int,input().split(' ')))
    d = {}
    s = {}
    for i in range(n):
        if arr[i] not in d.keys():
            d[arr[i]] = i
        else:
                
            d[arr[i]] = abs(d[arr[i]] - i) +1
            if arr[i] in s.keys():
                if d[arr[i]] < s[arr[i]]:
                    s[arr[i]] = d[arr[i]]
                    continue
            s[arr[i]] = d[arr[i]]
            d[arr[i]] = i
    if len(s) == 0:
        print(-1)
    else:
        print(min(s.values()))
