t = int(input())
for T in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    anum = dict()
    if n < 2:
        print(-1)
        continue
    maxi = 1
    for i in range(n):
        if a[i] in anum:
            anum[a[i]].append(i)
            if len(anum[a[i]]) > maxi:
                maxi = len(anum[a[i]])
        else:
            anum[a[i]] = list()
            anum[a[i]].append(i)
    if maxi == 1:
        print(-1)
        continue
    miks = n + 1
    l = 0
    r = n + 1
    for z in anum:
        for m in range(len(anum[z])-1):
            if anum[z][m+1] - anum[z][m] < miks:
                miks = anum[z][m+1] - anum[z][m]
                l = anum[z][m]
                r = anum[z][m+1]
    print(r - l+1)



