for x in range(int(input())):
    n = int(input())
    a = [int(x) for x in input().split()]
    b = [0] * (n + 5)
    if n == 1:
        print(-1)
        continue
    for x in a:
        b[x] += 1
    mx = b.index(max(b))
    b.sort()
    if b[len(b) - 1] == b[len(b) - 2]:
        print(-1)
    else:
        ans = n + 5
        j = -1
        for i in range(n):
            if a[i] == mx:
                if j != -1:
                    ans = min(ans, i - j + 1)
                j = i
        print(ans)
