import sys
input = sys.stdin.readline
from collections import Counter




def main():
    T = int(input())
    for _ in range(T):
        N = int(input())
        A = [int(x) for x in input().split()]
        if N == 1:
            print(-1)
        else:
            mi = N + 1
            S = Counter()
            i = 0
            for j in range(N):
                S[A[j]] += 1
                while S[A[j]] == 2:
                    mi = min(mi, j - i + 1)
                    S[A[i]] -= 1
                    i += 1

            if mi == N + 1:
                print(-1)
            else:
                print(mi)









if __name__ == '__main__':
    main()


