I = lambda: map(int, raw_input().split())
def s_1257c():
    def help(arr):
        d = {}
        for i in arr:
            c = d.get(i, 0) + 1
            d[i] = c
        maxc = max(d.values())

        c = 0
        val = 0
        for k, v in d.items():
            if v == maxc:
                val = k
                c += 1
                if c > 1:
                    break

        if c > 1:
            return -1

        fi = -1
        res = len(arr)
        for idx, i in enumerate(arr):
            if i == val:
                if fi == -1:
                    fi = idx
                else:
                    res = min(res, idx-fi+1)
                    fi = idx
        return res


    t = I()
    for _ in range(t[0]):
        n = input()
        arr = I()
        if n == 1:
            print -1
        print help(arr)



s_1257c()