for _ in range (int(input())):
    n = int(input())
    freq = [[0 for i in range (2)] for j in range (n)]
    m = 0
    ans = 999999999999
    l = list(map(int, input().split()))
    for i in range (n):
        x = freq[l[i] - 1][1]
        freq[l[i] - 1][1] = i + 1
        #print(x, freq, ans)
        if (freq[l[i] - 1][1] - x) > 0 and x != 0:
        #    print(freq[l[i] - 1][1] , x)
            ans = min(ans, freq[l[i] - 1][1] - x)
    if ans == 999999999999:
        print(-1)
    else:
        print(ans + 1)