import sys
input = sys.stdin.readline


def main():
    T = int(input())
    for _ in range(T):
        n = int(input())
        a = list(map(int, input().split()))
        d = dict()
        ans = float("inf")
        for i, x in enumerate(a):
            if x not in d:
                d[x] = i
            else:
                ans = min(ans, i - d[x] + 1)
                d[x] = i
        if ans == float("inf"):
            print(-1)
        else:
            print(ans)


if __name__ == '__main__':
    main()
