from collections import Counter

def dominated_subarray(n, array):
    if n < 2:
        return -1
    curr_sub = Counter(array[:2])
    if len(curr_sub) == 1:
        return 2
    curr_sub[array[0]] = [curr_sub[array[0]], 0]
    curr_sub[array[1]] = [curr_sub[array[1]], 1]
    ans = n+1
    for i in range(2, n):
        if array[i] in curr_sub:
            #print(ans, i+1 - curr_sub[array[i]][1], curr_sub[array[i]])
            ans = min(ans, i+1 - curr_sub[array[i]][1])
            curr_sub[array[i]][1] = i
        else:
            curr_sub[array[i]] = [1, i]
        if ans == 2:
            break
    if ans == n+1:
        return -1
    return ans

if __name__ == '__main__':
    q = int(input())
    for i in range(q):
        n = int(input())
        array = list(map(int, input().split()))
        print(dominated_subarray(n, array))