q = int(input())
for _ in range(q):
    n = int(input())
    l = list(map(int,input().split()))
    if n == 1:
        print(-1)
        continue
    minimum = 9999999
    a = 0
    b = 0
    d = {}
    while b != n:
        while b != n:
            if d.get(l[b]) == None:
                d[l[b]] = 1
            elif d[l[b]] == 1:
                d[l[b]] = 2
                break
            else:
                d[l[b]] += 1
            b += 1
        if b != n:
            while b-a>0:
                if d[l[a]] == 2:
                    d[l[a]] = 1
                    if b-a+1<minimum:
                        minimum = b-a+1
                    a += 1
                    b += 1
                    break
                else:
                    d[l[a]] -= 1
                a += 1
    if minimum == 9999999:
        print(-1)
    else:
        print(minimum)
