# Name: Akshath Jain
# Date: 11/13/19
# Purpose: Codeforces round 76 problem C

t = int(input())
for i in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    x = n + 1
    c = dict()
    for j in range(n):
        if a[j] not in c:
            c[a[j]] = j
        else:
            x = min(x, j - c[a[j]] + 1)
            c[a[j]] = j

    if x != n + 1:
        print(x)
    else:
        print(-1)