import sys
from array import array  # noqa: F401
import typing as Tp  # noqa: F401


def input():
    return sys.stdin.buffer.readline().decode('utf-8')


def main():
    t = int(input())
    inf = 10**9

    for _ in range(t):
        n = int(input())
        a = list(map(int, input().split()))
        prev = {}
        ans = inf

        for i, v in enumerate(a):
            if v in prev and ans > i - prev[v] + 1:
                ans = i - prev[v] + 1
            prev[v] = i

        print(ans if ans < inf else -1)


if __name__ == '__main__':
    main()
