T = int(raw_input())
while(T>0):
	T = T-1
	N = int(raw_input())
	a = map(int,(raw_input()).split())
	
	n = len(a)
  	length = 0
  	arr = []
  	for i in range(0, n):
  		arr.append(0)
  		
  	i = 1
  	while(i<n):
  		if(a[i] == a[length]):
  			length += 1
  			arr[i] = length
  			i += 1
  		else:
  			if(length != 0):
  				length = arr[length-1]
  			else:
  				arr[i] = 0
  				i += 1
  	else:
  		print max(arr)