def compute(arr):
  n = len(arr)
  length = 0
  lps = []
  for i in range(0, n):
    lps.append(0)

  i = 1
  while(i<n):
    if(arr[i] == arr[length]):
      length += 1
      lps[i] = length
      i += 1
    else:
      if(length != 0):
        length = lps[length-1]
      else:
        lps[i] = 0
        i += 1

  return max(lps)

t = int(raw_input())
while t>0:
  t -= 1
  n = int(raw_input())
  arr = list(map(int, raw_input().split()))
  print(compute(arr))