t = int(input())
for _ in range(t):

    n = int(input())
    arr = list(map(int,input().strip().split()))[:n]
    from collections import defaultdict

    ans = float('inf')
    store = defaultdict(lambda : -1)

    for i in range(n):
        if store[arr[i]] != -1:
            ans = min(ans,i-store[arr[i]]+1)
        store[arr[i]] = i



    if ans == float('inf'):
        ans = -1
    print(ans)
