import sys
sin = sys.stdin

t = int(sin.readline())

for _ in range(t):
    n = int(sin.readline())
    arr = [int(x) for x in sin.readline().split()]
    best = 1000000
    d = dict()
    for i in range(len(arr)):
        if arr[i] in d:
            newDist = i - d[arr[i]] + 1
            best = min(best, newDist)
        d[arr[i]] = i
    
    if best < 1000000:
        print(best)
    else:
        print(-1)
