import sys
import os.path

n = 0
a = []

class SegmentTree:
    tree = []
    def __init__(self,size_tree):
        self.tree = [[0] * 2 for node in range(4 * size_tree + 4)]
    def compare(self,l,r):
        return l if l[0] > r[0] else r
    def updatePos(self,l,r,pos,val,node = 1):
        if l > r :
            return
        if l == r:
           self.tree[node][0] += val
           self.tree[node][1] = pos
           return
        mid = (l + r) // 2
        if pos <= mid:
            self.updatePos(l,mid,pos,val,node << 1)
        else :
            self.updatePos(mid + 1,r,pos,val,node << 1 | 1)
        self.tree[node] = self.compare(self.tree[node << 1],self.tree[node << 1 | 1])
    def rmq(self,tl,tr,l,r,node = 1):
        if l > r or r < tl or l > tr or tl > tr:
            return [-1,-1]
        if tl <= l and r <= tr :
            return self.tree[node]
        mid = (l + r) // 2
        return self.compare(self.rmq(tl,tr,l,mid,node << 1),self.rmq(tl,tr,mid + 1,r,node << 1 | 1))

def ok(L):
    segtree = SegmentTree(n + 2)
    for i in range(n):
        segtree.updatePos(1,n,a[i],1)
        if i >= L :
            segtree.updatePos(1,n,a[i - L],-1)
        if i >= L - 1 :
             mx = segtree.rmq(1,n,1,n)
             mxl = segtree.rmq(1,mx[1] - 1,1,n)
             mxr = segtree.rmq(mx[1] + 1,n,1,n)
             if mx[0] > 0 :
                 if mx[0] > max(mxl[0],mxr[0]):
                     del segtree
                     return True
    del segtree
    return False

def solve():
    # code goes here!!         
    global n
    global a
    for tests in range(int(input())):
        a = []
        n = int(input())
        a = list(map(int,input().split()))
        ans,lo,hi = -1,2,n
        while lo <= hi :
            mid = (lo + hi) // 2
            if ok(mid) :
                hi = mid - 1
                ans = mid
            else :
                lo = mid + 1
        print(ans)

def main():
    # comment when submit.
    if os.path.exists('test.inp') :
        sys.stdin = open("test.inp","r")
    solve()

if __name__ == '__main__':
    main()


