t = int(input())

while(t > 0):
    t -= 1
    n = int(input())
    a = []
    a = list(map(int, input().split()))    
    if(n < 2):
        print("-1")
        continue

    mp = {}
    size = 100000000

    for i in range(n):
        if not a[i] in mp:
            mp[a[i]] = i
        else:
            size = min(size, i - mp[a[i]] + 1)
            mp[a[i]] = i
    print(size)