from sys import stdin
from collections import Counter


def solve():
    p1, ans, cur, most = 0, float('inf'), 0, Counter(a).most_common(1)[0][0]
    for i in range(n):
        if a[i] == most:
            cur += 1

        if cur == 2:
            for j in range(p1, i):
                if a[j] == most:
                    cur -= 1
                    p1 = j + 1
                    ans = min(ans, i - j + 1)
                    break

    return str(ans)


out = []
for i in range(int(stdin.readline())):
    n, a = int(stdin.readline()), list(map(int, stdin.readline().split()))
    out.append(str(-1) if n == 1 else solve())

print('\n'.join(out))
