t = int(input())


def slove():
    n = int(input())
    a = input().split(' ')
    mp = {}
    ans = 1000000000
    for i in range(len(a)):
        if mp.get(a[i]):
            ##print(mp[a[i]])
            ans = min(int(i + 1 - int(mp[a[i]])), ans)
            mp[a[i]] = i + 1
        else:
            mp[a[i]] = i + 1
    if ans == 1000000000:
        print(-1)
    else:
        print(ans + 1)


while t > 0:
    t -= 1
    slove()
