test = int(input())
while test > 0:
    test -= 1
    n = int(input())
    lst = sorted(zip(map(int, input().split()), range(1, n+1)))
    cnt = [0] * (n+1)
    pos = 1
    for a, b in lst:
        cnt[pos] = b
        pos += 1
    f = [0] * (n + 1)
    pos = 1
    pre = 1
    val = 0
    #print(mx)
    for a, b in lst:
        if pos > 1 and a != val:
            pre = pos
        #print(pos, pre)
        if pos - pre + 1 < 2:
            f[b] = -1
        else:
            f[b] = cnt[pre]
            pre += 1
        #print(pos, pre, b, f[b])
        pos += 1
        val = a
    mx = -1
    rs = n + 1
    for i in range(1, n+1):
        if f[i] > mx:
            rs = min(rs, i - f[i] + 1)
        mx = max(mx, f[i])
    if rs <= n:   print(rs)
    else:         print(-1)


