s = ""
for i in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    if n==1:
        s += str(-1) + "\n"
    else:
        v = [0]*(n+2)
        f = [200001]*(n+2)
        for i in range(n):
            if v[a[i]] > 0:
                f[a[i]] = min(f[a[i]], i-v[a[i]]+2)
                v[a[i]] = i+1
            else:
                v[a[i]] = i+1
                f[a[i]] = 200001
        h = min(f)
        if h==200001:
            s += str(-1) + "\n"
        else:
            s += str(h) + "\n"
print(s)
