import math
t = int(input())

for si in range(t):
    n = int(input())
    a = [int(s) for s in input().split(' ')]
    mstack = [-1] * n
    ans = n + 1
    for i in range(len(a)):
        if mstack[a[i] - 1] != -1:
            ans = min(ans, (i - mstack[a[i] - 1] + 1))
            #print("a", mstack[a[i] - 1], "A", i)
        mstack[a[i] - 1] = i
    
    if ans == n + 1 or ans == 1:
        print("-1")
        continue
    print(ans)

    
