a = int(input())
for i in range(a):
    b = int(input())
    lst = list(map(int, input().split()))
    c = b+1
    d = [-1]*(b+1)
    for t in range(b):
        if d[lst[t]] != -1:
            c = min(c, t-d[lst[t]]+1)
        d[lst[t]] = t
    if c > b:
        c = -1
    print(c)