for i in range(int(input())):
    n = int(input())
    arr = [[int(i), 0] for i in input().split()]
    for i in range(n):
        arr[i][1] = i
    arr.sort()
    ans = n+5
    for i in range(n-1):
        if(arr[i][0]==arr[i+1][0]):
            ans = min(ans, 1+abs(arr[i][1]-arr[i+1][1]))
    if(ans > n):
        print(-1)
    else:
        print(ans)
