import sys

def readline_single(dtype):
    """Read a single element from standard input, e.g:
        
        N = readline_single(int)
    """
    return dtype(sys.stdin.readline().rstrip())

def readline_multiple(dtype):
    """Read multiple elements from standard input, e.g:
        
        N, M = readline_multiple(int)
    """
    return map(dtype, sys.stdin.readline().split())

def readline_list(dtype):
    """Read a list of elements from standard input, e.g:

        array = readline_list(int)
    """
    return list(map(dtype, sys.stdin.readline().split()))

def readline_string():
    """Read one line of string from standard input, e.g:
        
        s = readline_string()
    """
    return sys.stdin.readline()[:-1]

def print_list(lst):
    print(" ".join(str(item) for item in lst))

def print_list_of_list(lists):
    for lst in lists:
        print_list(lst)

def solve(N, a):
    visited = {}
    ans = None
    start = 0

    for i in range(N):
        if a[i] in visited:
            if ans is None:
                ans = i - visited[a[i]] + 1
            else:
                ans = min(ans, i - visited[a[i]] + 1)
            start += 1
        visited[a[i]] = i
    return -1 if ans is None else ans

def main():
    T = readline_single(int)
    for _ in range(T):
        N = readline_single(int)
        a = readline_list(int)
        ans = solve(N, a)
        print(ans)


if __name__ == "__main__":
    main()
