def main():
    def solve():

        n = int(input())
        aa = [int(a) for a in input().split()]
        pos = [[] for _ in range(n+1)]
        for i,a in enumerate(aa):
            pos[a].append(i)
        res = n
        for pp in pos:
            if len(pp) > 1:
                for i in range(1, len(pp)):
                    if pp[i] - pp[i-1] < res:
                        res = pp[i] - pp[i-1]
        if res == n:
            print(-1)
        else:
            print(res+1)


    q = int(input())
    for _ in range(q):
        solve()


if __name__ == "__main__":
    main()