T = int(input())

for _ in range(T):
    n = int(input())
    a = tuple(int(_) for _ in input().split())

    if len(a) < 2:
        print(-1)
    else:
        freq = {}
        for index, item in enumerate(a):
            if item in freq:
                freq[item].append(index)
            else:
                freq[item] = [index]

        min_len = -1
        for k, v in freq.items():
            if len(v) >= 2:
                for i in range(len(v) - 1):
                    if min_len == -1:
                        min_len = v[i + 1] - v[i] + 1
                    else:
                        min_len = min(min_len, v[i + 1] - v[i] + 1)

        print(min_len)
