t = int(input())
while t:
    t -= 1
    n = int(input())
    a = list(map(int, input().split()))
    l = r = 0
    hashmap = {}
    ans = 1000000
    while r < n:
        if hashmap.get(a[r], None) == None:
            hashmap[a[r]] = r
        else:
            ans = min(ans, r-hashmap[a[r]]+1)
            hashmap[a[r]] = r
            l += 1
        r += 1
    if ans == 1000000:
        print("-1")
    else:
        print(ans)
