for _ in range(int(input())):
    n = int(input())
    a = list(map(int,input().split()))
    min = 1000000
    i = 0
    d = {}
    while(i < len(a)):
        x = a[i]
        if x in d:
            e = d[x]
            r = e[-1]
            di = i-r+1
            if di < min:
                min = di

            e.append(i)
            d[x] = e

        else:
            d[x] = [i]

        i = i+1

    if min == 1000000:
        print(-1)

    else:
        print(min)
