for i in range(int(input())):
    N, X, MyDict, Min = int(input()), list(map(int, input().split())), {}, 10 ** 9
    for j in range(N):
        if X[j] not in MyDict:
            MyDict[X[j]] = j
        else:
            Min = min(Min, j - MyDict[X[j]])
            MyDict[X[j]] = j
    print(-1 if Min == 10 ** 9 else Min + 1)

# Come together for getting better !!!!
