You are given an array $a$ consisting of $n$ integers.

Your task is to determine if $a$ has some subsequence of length at least $3$ that is a palindrome.

Recall that an array $b$ is called a subsequence of the array $a$ if $b$ can be obtained by removing some (possibly, zero) elements from $a$ (not necessarily consecutive) without changing the order of remaining elements. For example, $[2]$, $[1, 2, 1, 3]$ and $[2, 3]$ are subsequences of $[1, 2, 1, 3]$, but $[1, 1, 2]$ and $[4]$ are not.

Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $a$ of length $n$ is the palindrome if $a_i = a_{n - i - 1}$ for all $i$ from $1$ to $n$. For example, arrays $[1234]$, $[1, 2, 1]$, $[1, 3, 2, 2, 3, 1]$ and $[10, 100, 10]$ are palindromes, but arrays $[1, 2]$ and $[1, 2, 3, 1]$ are not.

You have to answer $t$ independent test cases.


-----Input-----

The first line of the input contains one integer $t$ ($1 \le t \le 100$) — the number of test cases.

Next $2t$ lines describe test cases. The first line of the test case contains one integer $n$ ($3 \le n \le 5000$) — the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the $i$-th element of $a$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$ ($\sum n \le 5000$).


-----Output-----

For each test case, print the answer — "YES" (without quotes) if $a$ has some subsequence of length at least $3$ that is a palindrome and "NO" otherwise.


-----Example-----
Input
5
3
1 2 1
5
1 2 2 3 2
3
1 1 2
4
1 2 2 1
10
1 1 2 2 3 3 4 4 5 5

Output
YES
YES
NO
YES
NO



-----Note-----

In the first test case of the example, the array $a$ has a subsequence $[1, 2, 1]$ which is a palindrome.

In the second test case of the example, the array $a$ has two subsequences of length $3$ which are palindromes: $[2, 3, 2]$ and $[2, 2, 2]$.

In the third test case of the example, the array $a$ has no subsequences of length at least $3$ which are palindromes.

In the fourth test case of the example, the array $a$ has one subsequence of length $4$ which is a palindrome: $[1, 2, 2, 1]$ (and has two subsequences of length $3$ which are palindromes: both are $[1, 2, 1]$).

In the fifth test case of the example, the array $a$ has no subsequences of length at least $3$ which are palindromes.

Source: codeforces | Difficulty: EASY
Tags: String algorithms

Complete search


Solutions:
t = int(input())
while t:
	f = 0
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n - 2):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				f = 1
				break
		if f == 1:
			break
	if f == 0:
		print('NO')
	else:
		print('YES')
	t -= 1


import sys
import math
T = int(sys.stdin.readline())
for _ in range(T):
	n = int(sys.stdin.readline())
	a = list(map(int, sys.stdin.readline().split()))
	pos = [[] for _ in range(27)]
	for (i, x) in enumerate(a):
		pos[x].append(i + 1)
	pre_sum = [[0] * (n + 1) if pos[i] else [] for i in range(27)]
	for (i, x) in enumerate(a):
		pre_sum[x][i + 1] = 1
	for x in range(1, 27):
		if pre_sum[x]:
			for i in range(1, n + 1):
				pre_sum[x][i] += pre_sum[x][i - 1]
	ans = 0
	for x in range(1, 27):
		if pos[x]:
			x_num = len(pos[x])
			ans = max(ans, x_num)
			mid = x_num // 2
			for j in range(mid):
				left = j
				right = x_num - 1 - j
				left_pos = pos[x][left]
				right_pos = pos[x][right]
				for y in range(1, 27):
					if pos[y] and y != x:
						ans = max(ans, pre_sum[y][right_pos] - pre_sum[y][left_pos] + (j + 1) * 2)
	print(ans)


from math import *

def binarySearch(arr, x, i):
	l = i
	r = len(arr) - 1
	while l <= r:
		mid = (l + r) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return -1

def js(arr, x):
	l = 0
	r = len(arr) - 1
	ans = -1
	while l <= r:
		m = (l + r) // 2
		if arr[m] <= x:
			ans = m
			l = m + 1
		else:
			r = m - 1
	return ans

def jg(arr, x):
	l = 0
	r = len(arr) - 1
	ans = -1
	while l <= r:
		m = (l + r) // 2
		if arr[m] >= x:
			ans = m
			r = m - 1
		else:
			l = m + 1
	return ans
for __ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 'NO'
	for i in range(n - 2):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				ans = 'YES'
	print(ans)


for _ in range(int(input())):
	N = int(input())
	lis = list(map(int, input().split()))
	dic = {}
	output = 0
	for i in range(N):
		dic[lis[i]] = dic.get(lis[i], 0) + 1
		if dic[lis[i]] == 2:
			if lis[i] != lis[i - 1]:
				output = 1
				break
		if dic[lis[i]] == 3:
			output = 1
			break
	if output == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	f = False
	for i in range(n):
		if a[i] in a[i + 1:]:
			t = a[i + 1:].index(a[i])
			if t > 0 or (t == 0 and a.count(a[i]) > 2):
				print('YES')
				f = True
				break
	if not f:
		print('NO')


import sys
input = sys.stdin.buffer.readline

def main():
	ans = []
	m = 200
	for _ in range(int(input())):
		n = int(input())
		(*a,) = map(int, input().split())
		where = [[] for _ in range(m)]
		acc = [[0] * (n + 1) for _ in range(m)]
		for (i, x) in enumerate(a):
			where[x - 1].append(i)
			for j in range(m):
				acc[j][i + 1] = acc[j][i] + (x - 1 == j)
		ret = 0
		for (q, l) in enumerate(acc):
			ret = max(ret, l[-1])
			if l[-1] == 0:
				continue
			for p in range(m):
				lim = acc[p][-1]
				for r in range(lim // 2):
					fw = where[p][r]
					bw = where[p][~r]
					if ret < 2 * (r + 1) + l[bw] - l[fw + 1]:
						ret = 2 * (r + 1) + l[bw] - l[fw + 1]
		ans.append(ret)
	print('\n'.join(map(str, ans)))
main()


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	s = set(l)
	dic = {}
	for j in s:
		dic[j] = -1
	check = 0
	for j in range(n):
		ind = l[j]
		if dic[ind] == -1:
			dic[ind] = j
		else:
			res = dic[ind]
			if j - res > 1:
				check = 1
				print('YES')
				break
	if check == 0:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	freq = {}
	flag = 0
	for i in arr:
		freq[i] = 0
	for i in range(0, n):
		freq[arr[i]] = freq[arr[i]] + 1
		x = max(freq.values())
		if x >= 3:
			flag = 1
		if i != 0:
			if freq[arr[i]] == 2 and arr[i - 1] != arr[i]:
				flag = 1
	if flag == 1:
		print('YES')
	else:
		print('NO')


def sol():
	T = int(input())
	for i in range(T):
		size = int(input())
		A = list(map(int, input().split(' ')))
		is_exist = False
		for l in range(size):
			for r in range(l + 2, size):
				if A[l] == A[r]:
					is_exist = True
					break
			if is_exist:
				break
		if is_exist:
			print('YES')
		else:
			print('NO')
sol()


from collections import defaultdict

def have_subpalindrome(array):
	indices = defaultdict(lambda : [])
	for (idx, number) in enumerate(array):
		indices[number].append(idx)
	for number_indices in indices.values():
		count = len(number_indices)
		if count == 1:
			continue
		min_max_diff = max(number_indices) - min(number_indices)
		if count == 2 and min_max_diff == 1:
			continue
		return True
	return False
number_of_tests = int(input())
for _ in range(number_of_tests):
	input()
	array = [int(x) for x in input().split()]
	result = 'YES' if have_subpalindrome(array) else 'NO'
	print(result)


from collections import Counter
TC = int(input())
while TC > 0:
	n = int(input())
	a = list(map(int, input().split()))
	ans = Counter(a).most_common()[0][1]
	for x in range(1, 201):
		S = Counter(a)
		i = 0
		j = n - 1
		cnt = 0
		while i < j:
			while i < j:
				S[a[i]] -= 1
				if a[i] == x:
					break
				i += 1
			while i < j:
				S[a[j]] -= 1
				if a[j] == x:
					break
				j -= 1
			if i >= j:
				break
			cnt += 1
			ans = max(ans, S.most_common(1)[0][1] + cnt * 2)
			i += 1
			j -= 1
	print(ans)
	TC -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	flag = 1
	for i in range(0, n - 2):
		for j in range(i + 2, n):
			if l[j] == l[i]:
				flag = 0
				break
		if flag == 0:
			break
	if flag:
		print('NO')
	else:
		print('YES')


import sys
input = sys.stdin.buffer.readline

def prog():
	for _ in range(int(input())):
		n = int(input())
		nums = list(map(int, input().split()))
		sums = []
		possible = set(nums)
		lengths = [1]
		psums = [[0] for i in range(201)]
		indexes = [[] for i in range(201)]
		for i in range(n):
			indexes[nums[i]].append(i)
			for j in possible:
				psums[j].append(psums[j][-1] + (j == nums[i]))
		for a in possible:
			for x in range(1, len(indexes[a]) // 2 + 1):
				start = indexes[a][x - 1] + 1
				end = indexes[a][len(indexes[a]) - x] - 1
				max_mid = 0
				for b in possible:
					max_mid = max(max_mid, psums[b][end + 1] - psums[b][start])
				lengths.append(2 * x + max_mid)
		sys.stdout.write(str(max(lengths)) + '\n')
prog()


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ok = False
	for i in range(n):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				ok = True
	print('YES' if ok else 'NO')


import sys
from collections import deque
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	a = deque(map(int, sys.stdin.readline().split()))
	ans = False
	while len(a):
		b = a.popleft()
		if b in a:
			if a.index(b) == 0:
				a.popleft()
				if b in a:
					ans = True
			else:
				ans = True
	print('YES' if ans else 'NO')


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = Counter(a)
	b = list(b)
	m = len(b)
	rui = [[0] * m for i in range(n + 1)]
	for i in range(n):
		for j in range(m):
			if a[i] == b[j]:
				rui[i + 1][j] = rui[i][j] + 1
			else:
				rui[i + 1][j] = rui[i][j]
	ans = 0
	for i in range(n):
		for j in range(i, n):
			cmax = 0
			dmax = 0
			cur = 0
			for k in range(m):
				q = rui[j + 1][k] - rui[i][k]
				r = min(rui[i][k], rui[n][k] - rui[j + 1][k])
				cmax = max(cmax, q)
				dmax = max(dmax, r)
			ans = max(ans, cmax + dmax * 2)
	print(ans)


def solve(lista):
	d = {}
	for i in range(len(lista)):
		el = lista[i]
		if d.get(el, -1) == -1:
			d[el] = i
		elif i - d[el] > 1:
			return True
	return False

def citire():
	t = int(input())
	sol = []
	for i in range(t):
		n = int(input())
		lista = [int(x) for x in input().split()]
		sol.append(solve(lista))
	return sol
sol = citire()
for s in sol:
	if s:
		print('YES')
	else:
		print('NO')


from sys import stdin
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().split()))
	if n <= 2:
		print('NO')
	else:
		cond = False
		for i in range(n - 2):
			if arr[i] in arr[i + 2:]:
				cond = True
				break
		if cond == True:
			print('YES')
		else:
			print('NO')


for t in range(int(input())):
	n = int(input())
	a = list(map(lambda x: int(x) - 1, input().split()))
	setA = [-1] * n
	yes = False
	for (i, e) in enumerate(a):
		if setA[e] != -1 and setA[e] != i - 1:
			yes = True
			break
		elif setA[e] == -1:
			setA[e] = i
	if yes:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	input()
	counter = 0
	y = []
	x = list(map(int, input().split()))
	for j in range(len(x)):
		if x.count(x[j]) > 2:
			print('YES')
			counter = 1
			break
		if x[j] not in y:
			y.append(x[j])
		elif x[j] != y[-1]:
			print('YES')
			counter = 1
			break
	if counter == 0:
		print('NO')


from collections import defaultdict
test_cases = int(input())

def handle():
	input()
	values = input().split(' ')
	values_and_indexes = defaultdict(list)
	for (index, value) in enumerate(values):
		values_and_indexes[value].append(index)
		if len(values_and_indexes[value]) == 3:
			return 'YES'
	for value in values_and_indexes:
		if values_and_indexes[value][-1] - values_and_indexes[value][0] > 1:
			return 'YES'
	return 'NO'
for _ in range(test_cases):
	print(handle())


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if l[i] in d:
			d[l[i]].append(i)
		else:
			d[l[i]] = [i]
	for i in d.values():
		if i[-1] - i[0] > 1:
			print('YES')
			break
	else:
		print('NO')


for f in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in a:
		d[i] = d.get(i, 0) + 1
	ans = 0
	d1 = {}
	for i in range(n):
		if d1.get(a[i], 0) + 1 <= d[a[i]] // 2:
			maxx = 0
			d2 = {}
			d1[a[i]] = d1.get(a[i], 0) + 1
			x = d1[a[i]]
			j = i + 1
			while d[a[i]] - d2.get(a[i], 0) - d1[a[i]] >= x:
				if a[i] == a[j]:
					d2[a[i]] = d2.get(a[i], 0) + 1
				else:
					d2[a[j]] = d2.get(a[j], 0) + 1
					maxx = max(maxx, d2[a[j]])
				j += 1
			ans = max(ans, maxx + 2 * x)
		else:
			ans = max(ans, d[a[i]])
	print(ans)


from sys import stdin, stdout
rl = lambda : stdin.readline()
rll = lambda : stdin.readline().split()

def main():
	T = int(rl())
	for _ in range(T):
		n = int(rl())
		A = [int(x) for x in rll()]
		ans = False
		for i in range(n):
			(left, right) = (set(), set())
			(L, R) = (i - 1, i + 1)
			while L >= 0 or R <= n - 1:
				if L >= 0:
					left.add(A[L])
				if R <= n - 1:
					right.add(A[R])
				if L >= 0 and A[L] in right:
					ans = True
				if R <= n - 1 and A[R] in left:
					ans = True
				L -= 1
				R += 1
		if ans:
			stdout.write('YES')
			stdout.write('\n')
		else:
			stdout.write('NO')
			stdout.write('\n')
	stdout.close()
main()


def main():
	n = int(input())
	a = list(map(int, input().split()))
	for j in range(n):
		for k in range(n - 1, j, -1):
			if a[k] == a[j] and k - j > 1:
				print('YES')
				return
	print('NO')
	return
t = int(input())
for i in range(t):
	main()


def isPalindrome(l):
	for i in range(0, len(l)):
		for j in range(i + 2, len(l)):
			if l[i] == l[j]:
				return True
	return False
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	if isPalindrome(l):
		print('YES')
	else:
		print('NO')


for t in range(int(input())):
	n = int(input())
	ls = list(map(int, input().split()))
	res = False
	for i in range(n):
		for j in range(i + 2, n):
			if ls[i] == ls[j]:
				res = True
	print('YES' if res else 'NO')


t = int(input())
for _ in range(t):
	n = int(input())
	list = [int(x) for x in input().split()]
	if n <= 2:
		print('NO')
		continue
	if n == 3:
		if list[0] == list[-1]:
			print('YES')
		else:
			print('NO')
		continue
	freq = {}
	ans = 'NO'
	temp = -1
	bool = 0
	for i in list:
		if i != temp:
			bool = 0
			if i in freq:
				freq[i] += 1
				if freq[i] == 2:
					ans = 'YES'
					break
			else:
				freq[i] = 1
		else:
			bool += 1
		if bool == 2:
			ans = 'YES'
			break
		temp = i
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	p = 0
	a = list(map(int, input().split()))
	b = {}
	for i in range(n):
		if a[i] in b:
			if i - b[a[i]] > 1:
				p = p + 1
		else:
			b[a[i]] = i
	if p == 0:
		print('NO')
	else:
		print('YES')


import sys

def data():
	return sys.stdin.readline().strip()
out = sys.stdout.write

def mdata():
	return map(int, data().split())
from collections import defaultdict as dd
for t in range(int(data())):
	n = int(data())
	A = list(mdata())
	s = set(A)
	m = 1
	for i in s:
		(str, end) = (0, n - 1)
		cnt = 0
		while str < end:
			while str < end:
				if A[str] == i:
					break
				str += 1
			while str < end:
				if A[end] == i:
					break
				end -= 1
			if A[str] == A[end] == i:
				d = dd(int)
				for j in range(str + 1, end):
					d[A[j]] += 1
				a = 2
				if str == end:
					a -= 1
				if len(d.keys()) != 0:
					m = max(m, 2 * cnt + a + max(d.values()))
				else:
					m = max(m, 2 * cnt + a)
			else:
				break
			cnt += 1
			str += 1
			end -= 1
	print(m)


for _ in range(int(input())):
	n = int(input())
	array = list(map(int, input().split()))
	dick = {}
	for i in array:
		dick[i] = dick.get(i, 0) + 1
	if max(list(dick.values())) > 2:
		print('YES')
		continue
	flag = False
	i = 0
	while i < n:
		if dick[array[i]] == 2:
			if array[i + 1] != array[i]:
				flag = True
				break
			else:
				i += 1
		i += 1
	if flag:
		print('YES')
	else:
		print('NO')


import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
from functools import lru_cache
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
if os.path.exists('out.txt'):
	sys.stdout = open('out.txt', 'w')

def input():
	return sys.stdin.readline()

def mapi(arg=0):
	return map(int if arg == 0 else str, input().split())
for _ in range(int(input())):
	n = int(input())
	a = list(mapi())
	flag = False
	for i in range(n):
		for j in range(n):
			if abs(i - j) > 1 and a[i] == a[j]:
				flag = True
				break
	if flag:
		print('YES')
	else:
		print('NO')


from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
for _ in range(ii()):
	MxN = 27
	n = ii()
	a = li()
	st = [[] for i in range(MxN)]
	pref = [[0 for i in range(MxN)] for j in range(n + 1)]
	for i in range(1, n + 1):
		st[a[i - 1]].append(i)
		for j in range(1, MxN):
			pref[i][j] = pref[i - 1][j]
		pref[i][a[i - 1]] += 1
	ans = 0
	for i in range(1, MxN):
		ans = max(ans, len(st[i]))
	for i in range(1, MxN):
		sz = len(st[i])
		if sz == 0:
			continue
		for j in range(sz // 2):
			idx1 = st[i][j]
			idx2 = st[i][sz - j - 1]
			mid = 0
			for k in range(1, MxN):
				if i == k:
					continue
				mid = max(mid, pref[idx2][k] - pref[idx1][k])
			ans = max(ans, 2 * (j + 1) + mid)
	print(ans)


def search(a, x):
	l = []
	for i in range(len(a)):
		if a[i] == x:
			l.append(i)
	if len(l) > 2:
		return 1
	if len(l) < 2:
		return 0
	if len(l) == 2:
		if abs(max(l) - l[0]) == 1 or abs(max(l) - l[1]) == 1:
			return 0
		else:
			return 1
t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	a = list(map(int, input().split(' ')))
	for i in range(n):
		if search(a, a[i]) == 1:
			print('YES')
			i = 0
			break
		else:
			continue
	if i == n - 1:
		print('NO')


import sys
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass

def max_freq(l, r):
	d = {}
	for i in range(l, r + 1):
		if arr[i] in d:
			d[arr[i]] += 1
		else:
			d[arr[i]] = 1
	return max(d.values()) if len(d) > 0 else 0

def palin(st, var):
	i = st
	j = n - 1
	match = 0
	stp = -1
	end = -1
	lmax = 0
	while i < j:
		if arr[i] != var:
			i += 1
		elif arr[j] != var:
			j -= 1
		elif arr[i] == arr[j] == var:
			match += 2
			stp = i
			end = j
			i += 1
			j -= 1
			lmax = max(lmax, match + max_freq(i, j))
	return lmax
input = sys.stdin.readline
for tt in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print(1)
		continue
	check = set()
	ans = 0
	for i in range(n):
		if arr[i] not in check:
			check.add(arr[i])
			ans = max(ans, palin(i, arr[i]))
	if ans == 0:
		print(1)
	else:
		print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	s = input().split()
	a = []
	for i in range(n):
		a.append(int(s[i]))
	b = []
	flag = 0
	for i in range(5000):
		b.append([0, 0])
	for i in range(len(a)):
		if b[a[i] - 1][0] != 0:
			if i - b[a[i] - 1][1] >= 2:
				print('YES')
				flag = 1
				break
		else:
			b[a[i] - 1][0] += 1
			b[a[i] - 1][1] = i
	if flag == 0:
		print('NO')


n = int(input())
for i in range(n):
	x = int(input())
	f = 'NO'
	a = list(map(int, input().split()))
	for i in range(x - 2):
		if a[i] in a[i + 2:]:
			f = 'YES'
			break
	print(f)


import os, sys, atexit
from io import BytesIO, StringIO
input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
_OUTPUT_BUFFER = StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
t = int(input())
while t:
	t += -1
	n = int(input())
	l = list(map(int, input().split()))
	(occ, f) = ([], [])
	for i in range(201):
		occ.append([])
		f.append([0] * (n + 1))
	for i in range(1, 201):
		for j in range(n):
			if l[j] == i:
				occ[i].append(j)
				f[i][j] -= -1
			if j > 0:
				f[i][j] += f[i][j - 1]
	ans = 1
	for i in range(1, 201):
		it = 0
		while 1:
			if it == len(occ[i]):
				break
			p1 = occ[i][it]
			p2 = occ[i][-it - 1]
			if p2 <= p1:
				break
			for j in range(1, 201):
				ch = 0
				if l[p1 + 1] == j:
					ch = 1
				ans = max(ans, 2 * (it + 1) + f[j][p2 - 1] - f[j][p1 + 1] + ch)
			it -= -1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	ls = list(map(int, input().split()))
	di = {i: [] for i in range(27)}
	t = 0
	for x in range(27):
		for i in range(n + 1):
			if i == n and ls[n - 1] == x:
				di[x].append(t)
			elif i == n:
				di[x].append(t)
			elif x == ls[i]:
				di[x].append(t)
				t += 1
			else:
				di[x].append(t)
		t = 0
	ans = max(max(di.values()))
	for i in range(n):
		for j in range(i, n):
			cntm = 0
			for x in range(27):
				cntm = max(cntm, di[x][j + 1] - di[x][i])
			cnts = 0
			for x in range(27):
				cnts = max(cnts, min(di[x][i] - di[x][0], di[x][n] - di[x][j + 1]))
			ans = max(ans, cntm + 2 * cnts)
	print(ans)


def main_function():
	from math import fabs
	t = int(input())
	for i in range(t):
		n = int(input())
		sequence = list(map(int, input().split(' ')))
		reverse_sequence = sequence[::-1]
		for j in range(len(sequence) - 2):
			if fabs(reverse_sequence.index(sequence[j]) - len(sequence) + 1) - j > 1:
				print('YES')
				break
		else:
			print('NO')
main_function()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	occ = {}
	f = 0
	for i in range(n):
		if a[i] in occ:
			if -occ[a[i]] + i > 1:
				f = 1
				break
		else:
			occ[a[i]] = i
	if f == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
while t:
	t -= 1
	n = int(input())
	flag = 0
	s = [int(o) for o in input().split()]
	for i in range(n):
		for j in range(i, n):
			if j + 2 < n:
				if s[i] == s[j + 2]:
					flag = 1
					break
		if flag:
			break
	if flag:
		print('YES')
	else:
		print('NO')


from functools import reduce
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def arr():
	return [int(i) for i in input().split()]

def sarr():
	return [int(i) for i in input()]

def inn():
	return int(input())
mo = 1000000007
for _ in range(int(input())):
	n = inn()
	a = arr()
	d = defaultdict(int)
	ans = 'NO'
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = i + 1
		elif i + 1 - d[a[i]] > 1:
			ans = 'YES'
			break
	print(ans)


I = input
exec(int(I()) * "I();a=*map(int,I().split()),;print('YNEOS'[all(x not in a[i+2:]for i,x in enumerate(a))::2]);")


import bisect
import collections
import copy
import functools
import heapq
import itertools
import math
import random
import re
import sys
import time
import string
from typing import List
sys.setrecursionlimit(99999)
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	d = [[] for _ in range(27)]
	for (i, c) in enumerate(arr):
		d[c].append(i)
	ans = 1
	for l in d:
		ans = max(ans, len(l))
		if len(l) > 1:
			i = 0
			j = len(l) - 1
			b = 0
			while i < j:
				b += 2
				for k in d:
					if k != l:
						ans = max(ans, b + bisect.bisect_left(k, l[j]) - bisect.bisect_left(k, l[i] + 1))
				i += 1
				j -= 1
	print(ans)


import sys
from collections import Counter
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	a = list(map(int, sys.stdin.readline().strip().split()))
	d = Counter(a)
	mx = f = 0
	for x in d:
		if d[x] > mx:
			mx = d[x]
	if mx >= 3:
		print('YES')
	elif mx == 1:
		print('NO')
	else:
		d1 = {}
		for i in range(n):
			if a[i] in d1:
				d1[a[i]].append(i)
			else:
				d1[a[i]] = [i]
		for x in d1:
			if len(d1[x]) >= 2:
				if d1[x][1] - d1[x][0] > 1:
					f = 4
					break
		if f == 4:
			print('YES')
		else:
			print('NO')


import sys
import math
from collections import defaultdict
mod = 1000000007
T = 1
T = int(sys.stdin.readline())
for _ in range(T):
	n = int(input())
	l = list(map(int, input().split()))
	a = []
	d = defaultdict(int)
	ans = 0
	for i in l:
		if d[i] == 0:
			a.append(i)
		d[i] += 1
		if d[i] > ans:
			ans = d[i]
	for i in range(len(a)):
		for j in range(len(a)):
			if i != j:
				m = max(d[a[i]], d[a[j]])
				mm = d[a[i]] >> 1
				y = 0
				for k in range(n):
					if a[i] == l[k]:
						y += 1
						if y > mm:
							break
						t = 0
						q = 0
						for kk in range(k + 1, n):
							if l[kk] == a[j]:
								t += 1
							if l[kk] == a[i]:
								if d[a[i]] - q - y < y:
									break
								ans = max(ans, t + (y << 1))
								q += 1
	print(ans)


from collections import Counter as cc
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = cc(a)
	for x in d:
		if d[x] > 2:
			print('YES')
			break
		elif d[x] > 1:
			i = a.index(x)
			a[i] = 'a'
			j = a.index(x)
			a[j] = 'b'
			if j - i > 1:
				print('YES')
				break
	else:
		print('NO')


t = int(input())
for case in range(t):
	n = int(input())
	data = list(map(int, input().split()))
	memory = {i: [0] * (n + 1) for i in range(1, 27)}
	for (i, val) in enumerate(data):
		for j in range(1, 27):
			memory[j][i + 1] = memory[j][i]
		memory[val][i + 1] += 1
	best = 0
	for i in range(1, 27):
		j = 1
		k = n - 1
		while j <= n:
			if memory[i][j] == memory[i][j - 1] + 1:
				while k >= 0 and memory[i][k] + 1 != memory[i][k + 1]:
					k -= 1
				if k >= 0 and j <= k:
					for l in range(1, 27):
						if i != l:
							best = max(best, 2 * memory[i][j] + memory[l][k] - memory[l][j])
				k -= 1
			j += 1
		best = max(best, memory[i][-1])
	print(best)


import math

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = [i, i]
		d[a[i]][1] = i
		if len(d[a[i]]) >= 2 and d[a[i]][1] - d[a[i]][0] > 1:
			return 'YES'
	return 'NO'
t = int(input())
while t > 0:
	t -= 1
	print(solve())


def binary_search(arr, low, high, x):
	if high >= low:
		mid = (high + low) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] > x:
			return binary_search(arr, low, mid - 1, x)
		else:
			return binary_search(arr, mid + 1, high, x)
	else:
		return -1
from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	x = set(l)
	d = defaultdict(list)
	s = defaultdict(list)
	for i in range(n):
		d[l[i]].append(i)
		for j in x:
			if j == l[i]:
				if len(s[j]) > 0:
					s[j].append(s[j][-1] + 1)
				else:
					s[j].append(1)
			elif len(s[j]) > 0:
				s[j].append(s[j][-1])
			else:
				s[j].append(0)
	ans = 1
	for i in range(n):
		j = binary_search(d[l[i]], 0, len(d[l[i]]) - 1, i) + 1
		k = d[l[i]][-j]
		if i < k:
			for z in x:
				ans = max(ans, 2 * j + s[z][k] - s[z][i]) if z != l[i] else max(ans, 2 * j + s[z][k] - s[z][i] - 1)
	print(ans)


import os
import sys
from io import BytesIO, IOBase

def iin():
	return int(input())

def lin():
	return list(map(int, input().split()))

def main():
	T = iin()
	mx = 200
	while T:
		T -= 1
		n = iin()
		a = lin()
		a1 = [[0] * (mx + 1) for i in range(n)]
		for j in range(n):
			a1[j][a[j]] += 1
		a1 = [[0] * (mx + 1)] + a1
		for i in range(1, n + 1):
			for j in range(mx + 1):
				a1[i][j] += a1[i - 1][j]

		def query(l, r):
			if l >= r:
				return 0
			return max([a1[r - 1][j] - a1[l][j] for j in range(mx + 1)])
		dc = [[] for i in range(mx + 1)]
		for i in range(n):
			dc[a[i]].append(i + 1)
		ans = query(0, n + 1)
		for i in range(mx + 1):
			if dc[i]:
				l1 = len(dc[i])
				(l, r) = (0, len(dc[i]) - 1)
				while l < r:
					x = min(l + 1, l1 - r) * 2 + query(dc[i][l], dc[i][r])
					l += 1
					r -= 1
					ans = max(ans, x)
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import os
from io import BytesIO, IOBase
import sys
from collections import defaultdict, deque, Counter
from bisect import *
from math import sqrt
import math
from itertools import permutations

def main():
	for t in range(int(input())):
		n = int(input())
		a = input().split()
		b = a[::-1]
		dp = [[0] * (n + 1) for i in range(n + 1)]
		for i in range(1, n + 1):
			for j in range(1, n + 1):
				dp[i][j] = max(dp[i - 1][j - 1] + (a[i - 1] == b[j - 1]), dp[i - 1][j], dp[i][j - 1])
		if dp[n][n] >= 3:
			print('YES')
		else:
			print('NO')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def help():
	n = int(input())
	arr = [[0] * n for i in range(27)]
	temp = list(map(int, input().split(' ')))
	for i in range(n):
		for j in range(1, 27):
			if temp[i] == j:
				if i == 0:
					arr[j][i] = 1
				else:
					arr[j][i] = arr[j][i - 1] + 1
			elif i == 0:
				arr[j][i] = 0
			else:
				arr[j][i] = arr[j][i - 1]
	ans = 0
	for i in range(1, 27):
		ans = max(ans, arr[i][n - 1])
	arr1 = [[] for i in range(27)]
	for i in range(n):
		arr1[temp[i]].append(i)
	for i in range(1, 27):
		if len(arr1[i]) < 2:
			continue
		for j in range(1, 27):
			if i == j or len(arr1[j]) < 1:
				continue
			start = 0
			end = len(arr1[i]) - 1
			toadd = 2
			while start < end:
				ans = max(ans, toadd + arr[j][arr1[i][end]] - arr[j][arr1[i][start]])
				start += 1
				end -= 1
				toadd += 2
	print(ans)
for qw in range(int(input())):
	help()


def palindrome():
	n = int(input())
	s = set()
	l = list(map(int, input().split()))
	for i in range(1, n):
		if l[i] in s:
			print('YES')
			return 0
		s.add(l[i - 1])
	print('NO')
	return 0
t = int(input())
for _ in range(t):
	palindrome()


from sys import stdin, gettrace
if not gettrace():

	def input():
		return next(stdin)[:-1]

def main():

	def solve():
		n = int(input())
		li = [int(x) for x in input().split()]
		counts = [[0] * 201]
		pos = [[] for i in range(201)]
		for (i, a) in enumerate(li):
			counts.append(list(counts[-1]))
			counts[-1][a] += 1
			pos[a].append(i)
		mx = 0
		for (i, a) in enumerate(li):
			pre_occur_a = counts[i + 1][a]
			pos_suff_occur_a = pos[a][-pre_occur_a]
			if pos_suff_occur_a > i:
				for b in range(201):
					mx = max(mx, counts[pos_suff_occur_a][b] - counts[i + 1][b] + 2 * pre_occur_a)
			elif pos_suff_occur_a == i:
				mx = max(mx, 2 * pre_occur_a - 1)
		print(mx)
	q = int(input())
	for _ in range(q):
		solve()
main()


t = int(input())
while t != 0:
	l = int(input())
	arr = list(map(int, input().split()))
	flag = 0
	for i in range(len(arr)):
		k = arr[i]
		for j in range(i + 2, len(arr)):
			if arr[j] == k:
				flag = 1
				break
	if flag == 1:
		print('YES')
	else:
		print('NO')
	t -= 1


import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
ORDA = 97

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return [int(i) for i in input().split()]

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=2):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	new_number = 0
	while number > 0:
		new_number += number % base
		number //= base
	return new_number

def cdiv(n, k):
	return n // k + (n % k != 0)

def ispal(s):
	for i in range(len(s) // 2 + 1):
		if s[i] != s[-i - 1]:
			return False
	return True

def main():
	for i in range(ii()):
		d = collections.defaultdict(int)
		n = ii()
		arr = li()
		for ele in arr:
			d[ele] += 1
		for key in list(d.keys()):
			if d[key] == 2:
				j = arr.index(key)
				if j + 1 <= n - 1 and arr[j] != arr[j + 1]:
					break
			elif d[key] > 2:
				break
		else:
			print('NO')
			continue
		print('YES')
main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 'NO'
	d = dict()
	for (i, num) in enumerate(a):
		if num not in d:
			d[num] = i
		elif i - d[num] > 1:
			ans = 'YES'
			break
	print(ans)


a = eval(input())
while a > 0:
	a -= 1
	n = eval(input())
	d1 = dict.fromkeys(range(n + 2), 0)
	l = input()
	l = l.split(' ')
	for i in range(0, n):
		d1[l[i]] = i
	c = 0
	for i in range(0, n):
		if d1[l[i]] > i + 1:
			c = 1
	if c == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	s = list(map(int, input().strip().split()))
	k = 0
	for i in range(n):
		for j in range(i + 2, n):
			if s[i] == s[j]:
				k = 1
				break
	if k != 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	t = 0
	for i in range(n):
		if a[i] in a[i + 2:]:
			print('YES')
			t = 1
			break
		else:
			continue
	if t == 0:
		print('NO')


for nt in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	flag = 0
	d = {}
	for i in range(n):
		if l[i] not in d:
			d[l[i]] = [i]
		else:
			d[l[i]].append(i)
	for i in d:
		if len(d[i]) > 2:
			print('YES')
			flag = 1
			break
		elif len(d[i]) == 2:
			if d[i][1] - d[i][0] > 1:
				print('YES')
				flag = 1
				break
	if flag == 0:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	c = 0
	for i in range(0, n - 2):
		for j in range(i + 2, n):
			if A[i] == A[j]:
				c = 1
				break
	if c == 0:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	pos = [[] for i in range(201)]
	for i in range(n):
		pos[a[i]] += [i]
	ans = 0
	for i in range(201):
		l = len(pos[i])
		for j in range(l // 2):
			if l < 2:
				continue
			ini = pos[i][j]
			end = pos[i][l - j - 1]
			mans = 0
			for k in range(201):
				cnt = 0
				for m in pos[k]:
					if m > ini and m < end:
						cnt += 1
				mans = max(mans, cnt)
			ans = max((j + 1) * 2 + mans, ans)
	if ans:
		print(ans)
	else:
		print(1)


t = int(input())
for i in range(t):
	n = int(input())
	mass = [int(x) for x in input().split()]
	f = False
	past = -1
	d = dict()
	for j in range(n):
		if mass[j] not in d:
			d[mass[j]] = 0
		if d[mass[j]] > 1 or (d[mass[j]] == 1 and past != mass[j]):
			f = True
			break
		d[mass[j]] += 1
		past = mass[j]
	if f:
		print('YES')
	else:
		print('NO')


def solve(n, arr):
	for i in range(n):
		for j in range(i + 2, n):
			if arr[i] == arr[j]:
				return 'YES'
	return 'NO'
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(n, arr))


t = int(input())
for i in range(t):
	n = int(input())
	S = list(map(int, input().split()))
	S1 = S.copy()
	S1.pop()
	for elem in S1:
		if S.count(elem) == 1:
			continue
		elif S.count(elem) > 2:
			print('YES')
			break
		elif S[S.index(elem)] != S[S.index(elem) + 1]:
			print('YES')
			break
	else:
		print('NO')


import math
from bisect import bisect_left, bisect_right
from sys import stdin, stdout
from collections import Counter
input = lambda : stdin.readline().strip()
print = stdout.write
T = int(input())
for i in range(T):
	n = int(input())
	ls = list(map(int, input().split()))
	d = {}
	for i in range(len(ls)):
		try:
			d[ls[i]].append(i)
		except:
			d[ls[i]] = [i]
	for (k, v) in d.items():
		v.sort()
		if len(v) > 2:
			print('YES\n')
			break
		for i in range(len(v) - 1):
			if v[i + 1] - v[i] > 1:
				print('YES\n')
				break
		else:
			continue
		break
	else:
		print('NO\n')


n = int(input())
for t in range(n):
	a = int(input())
	b = list(map(int, input().split()))
	for i in range(a - 2):
		if b[i] in b[i + 2:]:
			a = -1
			break
	print('YES' if a == -1 else 'NO')


t = int(input())
for y in range(t):
	d = dict()
	n = int(input())
	a = list(map(int, input().split()))
	prev = -1
	for i in a:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
		if d[i] == 2 and prev != i or d[i] > 2:
			print('YES')
			break
		prev = i
	else:
		print('NO')


for i in range(int(input())):
	lenght_of_array = int(input())
	array = [int(x) for x in input().split()]
	a = 0
	for i in range(lenght_of_array):
		if array[i] in array[i + 2:]:
			a += 1
	if a > 0:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = [str(x) for x in input().strip().split()]
	dic = {}
	i = 0
	flag = False
	for e in a:
		if e not in dic:
			dic[e] = []
		else:
			top = dic[e][-1]
			if i - top > 1:
				flag = True
				break
			elif len(dic[e]) >= 2:
				flag = True
				break
		dic[e].append(i)
		i += 1
	if flag:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	x = int(input())
	a = list(map(int, input().split()))
	flag = 0
	for i in range(x):
		if a[i] in a[i + 2:]:
			flag = 1
			break
	print('YES' if flag else 'NO')


def main():
	n = int(input())
	nums = list(map(int, input().split()))
	m = {}
	for i in range(n):
		if m.get(nums[i]):
			m[nums[i]] = (m[nums[i]][0] + 1, m[nums[i]][1], i)
		else:
			m[nums[i]] = (1, i, i)
	for k in m:
		if m[k][0] > 2:
			return 'YES'
		elif m[k][0] == 2 and m[k][1] + 1 < m[k][2]:
			return 'YES'
	return 'NO'
t = int(input())
for _ in range(t):
	print(main())


for _ in range(int(input())):
	lens = int(input())
	ans = 'NO'
	arrs = [int(x) for x in input().split()]
	for i in range(lens - 2):
		if arrs[i] in arrs[i + 2:]:
			ans = 'YES'
			break
	print(ans)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	C = Counter(a)
	if any((i > 2 for i in C.values())):
		print('YES')
		continue
	d = {}
	for i in range(n):
		d[a[i]] = d.get(a[i], -1)
		if d[a[i]] == -1:
			d[a[i]] = i
			continue
		j = d[a[i]]
		if i - j - 1 > 0:
			print('YES')
			break
	else:
		print('NO')


a = int(input())
for _ in range(a):
	x = int(input())
	li = list(map(int, input().split()))
	for i in range(x - 2):
		if li[i] in li[i + 2:]:
			print('YES')
			break
	else:
		print('NO')


import sys
import heapq
from math import ceil, floor, gcd, fabs, factorial, fmod
from collections import defaultdict as dd, deque, Counter as c
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(var)

def l():
	return list(map(int, data().split()))

def sl():
	return list(map(str, data().split()))

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [[val for i in range(n)] for j in range(m)]
for _ in range(int(data())):
	n = int(data())
	arr = l()
	dp = dd(set)
	for i in range(n):
		dp[arr[i]].add(i)
	result = False
	for i in dp.keys():
		if max(dp[i]) - min(dp[i]) > 1:
			result = True
			break
	if result:
		out('YES\n')
		continue
	out('NO\n')


def lower_bound(l, num):
	if l[0] > num:
		return -1
	lf = 0
	rg = len(l)
	while rg - lf > 1:
		mid = (rg + lf) // 2
		if l[mid] > num:
			rg = mid
		else:
			lf = mid
	return lf

def upper_bound(l, num):
	if l[len(l) - 1] < num:
		return -1
	lb = lower_bound(l, num)
	return lb + 1

def solve():
	import sys
	input = sys.stdin.readline
	t = int(input())
	while t > 0:
		n = int(input())
		a = list(map(int, input().split()))
		unique = {}
		for i in range(n):
			if not unique.get(a[i]):
				unique[a[i]] = 1
		unique = list(map(int, unique.keys()))
		unique = sorted(unique)
		pos = [[] for _ in range(len(unique))]
		for i in range(len(unique)):
			for j in range(n):
				if a[j] == unique[i]:
					pos[i].append(j)
		result = 0
		for i in range(len(unique)):
			n1 = len(pos[i])
			for j in range(len(unique)):
				if i == j:
					result = max(result, len(pos[i]))
				else:
					for k in range(n1 // 2 + 1):
						if k == 0:
							result = max(result, len(pos[j]))
						else:
							cur = k * 2
							mn = pos[i][k - 1]
							mx = pos[i][n1 - k]
							ub = upper_bound(pos[j], mn)
							lb = lower_bound(pos[j], mx)
							if ub == -1 or lb == -1 or ub > lb:
								pass
							else:
								cur += lb - ub + 1
							result = max(result, cur)
		print(result)
		t -= 1
solve()


t = int(input())
for i in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	z = 'NO'
	for i in range(n - 2):
		if arr[i] in arr[i + 2:n]:
			z = 'YES'
			break
	print(z)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	x = set(a)
	x = list(x)
	if len(x) == len(a):
		print('NO')
	else:
		count = 0
		for j in range(len(x)):
			g = x[j]
			if a.count(g) == 2:
				k = a.index(g) + 1
				if a[k] != g:
					count = 1
					print('YES')
					break
			elif a.count(g) > 2:
				count = 1
				print('YES')
				break
		if count == 0:
			print('NO')


from itertools import combinations
for _ in range(int(input())):
	N = int(input())
	L = input().split()
	P = False
	U = {}
	for index in range(N):
		x = L[index]
		if x not in U:
			U[x] = index
		elif U[x] < index - 1:
			P = True
	print('YES' if P else 'NO')


from sys import stdin, gettrace

def input():
	return stdin.buffer.readline()
T = int(input())
for t in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	repeats = [[0] * N for _ in range(200)]
	for i in range(0, N):
		if i > 0:
			for j in range(200):
				repeats[j][i] = repeats[j][i - 1]
		repeats[A[i] - 1][i] += 1
	result = 1
	for a in range(200):
		i1 = 0
		i2 = len(A) - 1
		while i2 > i1:
			while i1 < len(A) and A[i1] - 1 != a:
				i1 += 1
			while i2 >= 0 and A[i2] - 1 != a:
				i2 -= 1
			if i2 > i1:
				c = repeats[A[i1] - 1][i1]
				mr = max((repeats[j0][i2 - 1] - repeats[j0][i1] for j0 in range(200)))
				result = max(result, 2 * c + mr)
			i1 += 1
			i2 -= 1
	print(result)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = False
	for i in range(n):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				ans = True
	if ans:
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline
INF = 2 ** 62 - 1

def read_int():
	return int(input())

def read_int_n():
	return list(map(int, input().split()))

def read_float():
	return float(input())

def read_float_n():
	return list(map(float, input().split()))

def read_str():
	return input().strip()

def read_str_n():
	return list(map(str, input().split()))

def error_print(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time

	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		error_print(e - s, 'sec')
		return ret
	return wrap
from itertools import product
from collections import defaultdict
import bisect

def slv(N, A):
	K = list(set(A))
	idx = defaultdict(list)
	for (i, a) in enumerate(A):
		idx[a].append(i)
	ans = 0
	for (i, j) in product(K, repeat=2):
		if i == j:
			ans = max(ans, len(idx[i]))
		else:
			M = len(idx[i])
			for k in range(0, M // 2):
				li = idx[i][k]
				ri = idx[i][-k - 1]
				ll = bisect.bisect_left(idx[j], li)
				rl = bisect.bisect_left(idx[j], ri)
				ans = max(ans, 2 * (k + 1) + rl - ll)
	return ans

def main():
	T = read_int()
	for _ in range(T):
		N = read_int()
		A = read_int_n()
		print(slv(N, A))
main()


import sys
input = sys.stdin.readline
for j in range(int(input())):
	n = int(input())
	a = list(map(int, input().split(' ')))
	xd = {}
	ans = 'NO'
	for k in range(len(a)):
		if a[k] in xd:
			if xd[a[k]][0] + 1 < k:
				ans = 'YES'
				break
			else:
				xd[a[k]].append(k)
		else:
			xd[a[k]] = [k]
	print(ans)


import bisect
from collections import defaultdict
from collections import Counter
from itertools import product
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
INF = 2 ** 62 - 1

def read_int():
	return int(input())

def read_int_n():
	return list(map(int, input().split()))

def read_float():
	return float(input())

def read_float_n():
	return list(map(float, input().split()))

def read_str():
	return input().strip()

def read_str_n():
	return list(map(str, input().split()))

def error_print(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time

	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		error_print(e - s, 'sec')
		return ret
	return wrap

def slv(N, A):
	K = list(set(A))
	idx = defaultdict(list)
	s = [[0] * (N + 1) for _ in range(201)]
	for i in range(201):
		for (j, a) in enumerate(A):
			if a == i:
				idx[a].append(j)
				c = 1
			else:
				c = 0
			s[i][j + 1] = s[i][j] + c
	ans = 0
	for i in K:
		ans = max(ans, len(idx[i]))
		M = len(idx[i])
		for k in range(0, M // 2):
			li = idx[i][k]
			ri = idx[i][-k - 1]
			for j in range(201):
				if i == j:
					continue
				ans = max(ans, 2 * (k + 1) + s[j][ri] - s[j][li])
	return ans

def main():
	T = read_int()
	for _ in range(T):
		N = read_int()
		A = read_int_n()
		print(slv(N, A))
main()


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = int(input())
from bisect import bisect
from collections import defaultdict
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = defaultdict(list)
	for j in range(n):
		d[l[j]].append(j)
	ans = 0
	for j in d:
		ans = max(ans, len(d[j]))
		for p in d:
			if p != j:
				for k in range(len(d[j]) // 2):
					x = bisect(d[p], d[j][k])
					y = bisect(d[p], d[j][len(d[j]) - 1 - k])
					if y == x:
						break
					ans = max(ans, y - x + 2 * (k + 1))
	print(ans)


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	flag = False
	for j in range(len(arr)):
		for c in range(j + 2, len(arr)):
			if arr[c] == arr[j]:
				flag = True
				break
	print('YES' if flag else 'NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	for i in range(n - 2):
		if l[i] in l[i + 2:]:
			print('YES')
			break
	else:
		print('NO')


import math
from collections import defaultdict
x = int(input())
for _ in range(x):
	n = int(input())
	a = list(map(int, input().split()))
	d = defaultdict(int)
	f = False
	for i in range(n):
		d[a[i]] += 1
		if d[a[i]] >= 2 and i > 0:
			if a[i - 1] != a[i]:
				f = True
				break
			elif i > 1:
				if a[i] == a[i - 1] == a[i - 2]:
					f = True
					break
	if f:
		print('YES')
	else:
		print('NO')


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	got = False
	b = 1
	while not got and b < 2 * n - 1:
		if b % 2:
			(i, j) = ((b - 1) // 2, (b + 1) // 2)
		else:
			(i, j) = (b // 2 - 1, b // 2 + 1)
		(left, right) = (set(a[:i]), set(a[j:]))
		if left & right:
			got = True
		b += 1
	print('YES' if got else 'NO')


from sys import stdin, stdout
from math import *
from heapq import *
from collections import *
res = 0

def bestmatch(a, b):
	global res
	if len(a) + len(b) < res:
		return 0
	subres = len(a)
	lid = 0
	rid = len(a) - 1
	x = 0
	y = len(b) - 1
	la = len(a)
	lb = len(b)
	while lid < rid:
		l = a[lid]
		r = a[rid]
		while x < lb:
			if b[x] <= l:
				x += 1
			else:
				break
		while y > 0:
			if r <= b[y]:
				y -= 1
			else:
				break
		if 0 <= x and x <= y and (y < lb):
			if l < b[x] and b[x] <= b[y] and (b[y] < r):
				subres = max(subres, lid + 1 + (y - x + 1) + (la - rid))
		lid += 1
		rid -= 1
	return subres

def main():
	global res
	ntest = int(stdin.readline())
	for test in range(ntest):
		n = int(stdin.readline())
		a = [int(x) for x in stdin.readline().split()]
		poslist = {}
		for (index, x) in enumerate(a, 1):
			if x not in poslist:
				poslist[x] = []
			poslist[x].append(index)
		res = 0
		all_lists = list(poslist.values())
		all_lists.sort(key=lambda lst: len(lst), reverse=True)
		for id1 in range(len(all_lists)):
			for id2 in range(len(all_lists)):
				res = max(res, bestmatch(all_lists[id1], all_lists[id2]))
		stdout.write('%d\n' % res)
	return 0
main()


I = lambda : map(int, input().split())
for _ in range(int(input())):
	n = int(input())
	l = list(I())
	c = 0
	vis = [-1] * 5005
	for i in range(n):
		if vis[l[i]] != -1:
			if i - vis[l[i]] > 1:
				c = 1
				break
		else:
			vis[l[i]] = i
	if c != 0:
		print('YES')
	else:
		print('NO')


for u in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	ind = [[] for i in range(n + 1)]
	flag = 1
	for i in range(n):
		ind[x[i]] += [i]
		if len(ind[x[i]]) == 2 and ind[x[i]][-1] - ind[x[i]][-2] > 1 or len(ind[x[i]]) >= 3:
			print('YES')
			flag = 0
			break
	if flag:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	s = 0
	for j in range(n):
		m = l[j + 2:]
		for k in m:
			if k == l[j]:
				print('YES')
				s += 1
				break
		if s == 1:
			break
	if s == 0:
		print('NO')


def solve():
	n = int(input())
	lst = list(map(int, input().split()))
	dct = dict()
	p = 1
	for i in lst:
		if dct.get(i, False):
			if p - dct[i] > 1:
				print('YES')
				return
		else:
			dct[i] = p
		p += 1
	print('NO')
for i in range(int(input())):
	solve()


def isPalindrome(a):
	n = len(a)
	for i in range(n // 2):
		if a[i] != a[n - i - 1]:
			return False
	return True
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	check = False
	for i in range(n):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				check = True
	print('YES' if check else 'NO')


t = int(input())
for i in range(t):
	n = int(input())
	nums = list(map(int, input().split()))
	stop = 0
	for k in range(n):
		temp = []
		temp.append(nums[k])
		for j in range(k + 1, n - 1):
			temp.append(nums[j])
			temp.append(nums[j + 1])
			if temp[0] == temp[-1]:
				stop = 1
				break
			else:
				stop = 0
		if stop == 1:
			print('YES')
			break
	if stop == 0:
		print('NO')


for _ in range(int(input())):
	input()
	s = input().split()
	d = {}
	res = 'NO'
	for (i, v) in enumerate(s):
		if v in d:
			if i - d[v] > 1:
				res = 'YES'
				break
		else:
			d[v] = i
	print(res)


for i in range(int(input())):
	n = int(input())
	o = False
	ls = list(map(int, input().split()))
	for i in range(n):
		for j in range(i + 2, n):
			if ls[i] == ls[j]:
				o = True
	print(['NO', 'YES'][o])


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	dup = []
	seen = set()
	prev = a[0]
	a_ = a[1:]
	for x in a_:
		if x in seen:
			print('Yes')
			break
		else:
			seen.add(prev)
			prev = x
	else:
		print('No')


from collections import Counter

def solve(new):
	ans = 0
	x = 0
	cnt = Counter(new)
	y = cnt[1]
	i = 0
	j = len(new) - 1
	while i < j:
		if new[i] == 0:
			if new[j] == 0:
				x += 1
				i += 1
				j -= 1
			else:
				y -= 1
				j -= 1
		else:
			y -= 1
			i += 1
		ans = max(ans, 2 * x + y)
	return ans

def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))
t = ri()
for _ in range(t):
	n = ri()
	numbers = rl()
	cnt = Counter(numbers)
	ans = 0
	for a in cnt.keys():
		for b in cnt.keys():
			if a == b:
				ans = max(cnt[a], ans)
			else:
				new = []
				for i in numbers:
					if i == a:
						new.append(0)
					if i == b:
						new.append(1)
				ans = max(ans, solve(new))
	print(ans)


def max_tbp(xs, a, b, O, C):
	m = 1
	occs = O[a]
	b_cnts = C[b]
	for i in range(len(occs) // 2):
		st = occs[i]
		en = occs[len(occs) - 1 - i]
		cnt = b_cnts[en - 1] - b_cnts[st]
		m = max(m, (i + 1) * 2 + cnt)
	return m
t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	m = 0
	sA = set(A)
	occs = {}
	cnts = {}
	for a in sA:
		a_occs = []
		a_cnts = []
		cur = 0
		for (i, x) in enumerate(A):
			if x == a:
				cur += 1
				a_occs.append(i)
			a_cnts.append(cur)
		occs[a] = a_occs
		cnts[a] = a_cnts
	for a in sA:
		for b in sA:
			m = max(m, max_tbp(A, a, b, occs, cnts))
	print(m)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	counters = [[] for _ in range(len(arr))]
	counter = [0 for _ in range(201)]
	mp = dict()
	for idx in range(len(arr)):
		counter[arr[idx]] += 1
		counters[idx] = counter.copy()
		mp[arr[idx], counter[arr[idx]]] = idx
	ans = 1
	for idx_v in range(len(arr)):
		v = arr[idx_v]
		occ_v = counters[idx_v][v]
		total_occs_v = counters[-1][v]
		symmetric_idx_v = mp[v, total_occs_v - occ_v + 1]
		if idx_v >= symmetric_idx_v:
			continue
		mx = max([second - first for (first, second) in zip(counters[idx_v], counters[symmetric_idx_v - 1])]) + occ_v * 2
		ans = max(ans, mx)
	print(ans)


p = int(input())
for q in range(p):
	n = int(input())
	entrada = list(map(int, input().split(' ')))
	flag = False
	for k in range(n):
		if entrada[k] in entrada[k + 2:]:
			flag = True
	if flag == True:
		print('YES')
	else:
		print('NO')


from collections import defaultdict
for _ in range(int(input())):
	n = input()
	L = [int(x) for x in input().strip().split(' ')]
	hmap = defaultdict(list)
	for (i, x) in enumerate(L):
		hmap[x].append(i)
	res = 'NO'
	for (k, l) in hmap.items():
		if len(l) >= 3:
			res = 'YES'
		if len(l) == 2:
			if l[1] - l[0] > 1:
				res = 'YES'
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	f = []
	for i in range(0, n):
		b.append(0)
	for i in range(0, n):
		f.append(False)
	flag = False
	b[a[0] - 1] += 1
	for i in range(1, n):
		if b[a[i] - 1] >= 1 and f[a[i] - 1]:
			flag = True
			break
		else:
			b[a[i] - 1] += 1
			f[a[i - 1] - 1] = True
	if flag:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [0] * n
	c = [5000] * n
	d = [0] * n
	ss = 0
	for i in range(n):
		k = a[i] - 1
		b[k] += 1
		c[k] = min(c[k], i)
		d[k] = max(d[k], i)
		if b[k] >= 2 and d[k] - c[k] > 1:
			ss = 1
			break
	if ss:
		print('YES')
	else:
		print('NO')


import io
import os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def main():
	for _ in range(int(input())):
		n = int(input())
		arr = list(map(int, input().split()))
		x = set(arr)
		c = [[0] * n for _ in range(200)]
		for (i, val) in enumerate(arr):
			for xx in range(200):
				c[xx][i] += c[xx][i - 1]
			c[val - 1][i] += 1
		ans = max(c, key=lambda y: y[-1])[-1]
		for a in x:
			(l, r) = (0, n - 1)
			for cou in range(c[a - 1][-1] // 2 + 1):
				maxi = 0
				while cou and c[a - 1][l - 1] != cou:
					l += 1
				while c[a - 1][r] != c[a - 1][-1] - cou:
					r -= 1
				for i in x:
					if i == a:
						continue
					maxi = max(maxi, c[i - 1][r] - (0 if l == 0 else c[i - 1][l - 1]))
				ans = max(ans, maxi + 2 * cou)
		print(ans)
main()


p = int(input())
for k in range(p):
	n = int(input())
	a = [int(i) for i in input().split()]
	fl = False
	d = dict()
	for i in range(len(a)):
		if str(a[i]) in d.keys():
			d[str(a[i])] += [i]
		else:
			d[str(a[i])] = [i]
	for key in d:
		if abs(d[key][0] - d[key][-1]) >= 2:
			fl = True
	print('YES' if fl else 'NO')


a = int(input())
for i in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	l = [0] * (b + 1)
	l[c[b - 1]] += 1
	if c[0] != c[1] or (c[0] == c[1] and c[1] == c[2]):
		l[c[0]] += 1
	if c[b - 2] != c[b - 1]:
		l[c[b - 2]] += 1
	for i in range(1, b - 2):
		if c[i] != c[i + 1] or (c[i] == c[i] and c[i + 1] == c[i + 2]):
			l[c[i]] += 1
	if max(l) > 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	seen = {}
	ans = 'NO'
	for i in range(n):
		if arr[i] not in seen:
			seen[arr[i]] = i
		elif i - seen[arr[i]] >= 2:
			ans = 'YES'
	print(ans)


for i in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	d = {}
	for j in range(n):
		d[x[j]] = j + 1 if d.get(x[j], 0) == 0 else d[x[j]] - (j + 1)
	print('YES' if min(d.values()) < -1 else 'NO')


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	a = list(map(int, sys.stdin.readline().split()))
	judge1 = False
	for i in range(n):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				judge1 = True
				break
	if judge1 == True:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	flag = 0
	for j in range(n - 2):
		if a[j] in a[j + 2:]:
			flag = 1
			break
	if flag == 1:
		print('YES')
	else:
		print('NO')


def solve(a):
	for (i, x) in enumerate(a):
		try:
			j = a[i + 2:].index(x)
			return True
		except ValueError:
			continue
	return False
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(s) for s in input().split()]
	print('YES' if solve(a) else 'NO')


tc = int(input())
for i in range(tc):
	nums = int(input())
	vals = str(input())
	j = []
	j = vals.split()
	flag = 1
	for t in range(0, len(j)):
		for i in range(len(j) - 1, t + 1, -1):
			if j[i] == j[t]:
				print('Yes')
				flag = 0
				break
		if flag == 0:
			break
	if flag == 1:
		print('No')


for _ in range(int(input())):
	n = int(input())
	a = input().split()
	occured = set()
	last = None
	for b in a:
		if b in occured:
			print('YES')
			break
		else:
			occured.add(last)
			last = b
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	ind = {}
	for i in range(n):
		if s[i] in ind:
			ind[s[i]].append(i)
		else:
			ind[s[i]] = [i]
	for i in ind:
		if ind[i][-1] - ind[i][0] > 1:
			print('YES')
			break
	else:
		print('NO')


from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline
M = mod = 10 ** 9 + 7

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip('\n').split()]

def st():
	return input().rstrip('\n')

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n')]

def li3():
	return [int(i) for i in input().rstrip('\n')]
for _ in range(val()):
	n = val()
	l = li()
	d = {}
	ans = 'NO'
	for i in range(n):
		if l[i] in d:
			if i - d[l[i]] > 1:
				ans = 'YES'
				break
		else:
			d[l[i]] = i
	print(ans)


T = int(input())
for z in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	c = 0
	for i in range(N):
		l = A[i]
		for j in range(i + 2, N):
			if l == A[j]:
				c = 1
				break
		if c == 1:
			break
	if c == 1:
		print('YES')
	else:
		print('NO')


n = int(input())
for i in range(0, n):
	m = int(input())
	arr = []
	arr = list(map(int, input().split()))
	for j in range(0, m - 2):
		a = False
		if arr[j] in arr[j + 2:]:
			a = True
			break
	if a == False:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	f = 0
	f_i = [-1] * 5005
	l_i = [-1] * 5005
	for i in range(n):
		if f_i[a[i]] == -1:
			f_i[a[i]] = i
		else:
			l_i[a[i]] = max(l_i[a[i]], i)
	for i in range(5005):
		if l_i[i] != -1 and abs(f_i[i] - l_i[i]) > 1:
			f = 1
			break
	if f == 1:
		print('YES')
	else:
		print('NO')


from sys import stdin
import math

def inp():
	return stdin.readline().strip()
t = int(inp())
for _ in range(t):
	n = int(inp())
	a = [int(x) for x in inp().split()]
	d = {}
	for i in range(n):
		if d.get(a[i], 0):
			d[a[i]].append(i)
		else:
			d[a[i]] = [i]
	for i in d.keys():
		if len(d[i]) > 1 and max(d[i]) - min(d[i]) >= 2:
			print('YES')
			break
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	ok = False
	if n < 3:
		print('NO')
		continue
	for i in range(n):
		for j in range(i + 2, n):
			if l[i] == l[j]:
				ok = True
	if ok:
		print('YES')
	else:
		print('NO')


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	ans = 'NO'
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = []
			d[a[i]].append(i)
		else:
			x = a[i]
			if i - d[x][0] > 1:
				ans = 'YES'
				break
	print(ans)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	l = list(map(lambda x: int(x) - 1, input().split()))
	pref = {i: [] for i in range(26)}
	cnt = [0 for i in range(26)]
	counter = []
	for i in range(n):
		pref[l[i]].append(i)
		cnt[l[i]] += 1
		counter.append(cnt.copy())
	ans = 1
	for i in range(26):
		for j in range(1, cnt[i] // 2 + 1):
			(r, l) = (pref[i][-j], pref[i][j - 1])
			curr = 0
			for x in range(26):
				curr = max(curr, counter[r - 1][x] - counter[l][x])
			ans = max(ans, j * 2 + curr)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	if n < 3:
		print('NO')
		continue
	for i in range(n):
		if d.get(l[i]) == i - 1:
			pass
		elif d.get(l[i]) == None:
			d[l[i]] = i
		else:
			print('YES')
			break
	else:
		print('NO')


t = int(input())
for i in range(t):
	f = 0
	n = int(input())
	l = list((int(x) for x in input().strip().split()))[:n]
	for i in range(n):
		if l[i] in l[i + 2:]:
			ind = l[i + 2:].index(l[i]) + i + 2
			if len(l[i + 1:ind]) >= 1:
				print('YES')
				f = 1
				break
	if f == 0:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	d = {}
	ans = 0
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = i
		elif d[a[i]] + 1 != i:
			ans = 1
			break
		else:
			continue
	if ans:
		print('YES')
	else:
		print('NO')


import re
t = int(input())
for _ in range(t):
	n = input()
	a = input() + ' '
	print('YES' if re.search('(\\d+) .* \\1 ', a) else 'NO')


ans = []
t = int(input())
for i in range(t):
	n = int(input())
	d = {}
	L = [int(i) for i in input().split()]
	for (i, c) in enumerate(L):
		try:
			d[c].append(i)
		except:
			d[c] = [i]
	for key in d.keys():
		if len(d[key]) >= 2:
			if d[key][-1] - d[key][0] >= 2:
				ans.append('YES')
				break
	else:
		ans.append('NO')
print('\n'.join(ans))


for _ in range(int(input())):
	n = int(input())
	A = [int(o) - 1 for o in input().split()]
	curr = [0] * 200
	B = [list(curr)]
	C = [[] for i in range(200)]
	for i in range(n):
		a = A[i]
		curr[a] += 1
		B.append(list(curr))
		C[a].append(i)
	ans = max((len(i) for i in C))
	for i in range(200):
		occ = C[i]
		for j in range(len(occ) // 2):
			D1 = B[occ[j] + 1]
			D2 = B[occ[~j]]
			for k in range(200):
				ans = max(ans, 2 * (j + 1) + D2[k] - D1[k])
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	f = False
	for i in range(n - 2):
		if l[i] == l[i + 2]:
			f = True
	last = l[0]
	s = set([last])
	for i in range(1, n):
		if l[i] != last and l[i] in s:
			f = True
		last = l[i]
		s.add(l[i])
	if f:
		print('YES')
	else:
		print('NO')


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def leftBinarySearch(arr, l, r, x):
	temp = -1
	while l <= r:
		mid = (l + r) // 2
		if arr[mid] < x:
			temp = mid
			l = mid + 1
		else:
			r = mid - 1
	return temp

def rightBinarySearch(arr, l, r, x):
	temp = -1
	while l <= r:
		mid = (l + r) // 2
		if arr[mid] > x:
			temp = mid
			r = mid - 1
		else:
			l = mid + 1
	return temp
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	dict1 = {}
	ind = {}
	for i in range(n):
		if arr[i] not in dict1.keys():
			dict1[arr[i]] = 1
			ind[arr[i]] = [i]
		else:
			dict1[arr[i]] += 1
			ind[arr[i]].append(i)
	ans = 0
	for i in dict1.keys():
		ans = max(ans, dict1[i])
	for i in dict1.keys():
		a = dict1[i]
		for j in ind.keys():
			if j != i:
				x = 0
				y = dict1[i] - 1
				for k in range(a // 2):
					ind1 = rightBinarySearch(ind[j], 0, dict1[j] - 1, ind[i][x])
					ind2 = leftBinarySearch(ind[j], 0, dict1[j] - 1, ind[i][y])
					if ind1 != -1 and ind2 != -1 and (ind2 >= ind1):
						ans = max(ans, ind2 - ind1 + 1 + 2 * (x + 1))
					x += 1
					y -= 1
	print(ans)


def func(arr, n):
	for i in range(n):
		for j in range(i + 1, n):
			if arr[j] == arr[i] and j - i > 1:
				return 1
	return 0
t = int(input())
for you in range(t):
	n = int(input())
	l = input().split()
	li = [int(i) for i in l]
	if func(li, n):
		print('YES')
	else:
		print('NO')


t = int(input())

def fonction(n, M):
	if any((M.count(k) >= 2 and n - 1 - M[::-1].index(k) > M.index(k) + 1 for k in set(M))):
		return 'YES'
	else:
		return 'NO'
M = []
for i in range(t):
	n = int(input())
	L = [int(x) for x in input().split()]
	M.append(fonction(n, L))
for k in M:
	print(k)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(c) for c in input().split()]
	dic = {}
	for i in range(len(arr)):
		if arr[i] in dic:
			if dic[arr[i]] < i - 1:
				print('YES')
				break
		else:
			dic[arr[i]] = i
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	ali = list(map(int, input().split()))
	f = 0
	for i in range(n):
		for j in range(i + 2, n):
			if ali[i] == ali[j]:
				f = 1
				break
	if f:
		print('YES')
	else:
		print('NO')


import bisect
for _ in range(int(input())):
	n = int(input())
	a = list(map(lambda x: int(x) - 1, input().split()))
	idx = [[] for _ in range(200)]
	for i in range(len(a)):
		idx[a[i]].append(i)
	mxl = 1
	dp = [[0] * len(a) for _ in range(200)]
	for i in range(len(a)):
		for j in range(200):
			dp[j][i] = dp[j][i - 1]
		dp[a[i]][i] += 1
	for r in range(200):
		mxl = max(mxl, len(idx[r]))
		for c in range(200):
			if c == r:
				continue
			if len(idx[r]) == 0:
				break
			if len(idx[c]) == 0:
				continue
			for i in range(len(idx[r]) // 2):
				k2 = idx[r][-(i + 1)]
				k1 = idx[r][i]
				if k2 <= k1:
					break
				ct = dp[c][k2] - dp[c][k1]
				mxl = max(mxl, 2 * (i + 1) + ct)
	print(mxl)


import math

def test(data):
	for i in range(len(data)):
		for j in range(i + 2, len(data)):
			if data[i] == data[j]:
				print('YES')
				return
	print('NO')
T = int(input())
for i in range(T):
	N = int(input())
	info = list(map(int, input().split()))
	test(info)


t = int(input())
while t != 0:
	n = int(input())
	v = list(map(int, input().split()))
	flag = 0
	if v == v[::-1]:
		flag = 1
	else:
		for i in range(n - 2):
			if v[i] in v[i + 2:]:
				flag = 1
	if flag == 0:
		print('NO')
	else:
		print('Yes')
	t -= 1


from math import factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 1000000007
INF = float('inf')

def main():
	for _ in range(N()):
		n = N()
		arr = list(RL())
		dic = {}
		res = False
		for (i, v) in enumerate(arr):
			if v in dic and dic[v] != i - 1:
				res = True
				break
			else:
				dic.setdefault(v, i)
		if res:
			print('YES')
		else:
			print('NO')
main()


MOD = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
from collections import *
for _ in range(ii()):
	n = ii()
	a = il()
	c = 0
	for i in range(0, n - 2):
		if c >= 1:
			break
		for j in range(i + 2, n):
			if a[i] == a[j]:
				print('YES')
				c = c + 1
				break
	if c == 0:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	okay = 0
	for i in range(1, n):
		b = set(a[0:i])
		c = set(a[i + 1:])
		d = b.intersection(c)
		if len(d) != 0:
			print('YES')
			okay = 1
			break
	if not okay:
		print('NO')


import sys
sys.setrecursionlimit(10 ** 9)
from collections import defaultdict
IA = lambda : map(int, input().split())
T = int(input())
for i in range(0, T):
	n = int(input())
	vis = [int(-1) for i in range(0, n + 1)]
	a = list(IA())
	le = len(a)
	flag = 0
	for i in range(0, le):
		if vis[a[i]] != -1:
			if i - vis[a[i]] > 1:
				flag = 1
				break
		else:
			vis[a[i]] = i
	if flag == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	dict = {}
	for i in range(len(l)):
		dict[l[i]] = [0, 0]
	for i in range(len(l)):
		dict[l[i]][0] = dict[l[i]][0] + 1
		if dict[l[i]][0] < 2:
			dict[l[i]][1] = i
		flag = 0
		if dict[l[i]][0] > 2:
			print('YES')
			flag = 1
			break
		if dict[l[i]][0] == 2:
			if i > dict[l[i]][1] + 1:
				print('YES')
				flag = 1
				break
	if flag == 0:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	entrance = dict()
	a = list(map(int, input().split()))
	for (idx, aa) in enumerate(a):
		if aa not in entrance:
			entrance[aa] = [idx, idx]
		else:
			entrance[aa][1] = idx
	margin = max([b - a for (_, (a, b)) in entrance.items()])
	print('YES' if margin > 1 else 'NO')


import sys
from array import array
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	s = [int(i) for i in input().split()]
	prev = [array('l', (0 for i in range(n))) for i in range(200)]
	pos = [[] for i in range(200)]
	for i in range(n):
		for j in range(200):
			if i == 0:
				continue
			prev[j][i] = prev[j][i - 1]
		prev[s[i]][i] += 1
		pos[s[i]].append(i)
	ans = 0
	for i in range(200):
		for k in range(200):
			if len(pos[i]):
				ans = max(ans, prev[k][pos[i][-1]] - prev[k][pos[i][0]] + 1)
		for j in range(1, len(pos[i]) // 2 + 1):
			m = 0
			for k in range(200):
				m = max(m, prev[k][pos[i][-j] - 1] - prev[k][pos[i][j - 1]])
			ans = max(ans, m + 2 * j)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	s = [int(x) for x in input().split()]
	pos = [-1] * (n + 1)
	cnt = [0] * (n + 1)
	temp = 0
	for i in range(0, len(s)):
		if pos[s[i]] != -1 and pos[s[i]] != i - 1 or cnt[s[i]] > 1:
			temp = 1
			break
		pos[s[i]] = i
		cnt[s[i]] += 1
	if temp == 0:
		print('NO')
	else:
		print('YES')


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	N = int(input())
	arr = [int(x) for x in input().split(' ')]
	b = False
	for x in range(2, N):
		if arr[x] in arr[:x - 1]:
			b = True
			break
	if b:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	l = 0
	a = list(map(int, input().split()))
	c = 1
	for i in range(n - 2):
		if a[i] in a[i + 2:]:
			l = 1
			break
	print('YES' if l == 1 else 'NO')


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	f = 0
	for i in range(len(a)):
		if a[i] in a[i + 2:]:
			f = 1
			break
	print('YES' if f else 'NO')


try:
	t = int(input())
	for _ in range(t):
		n = int(input())
		l = list(map(int, input().split()))
		setl = list(set(l))
		flag = 0
		for i in setl:
			if l.count(i) > 1:
				if n - 1 - l[::-1].index(i) - l.index(i) > 1:
					print('YES')
					flag = 1
					break
		if flag == 0:
			print('NO')
except Exception:
	pass


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	e = 0
	for i in range(n - 1):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				e = 1
	if e == 1:
		print('YES')
	else:
		print('NO')


def solve(a, n):
	if n < 3:
		return False
	d = {}
	d[a[0]] = 1
	for i in range(1, n - 1):
		if a[i - 1] == a[i] == a[i + 1]:
			return True
	for i in range(1, n):
		if a[i] != a[i - 1]:
			d[a[i]] = d.get(a[i], 0) + 1
	for i in d.values():
		if i >= 2:
			return True
	return False
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if solve(a, n):
		print('YES')
	else:
		print('NO')


t = int(input())
while t:
	flag = False
	n = int(input())
	array = list(map(int, input().split()))
	for i in range(len(array)):
		j = len(array) - 1
		while j > i + 1:
			if array[i] == array[j] and j > i + 1:
				flag = True
				break
			j -= 1
		if flag:
			break
	if flag:
		print('YES')
	else:
		print('NO')
	t -= 1


import sys
input = sys.stdin.readline
from collections import defaultdict

def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		pos = defaultdict(list)
		ans = 'NO'
		for i in range(n):
			if not len(pos[a[i]]):
				pos[a[i]].append(i)
				continue
			if i - pos[a[i]][0] >= 2:
				ans = 'YES'
				break
		print(ans)
main()


T = int(input().strip())
for t in range(T):
	N = int(input().strip())
	A = list(map(int, input().split()))
	d = {}
	idx = 0
	for a in A:
		if a in d:
			d[a].append(idx)
		else:
			d[a] = [idx]
		idx += 1
	status = 'NO'
	for item in d:
		l = d[item]
		if len(l) >= 3:
			status = 'YES'
			break
		elif len(l) == 2:
			if l[0] + 1 < l[1]:
				status = 'YES'
				break
		else:
			continue
	print(status)


def solution():
	from sys import stdout, stdin
	(_input, _print) = (stdin.readline, stdout.write)
	(_int, _range, _min, _max) = (int, range, min, max)
	for _ in _range(_int(_input())):
		n = _int(_input())
		arr = [_int(i) for i in _input().split()]
		t = [[0] * 27 for i in _range(n)]
		t[0][arr[0]] += 1
		for i in _range(1, n):
			for j in _range(27):
				if j == arr[i]:
					t[i][j] += t[i - 1][j] + 1
				else:
					t[i][j] = t[i - 1][j]
		ans = 0
		for i in _range(n - 1):
			for e in _range(i + 1, n - 1):
				right = [0, 0]
				right_v = [0, 0]
				mid = [0, 0]
				mid_v = [0, 0]
				for j in _range(27):
					if mid[0] < t[e][j] - t[i][j]:
						mid[1] = mid[0]
						mid_v[1] = mid_v[0]
						mid[0] = t[e][j] - t[i][j]
						mid_v[0] = j
					elif mid[1] < t[e][j] - t[i][j]:
						mid[1] = t[e][j] - t[i][j]
						mid_v[1] = j
					temp = _min(t[-1][j] - t[e][j], t[i][j])
					if temp > right[0]:
						right[1] = right[0]
						right_v[1] = right_v[0]
						right[0] = temp
						right_v[0] = j
					elif temp > right[1]:
						right[1] = temp
						right_v[1] = j
				if right_v[0] != mid_v[0]:
					if right[0] * 2 + mid[0] > ans:
						ans = right[0] * 2 + mid[0]
				if right_v[1] != mid_v[0]:
					if right[1] * 2 + mid[0] > ans:
						ans = right[1] * 2 + mid[0]
				if right_v[0] != mid_v[1]:
					if right[0] * 2 + mid[1] > ans:
						ans = right[0] * 2 + mid[1]
				if right_v[1] != mid_v[1]:
					if right[1] * 2 + mid[1] > ans:
						ans = right[1] * 2 + mid[1]
		print(_max(ans, _max(t[-1])))
solution()


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(k) for k in input().split()]
	yes = False
	occ = {}
	j = 0
	while not yes and j < len(a):
		if a[j] in occ.keys():
			index = occ[a[j]]
			yes = j - index > 1
		else:
			occ[a[j]] = j
		j += 1
	if yes:
		print('YES')
	else:
		print('NO')


def yap(l):
	for i in range(len(l) - 2):
		for j in range(i + 2, len(l)):
			if l[i] == l[j]:
				print('YES')
				return
	print('NO')
	return
n = int(input())
for i in range(n):
	p = int(input())
	t = input()
	l = list(map(int, t.split(' ')))
	yap(l)


tests = int(input())
for _ in range(tests):
	n = int(input())
	a = [int(x) for x in input().split()]
	found = False
	for i in range(n):
		if a[i] in a[i + 2:]:
			found = True
			break
	print('YES' if found else 'NO')


import sys

class E2ThreeBlocksPalindromeHardVersion:

	def solve(self):
		for _ in range(int(input())):
			n = int(input())
			arr = [int(_) for _ in input().split()]
			pos = {x: [] for x in arr}
			for (i, x) in enumerate(arr):
				pos[x].append(i)
			ans = 1
			for a in pos:
				ln = len(pos[a])
				for _x in range(ln // 2):
					x = _x + 1
					(l, r) = (pos[a][_x] + 1, pos[a][ln - _x - 1] - 1)
					cnt = {item: 0 for item in arr[l:r + 1]}
					for v in arr[l:r + 1]:
						cnt[v] += 1
					most_common = max(cnt.values()) if cnt.values() else 0
					ans = max(ans, 2 * x + most_common)
			print(ans)
solver = E2ThreeBlocksPalindromeHardVersion()
input = sys.stdin.readline
solver.solve()


import bisect
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	dic = {}
	for i in range(n):
		if ar[i] in dic:
			dic[ar[i]].append(i)
		else:
			dic[ar[i]] = [i]
	ans = 0
	for i in dic:
		le = len(dic[i])
		ans = max(ans, le)
		for j in range(le // 2, 0, -1):
			l = dic[i][j - 1]
			r = dic[i][-j]
			for k in dic:
				if i != k:
					xx = bisect.bisect(dic[k], l)
					yy = bisect.bisect(dic[k], r) - 1
					ans = max(ans, yy - xx + 1 + 2 * j)
	print(ans)


import collections
local = False
if local:
	file = open('A.txt', 'r')

def IN():
	if local:
		return file.readline()
	else:
		return input()

def solve(arr):
	right = collections.defaultdict(int)
	for n in arr:
		right[n] += 1
	right[arr[0]] -= 1
	left = collections.defaultdict(int)
	left[arr[0]] += 1
	for i in range(1, len(arr) - 1):
		right[arr[i]] -= 1
		if arr[i + 1] in left and left[arr[i + 1]] > 0:
			return 'YES'
		if 2 <= i < len(arr) - 2:
			if arr[i] == arr[i - 1] and arr[i + 1] in left:
				if arr[i] == arr[i + 1] and left[arr[i + 1]] >= 2:
					return 'YES'
				elif arr[i] != arr[i + 1] and left[arr[i + 1]] >= 1:
					return 'YES'
		left[arr[i]] += 1
	return 'NO'
t = int(IN())
for i in range(t):
	n = int(IN())
	arr = [int(_) for _ in IN().split()]
	print(solve(arr))


import sys
import math
import bisect

def solve(A):
	n = len(A)
	d = dict()
	for i in range(n):
		if A[i] not in d:
			d[A[i]] = i
		elif i - d[A[i]] > 1:
			return True
	return False

def main():
	for _ in range(int(input())):
		n = int(input())
		A = list(map(int, input().split()))
		if solve(A):
			print('YES')
		else:
			print('NO')
main()


import sys
input = sys.stdin.readline
T = int(input())
while T:
	T -= 1
	N = int(input())
	vl = [0] * (N + 1)
	nl = list(map(int, input().split()))
	chk = 0
	for i in range(N):
		if vl[nl[i]]:
			if vl[nl[i]] + 1 <= i:
				chk = 1
				break
		else:
			vl[nl[i]] = i + 1
	print('YES' if chk else 'NO')


t = int(input())
a = list()
dp = list()

def getInd(j, i):
	global n, a, dp
	for k in range(n - 1, i, -1):
		bias = 0
		if a[k] == j + 1:
			bias = 1
		if dp[j][n - 1] - dp[j][k] + bias == dp[j][i]:
			return k
	return -1

def getMax(j, i, ind):
	global dp
	if ind - i == 1:
		return 0
	m = 0
	for it in range(26):
		m = max(m, dp[it][ind - 1] - dp[it][i])
	return m
for _ in range(t):
	dp = []
	n = int(input())
	for i in range(26):
		dp.append([0 for j in range(n)])
	a = list(map(int, input().split()))
	for i in range(n):
		for j in range(1, 27):
			if j == a[i]:
				dp[j - 1][i] = dp[j - 1][i - 1] + 1
			else:
				dp[j - 1][i] = dp[j - 1][i - 1]
	ans = 1
	for i in range(n - 1):
		for j in range(26):
			if dp[j][i] <= 0 or dp[j][i] == dp[j][i - 1]:
				continue
			ind = getInd(j, i)
			if ind != -1:
				m = getMax(j, i, ind)
				ans = max(ans, 2 * dp[j][i] + m)
	print(ans)


for i in range(int(input())):
	n = int(input())
	r = 0
	a = [int(x) for x in input().split()]
	if n == 3:
		if a[0] == a[2]:
			print('YES')
			r = 1
		else:
			print('NO')
			r = 1
	else:
		for i in range(n):
			if a.count(a[i]) >= 3:
				print('YES')
				r = 1
				break
			elif a.count(a[i]) == 2:
				if i == 0 or i == n - 1:
					if i == 0:
						if a[i] != a[i + 1]:
							print('YES')
							r = 1
							break
					if i == n - 1:
						if a[i] != a[i - 1]:
							print('YES')
							r = 1
							break
				elif a[i] != a[i - 1] and a[i] != a[i + 1]:
					print('YES')
					r = 1
					break
	if not r:
		print('NO')


t = int(input())
out = []
for i in range(t):
	n = int(input())
	nums = {}
	flag = False
	for (i, num) in enumerate(input().split()):
		if not num in nums:
			nums[num] = [i]
		elif len(nums[num]) >= 2:
			flag = True
			break
		elif nums[num][-1] + 1 == i:
			nums[num].append(i)
		else:
			flag = True
			break
	if flag:
		out.append('YES')
	else:
		out.append('NO')
print('\n'.join(out))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	track = [[] for i in range(5005)]
	for i in range(n):
		track[l[i]].append(i)
	flag = 0
	for temp_list in track:
		if len(temp_list) > 2:
			flag = 1
		elif len(temp_list) > 1:
			for i in range(1, len(temp_list)):
				if temp_list[i] - temp_list[i - 1] > 1:
					flag = 1
					break
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	d = {}
	d[ar[0]] = 0
	flag = 0
	for j in range(1, n):
		if ar[j] in d:
			if j - d[ar[j]] > 1:
				flag = 1
				break
		else:
			d[ar[j]] = j
	if flag == 1:
		print('YES')
	else:
		print('NO')


import sys
for _ in range(int(sys.stdin.readline())):
	n = int(sys.stdin.readline())
	l = list(map(int, sys.stdin.readline().split()))
	d = {}
	for i in range(n):
		if d.get(l[i]) == None:
			d[l[i]] = [i]
		else:
			d[l[i]].append(i)
	ans = 0
	for i in d:
		ind = 0
		left = d[i][ind]
		right = d[i][-1 * ind - 1]
		if left == right:
			leng = 1
		else:
			leng = 2
		while left <= right:
			for k in d:
				temp = 0
				for j in d[k]:
					if left < j < right:
						temp += 1
				ans = max(temp + leng, ans)
			ind += 1
			try:
				left = d[i][ind]
				right = d[i][-1 * ind - 1]
				if left == right:
					leng += 1
				else:
					leng += 2
			except IndexError:
				break
	sys.stdout.write(str(ans))
	sys.stdout.write('\n')


t = int(input())
for _ in range(t):
	n = int(input())
	D = {}
	DC = {}
	DS = {}
	A = list(map(int, input().split()))
	for i in range(n):
		if A[i] in D:
			D[A[i]].append(i)
			DS[A[i]][i] = 1
			DC[A[i]] += 1
		else:
			D[A[i]] = [i]
			DS[A[i]] = [0 for __ in range(n)]
			DS[A[i]][i] = 1
			DC[A[i]] = 1
	for i in DS:
		for k in range(n - 1):
			DS[i][k + 1] += DS[i][k]
	M = max((DC[i] for i in DC))
	for i in D:
		m = DC[i]
		for j in D:
			if i != j:
				for k in range(1, m // 2 + 1):
					(l, r) = (D[i][k - 1], D[i][-k])
					if l != 0:
						cnt = DS[j][r - 1] - DS[j][l - 1]
					else:
						cnt = DS[j][r - 1]
					if M < cnt + k * 2:
						M = cnt + k * 2
	print(M)


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	(l, r) = ([-1 for i in range(n + 1)], [-1 for i in range(n + 1)])
	for i in range(n - 1, -1, -1):
		l[a[i]] = i
	for i in range(n):
		r[a[i]] = i
	for i in range(n + 1):
		if r[i] - l[i] >= 2:
			return 'YES'
	return 'NO'

def main():
	t = int(input())
	for _ in range(t):
		print(solve())
main()


from sys import stdin
input = stdin.readline
import bisect
import math
for xoxo in range(1):
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		b = [[] for i in range(201)]
		c = [0] * 26
		for i in range(n):
			b[a[i]].append(i)
		ans = 1
		for i in range(1, 201):
			for j in range(len(b[i]) // 2):
				(s, e) = (b[i][j], b[i][-1 * (j + 1)])
				ans = max(ans, len(b[i]))
				for k in range(1, 201):
					if k == i:
						continue
					sx = bisect.bisect_left(b[k], s)
					se = bisect.bisect_left(b[k], e)
					if se > sx:
						ans = max(ans, se - sx + (j + 1) * 2)
		print(ans)


def answer(arr):
	result = 'NO'
	if len(arr) > 3:
		for i in range(0, len(arr) - 2):
			if result == 'YES':
				break
			for z in range(len(arr) - 1, i + 1, -1):
				if arr[i] == arr[z]:
					result = 'YES'
					break
	elif arr[0] == arr[2]:
		result = 'YES'
	return result
testCase = int(input())
for _ in range(0, testCase):
	_len = int(input())
	arr = list(map(int, input().split()))
	print(answer(arr))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	flagn = 0
	for i in range(len(arr) - 2):
		flag = 0
		num = None
		for j in range(i + 1, len(arr)):
			if num == None:
				num = arr[j]
				continue
			if arr[i] == arr[j]:
				flag = 1
				flagn = 1
				break
		if flag == 1:
			print('YES')
			break
	if flagn == 0:
		print('NO')


import collections
t = int(input())
for _ in range(t):
	table = collections.defaultdict(list)
	n = int(input())
	a = list(map(int, input().split()))
	for (i, x) in enumerate(a):
		table[x].append(i)
	flag = False
	for x in table:
		if table[x][-1] - table[x][0] > 1:
			flag = True
			break
	if flag:
		print('YES')
	else:
		print('NO')


import bisect
t = int(input())
num = 26
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	prefix = {}
	for i in range(1, num + 1):
		prefix[i] = list()
		prefix[i].append(0)
	for i in range(n):
		for j in range(1, num + 1):
			if arr[i] == j:
				prefix[j].append(prefix[j][-1] + 1)
			else:
				prefix[j].append(prefix[j][-1])
	ans = 0
	for i in range(1, n + 1):
		for j in range(1, num + 1):
			right = prefix[j][i]
			right_ind = i
			left_ind = bisect.bisect_right(prefix[j], prefix[j][-1] - right) - 1
			if left_ind >= len(prefix[j]):
				left_ind = len(prefix[j]) - 1
			left = prefix[j][-1] - prefix[j][left_ind]
			mid = 0
			for g in range(1, num + 1):
				if prefix[g][left_ind] - prefix[g][right_ind] > mid:
					mid = prefix[g][left_ind] - prefix[g][right_ind]
			if left_ind >= right_ind and 2 * min(right, left) + mid > ans:
				ans = 2 * min(right, left) + mid
			if prefix[j][-1] > ans:
				ans = prefix[j][-1]
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	flag = True
	for i in range(0, n):
		if a.count(a[i]) == 2:
			if a.index(a[i]) + 1 != len(a) - a[::-1].index(a[i]) - 1:
				flag = False
				print('YES')
				break
		elif a.count(a[i]) > 2:
			flag = False
			print('YES')
			break
	if flag:
		print('NO')


def get_ans():
	n = int(input())
	arr = list(map(int, input().split()))
	hash_a = {}
	hash_ind = {}
	for i in range(n):
		try:
			hash_a[arr[i]] += 1
		except KeyError:
			hash_a[arr[i]] = 1
		try:
			hash_ind[arr[i]].append(i)
		except KeyError:
			hash_ind[arr[i]] = [i]
	for i in hash_a:
		if hash_a[i] > 2:
			return True
	for i in hash_a:
		if hash_a[i] == 2:
			temp = hash_ind[i]
			for j in range(len(temp) - 1):
				if temp[j + 1] - temp[j] > 1:
					return True
	return False
for _ in range(int(input())):
	ans = get_ans()
	if ans:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	c = [0] * 27
	id = [[] for i in range(27)]
	for i in range(n):
		id[a[i]].append(i)
	for x in a:
		c[x] += 1
	res = max(c)
	for l in range(n - 1):
		r = id[a[l]][-1]
		c = [0] * 27
		for x in a[l:r + 1]:
			c[x] += 1
		if c[a[l]] == 1:
			continue
		c[a[l]] = 0
		(k, t) = (a[l], 0)
		while l < r:
			t += 2
			res = max(res, max(c) + t)
			(l, r) = (l + 1, r - 1)
			while a[l] != k:
				c[a[l]] -= 1
				l += 1
			while a[r] != k:
				c[a[r]] -= 1
				r -= 1
	print(res)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	f = 0
	if n == 3:
		if l[0] == l[2]:
			print('YES')
		else:
			print('NO')
	else:
		for i in range(n):
			tmp = l[i + 2:n]
			if tmp.count(l[i]) > 0:
				f = 1
				print('YES')
				break
		if f == 0:
			print('NO')


t = int(input())
for t1 in range(t):
	n = int(input())
	l = list(map(int, input().split(' ')))
	d = {}
	x = 'NO'
	for i in range(len(l)):
		if d.get(l[i], -1) != -1:
			if d.get(l[i], -1) + 1 != i:
				x = 'YES'
		else:
			d[l[i]] = i
	print(x)


for i in range(int(input())):
	n = int(input())
	s = [int(i) for i in input().split()]
	d = [[0, 0] for i in range(n + 1)]
	e = set()
	f = False
	for i in range(n):
		if s[i] not in e:
			d[s[i]][0] = i
			e.add(s[i])
		else:
			d[s[i]][1] = i
	for i in range(n + 1):
		if d[i][1] - d[i][0] > 1:
			f = True
			break
	if f:
		print('YES')
	else:
		print('NO')


def fun(n, arr):
	if n <= 1:
		return n
	prefix = [[0 for _ in range(n)] for i in range(0, 26)]
	prefix[arr[0] - 1][0] = 1
	for i in range(1, n):
		prefix[arr[i] - 1][i] = prefix[arr[i] - 1][i - 1] + 1
		for k in range(1, 27):
			if k != arr[i]:
				prefix[k - 1][i] = prefix[k - 1][i - 1]
	key = set()
	for i in range(1, 27):
		if prefix[i - 1][-1] == n:
			return n
		if prefix[i - 1][-1] != 0:
			key.add(i - 1)
	ans = -float('inf')
	for split1 in range(-1, n):
		for split2 in range(split1 + 1, n):
			if split1 != -1:
				mx = 0
				for k in key:
					a = prefix[k][split1]
					b = prefix[k][n - 1] - prefix[k][split2]
					if a == b:
						mx = max(a, mx)
				for j in key:
					c = prefix[j][split2] - prefix[j][split1]
					ans = max(2 * mx + c, ans)
			else:
				for k in key:
					ans = max(prefix[k][split2], ans)
	return ans
t = int(input())
while t > 0:
	n = int(input())
	arr = list(map(int, input().split()))
	print(fun(n, arr))
	t -= 1


IA = lambda : map(int, input().split())
T = int(input())
for i in range(0, T):
	n = int(input())
	vis = [int(-1) for i in range(0, n + 1)]
	a = list(IA())
	le = len(a)
	flag = 0
	for i in range(0, le):
		if vis[a[i]] != -1:
			if i - vis[a[i]] > 1:
				flag = 1
				break
		else:
			vis[a[i]] = i
	if flag == 1:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	l = int(input())
	a = list(map(int, input().split()))
	p = 0
	for j in range(l):
		if a[j] in a[j + 2:]:
			p = 1
			break
	if p == 0:
		print('NO')
	else:
		print('YES')


from itertools import combinations
test_count = int(input())

def check_palindromic(arr):
	for i in range(len(arr) // 2):
		if arr[i] != arr[len(arr) - 1 - i]:
			return False
	return True
for i in range(test_count):
	ans_given = False
	col_count = int(input())
	arr = list(map(int, input().split()))
	values = set()
	awaiting = -1
	for j in range(len(arr)):
		pre_await = arr[j]
		if pre_await in values:
			print('YES')
			ans_given = True
			break
		if awaiting != -1:
			values.add(awaiting)
		awaiting = pre_await
	if not ans_given:
		print('NO')


import sys
from itertools import accumulate

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7

def bisearch_max(mn, mx, func):
	ok = mn
	ng = mx
	while ok + 1 < ng:
		mid = (ok + ng) // 2
		if func(mid):
			ok = mid
		else:
			ng = mid
	return ok
MAX = 200
for _ in range(INT()):
	N = INT()
	A = [0] + [a - 1 for a in LIST()]
	acc = list2d(MAX, N + 1, 0)
	for (i, a) in enumerate(A[1:], 1):
		acc[a][i] = 1
	for a in range(MAX):
		for i in range(N):
			acc[a][i + 1] += acc[a][i]
	ans = 0
	prevx = -1
	for a in range(MAX):
		for l in range(N + 1):
			x = acc[a][l]
			if x == prevx:
				continue
			prevx = x
			r = bisearch_max(-1, N + 1, lambda m: acc[a][N] - acc[a][m] >= x)
			if l > r:
				break
			y = 0
			for b in range(MAX):
				y = max(y, acc[b][r] - acc[b][l])
			ans = max(ans, x * 2 + y)
	print(ans)


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
t = int(input())
for tests in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	COUNT = [[0] * 201 for i in range(n)]
	for i in range(n):
		for j in range(1, 201):
			COUNT[i][j] = COUNT[i - 1][j]
		COUNT[i][A[i]] += 1
	ANS = 1
	for i in range(1, 201):
		bind = 0
		eind = n - 1
		score = 0
		while bind < eind:
			while bind < n and A[bind] != i:
				bind += 1
			while eind >= 0 and A[eind] != i:
				eind -= 1
			if bind < eind:
				score += 2
				C = [0] * 201
				for j in range(201):
					C[j] = COUNT[eind - 1][j] - COUNT[bind][j]
				ANS = max(ANS, score + max(C))
				bind += 1
				eind -= 1
	print(ANS)


import sys
input = sys.stdin.readline
n = int(input())
while n:
	n -= 1
	length = int(input())
	nums = list(map(int, input().split()))
	index = {}
	for (idx, num) in enumerate(nums):
		index[num] = idx
	flag = 'NO'
	for (idx, num) in enumerate(nums):
		if index[num] - idx >= 2:
			flag = 'YES'
	print(flag)


from sys import stdin, stdout
import math, sys
from itertools import permutations, combinations
from collections import defaultdict
import bisect
import heapq as hq

def main():
	try:
		for _ in range(int(input())):
			n = int(input())
			l = list(map(int, input().split()))
			q = 0
			for i in range(n - 1):
				for j in range(i + 2, n):
					if l[i] == l[j] and i != j:
						q = 1
						break
			if q == 1:
				print('YES')
			else:
				print('NO')
	except:
		pass
M = 998244353
P = 1000000007
main()


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	res = 'NO'
	for (i, e) in enumerate(a):
		if e in a[i + 2:]:
			res = 'YES'
			break
	print(res)


def main():
	t = int(input())
	while t:
		t += -1
		n = int(input())
		l = list(map(int, input().split()))
		(occ, f) = ([], [])
		for i in range(201):
			occ.append([])
			f.append([0] * (n + 1))
		for i in range(1, 201):
			for j in range(n):
				if l[j] == i:
					occ[i].append(j)
					f[i][j] -= -1
				if j > 0:
					f[i][j] += f[i][j - 1]
		ans = 1
		for i in range(1, 201):
			it = 0
			while 1:
				if it == len(occ[i]):
					break
				(p1, p2) = (occ[i][it], occ[i][-it - 1])
				if p2 <= p1:
					break
				for j in range(1, 201):
					ch = 0
					if l[p1 + 1] == j:
						ch = 1
					ans = max(ans, 2 * (it + 1) + f[j][p2 - 1] - f[j][p1 + 1] + ch)
				it -= -1
		print(ans)
main()


t = int(input())
for i in range(t):
	q = {}
	n = int(input())
	w = [int(k) for k in input().split()]
	for j in range(n):
		if w[j] in q:
			q[w[j]].append(j)
		else:
			q[w[j]] = [j]
	for j in q.keys():
		if len(q[j]) >= 3 or (len(q[j]) == 2 and q[j][1] - q[j][0] != 1):
			print('YES')
			break
	else:
		print('NO')


tc = int(input())
for hj in range(tc):
	a = int(input())
	s = input().split(' ')
	for kl in range(len(s)):
		s[kl] = int(s[kl])
	chai = 0
	for i in range(a):
		if i == 0:
			if s[i] in s[i + 2:]:
				print('YES')
				break
		elif i == a - 1:
			if s[i] in s[0:a - 2]:
				print('YES')
				break
		elif s[i] in s[0:i - 1] or s[i] in s[i + 2:]:
			print('YES')
			break
		elif s[i - 1] == s[i + 1]:
			print('YES')
			break
	else:
		print('NO')


for _ in range(int(input())):
	a = int(input())
	arr = list(map(int, input().split()))
	m = False
	for i in range(a - 2):
		for j in range(i + 2, a):
			if arr[i] == arr[j]:
				print('YES')
				m = True
				break
		if m is True:
			break
	if m is False:
		print('NO')


from functools import cmp_to_key
from collections import deque, Counter
from heapq import heappush, heappop
from math import log, ceil
import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def ii():
	return int(inp())

def si():
	return str(inp())

def li(lag=0):
	l = list(map(int, inp().split()))
	if lag != 0:
		for i in range(len(l)):
			l[i] += lag
	return l

def mi(lag=0):
	matrix = list()
	for i in range(n):
		matrix.append(li(lag))
	return matrix

def lsi():
	return list(map(str, inp().split()))

def print_list(lista, space=' '):
	print(space.join(map(str, lista)))

def bisect_left(a, x):
	left = 0
	right = len(a)
	while left < right:
		mid = (left + right) // 2
		if a[mid] < x:
			left = mid + 1
		else:
			right = mid
	return left

def bisect_right(a, x):
	left = 0
	right = len(a)
	while left < right:
		mid = (left + right) // 2
		if a[mid] > x:
			right = mid
		else:
			left = mid + 1
	return left

def bisect_elements(a, x):
	return bisect_right(a, x) - bisect_left(a, x)
MOD = 10 ** 9 + 7
maxN = 5
FACT = [0] * maxN
INV_FACT = [0] * maxN

def add(x, y):
	return (x + y) % MOD

def multiply(x, y):
	return x * y % MOD

def power(x, y):
	if y == 0:
		return 1
	elif y % 2:
		return multiply(x, power(x, y - 1))
	else:
		a = power(x, y // 2)
		return multiply(a, a)

def inverse(x):
	return power(x, MOD - 2)

def divide(x, y):
	return multiply(x, inverse(y))

def allFactorials():
	FACT[0] = 1
	for i in range(1, maxN):
		FACT[i] = multiply(i, FACT[i - 1])

def inverseFactorials():
	n = len(INV_FACT)
	INV_FACT[n - 1] = inverse(FACT[n - 1])
	for i in range(n - 2, -1, -1):
		INV_FACT[i] = multiply(INV_FACT[i + 1], i + 1)

def coeffBinom(n, k):
	if n < k:
		return 0
	return multiply(FACT[n], multiply(INV_FACT[k], INV_FACT[n - k]))

def create_graph(n, m, undirected=1, unweighted=1):
	graph = [[] for i in range(n)]
	if unweighted:
		for i in range(m):
			[x, y] = li(lag=-1)
			graph[x].append(y)
			if undirected:
				graph[y].append(x)
	else:
		for i in range(m):
			[x, y, w] = li(lag=-1)
			w += 1
			graph[x].append([y, w])
			if undirected:
				graph[y].append([x, w])
	return graph

def create_tree(n, unweighted=1):
	children = [[] for i in range(n)]
	if unweighted:
		for i in range(n - 1):
			[x, y] = li(lag=-1)
			children[x].append(y)
			children[y].append(x)
	else:
		for i in range(n - 1):
			[x, y, w] = li(lag=-1)
			w += 1
			children[x].append([y, w])
			children[y].append([x, w])
	return children

def dist(tree, n, A, B=-1):
	s = [[A, 0]]
	(massimo, massimo_nodo) = (0, 0)
	distanza = -1
	v = [-1] * n
	while s:
		(el, dis) = s.pop()
		if dis > massimo:
			massimo = dis
			massimo_nodo = el
		if el == B:
			distanza = dis
		for child in tree[el]:
			if v[child] == -1:
				v[child] = 1
				s.append([child, dis + 1])
	return (massimo, massimo_nodo, distanza)

def diameter(tree):
	(_, foglia, _) = dist(tree, n, 0)
	(diam, _, _) = dist(tree, n, foglia)
	return diam

def dfs(graph, n, A):
	v = [-1] * n
	s = [[A, 0]]
	v[A] = 0
	while s:
		(el, dis) = s.pop()
		for child in graph[el]:
			if v[child] == -1:
				v[child] = dis + 1
				s.append([child, dis + 1])
	return v

def bfs(graph, n, A):
	v = [-1] * n
	s = deque()
	s.append([A, 0])
	v[A] = 0
	while s:
		(el, dis) = s.popleft()
		for child in graph[el]:
			if v[child] == -1:
				v[child] = dis + 1
				s.append([child, dis + 1])
	return v

def parents_children_root_unrooted_tree(tree, n, root=0):
	q = deque()
	visited = [0] * n
	parent = [-1] * n
	children = [[] for i in range(n)]
	q.append(root)
	while q:
		all_done = 1
		visited[q[0]] = 1
		for child in tree[q[0]]:
			if not visited[child]:
				all_done = 0
				q.appendleft(child)
		if all_done:
			for child in tree[q[0]]:
				if parent[child] == -1:
					parent[q[0]] = child
					children[child].append(q[0])
			q.popleft()
	return (parent, children)

def all_longest_path_passing_from_node(parent, children, n):
	q = deque()
	visited = [len(children[i]) for i in range(n)]
	downwards = [[0, 0] for i in range(n)]
	upward = [1] * n
	longest_path = [1] * n
	for i in range(n):
		if not visited[i]:
			q.append(i)
			downwards[i] = [1, 0]
	while q:
		node = q.popleft()
		if parent[node] != -1:
			visited[parent[node]] -= 1
			if not visited[parent[node]]:
				q.append(parent[node])
		else:
			root = node
		for child in children[node]:
			downwards[node] = sorted([downwards[node][0], downwards[node][1], downwards[child][0] + 1], reverse=True)[0:2]
	s = [node]
	while s:
		node = s.pop()
		if parent[node] != -1:
			if downwards[parent[node]][0] == downwards[node][0] + 1:
				upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][1])
			else:
				upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][0])
		longest_path[node] = downwards[node][0] + downwards[node][1] + upward[node] - min([downwards[node][0], downwards[node][1], upward[node]]) - 1
		for child in children[node]:
			s.append(child)
	return longest_path

def occ(c, letter, L, R):
	return c[letter][R + 1] - c[letter][L]
K = 200
for test in range(ii()):
	n = ii()
	l = li(-1)
	c = [[0 for i in range(n + 1)] for j in range(K)]
	letters = [[] for j in range(K)]
	for j in range(n):
		letters[l[j]].append(j)
	g = 0
	for i in range(K):
		g = max(g, len(letters[i]))
	for j in range(1, n + 1):
		for i in range(K):
			c[i][j] = c[i][j - 1]
		letter = l[j - 1]
		c[letter][j] += 1
	for j in range(n):
		letter = l[j]
		o = occ(c, letter, 0, j)
		index = letters[letter][-o] - 1
		massimo = 0
		if j < index:
			for adj in range(K):
				massimo = max(massimo, occ(c, adj, j + 1, index))
			g = max(g, 2 * o + massimo)
	print(g)


from math import *
from collections import *
from operator import itemgetter
import bisect
ii = lambda : int(input())
iia = lambda : list(map(int, input().split()))
isa = lambda : list(input().split())
I = lambda : list(map(int, input().split()))
chrIdx = lambda x: ord(x) - 96
idxChr = lambda x: chr(96 + x)
t = ii()
for i in range(t):
	n = ii()
	a = iia()
	d = defaultdict(lambda : [])
	for i in range(n):
		d[a[i]].append(i)
	flag = 'NO'
	for i in d:
		if d[i][-1] - d[i][0] > 1:
			flag = 'YES'
			break
	print(flag)


t = int(input())
for i in range(t):
	input()
	L = [int(i) for i in input().split()]
	if len(L) < 3:
		print('NO')
	elif len(L) == 3:
		if L == L[::-1]:
			print('YES')
		else:
			print('NO')
	elif L == L[::-1]:
		print('YES')
	else:
		for i in range(len(L)):
			if L[i] in L[i + 2:]:
				print('YES')
				break
		else:
			print('NO')


import math
N = int(input())
for i in range(0, N):
	n = int(input())
	S = input().split()
	P = []
	for i in range(0, n):
		P.append(int(S[i]))
	ans = 0
	for i in range(1, 27):
		for j in range(1, 27):
			templist = []
			if i == j:
				continue
			inumber = 0
			numj = 0
			for k in range(0, n):
				if P[k] == i:
					templist.append([i, numj])
					inumber = inumber + 1
				if P[k] == j:
					numj = numj + 1
			best = numj
			for k in range(0, math.floor(inumber / 2)):
				if 2 * (k + 1) + templist[inumber - 1 - k][1] - templist[k][1] > best:
					best = 2 * (k + 1) + templist[inumber - 1 - k][1] - templist[k][1]
			if ans < best:
				ans = best
	print(ans)


import sys
input = sys.stdin.readline
from collections import Counter
t = int(input())
for tests in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	CA = Counter(A)
	if max(CA.values()) >= 3:
		print('YES')
		continue
	B = []
	A.append(1 << 28)
	for i in range(n):
		if A[i] != A[i - 1]:
			B.append(A[i])
	C = Counter(B)
	if max(C.values()) > 1:
		print('YES')
	else:
		print('NO')


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
from queue import PriorityQueue
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from itertools import permutations
from math import factorial as f

def ncr(x, y):
	return f(x) // (f(y) * f(x - y))
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	z = set()
	flag = 0
	hash = defaultdict(list)
	for i in range(n):
		if l[i] not in z:
			z.add(l[i])
			hash[l[i]].append(i)
		else:
			if len(hash[l[i]]) == 2:
				flag = 1
				break
			elif abs(hash[l[i]][-1] - i) != 1:
				flag = 1
				break
			hash[l[i]].append(i)
	if flag:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print('YES')
	l = list(map(int, input().split()))
	if n == 2:
		if l[0] == l[1]:
			print('YES')
		else:
			print('NO')
	else:
		x = []
		for (i, j) in enumerate(l):
			x.append([j, i])
		x.sort()
		u = 0
		for i in range(1, n):
			if x[i - 1][0] == x[i][0]:
				if abs(x[i - 1][1] - x[i][1]) > 1:
					print('YES')
					u = 1
					break
		if u != 1:
			for i in range(0, n - 2):
				if x[i][0] == x[i + 2][0]:
					print('YES')
					u = 1
					break
		if u == 0:
			print('NO')


TC = int(input())
for __ in range(TC):
	n = int(input())
	a = [0] + list(map(int, input().split()))
	cnt = [[0 for _ in range(201)] for _ in range(n + 1)]
	prev = [0] * (n + 1)
	pos = [0] * 201
	ans = 0
	for i in range(1, len(a)):
		for j in range(201):
			cnt[i][j] = cnt[i - 1][j]
		cnt[i][a[i]] += 1
		prev[i] = pos[a[i]]
		pos[a[i]] = i
		ans = max(ans, cnt[i][a[i]])
	for i in range(1, len(a)):
		if i >= pos[a[i]]:
			continue
		ans = max(ans, cnt[i][a[i]] * 2 + max((cnt[pos[a[i]] - 1][_] - cnt[i][_] for _ in range(201))))
		pos[a[i]] = prev[pos[a[i]]]
	print(ans)


def foo(ar):
	if ar[0] == ar[2]:
		return True
	se = {}
	for i in range(len(a)):
		if a[i] in se and se[a[i]] == 2:
			return True
		if a[i] in se and (se[a[i]] < 2 and a[i - 1] == a[i]):
			se[a[i]] += 1
		elif a[i] not in se:
			se[a[i]] = 1
		else:
			return True
	return False
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(s) for s in input().split()]
	if foo(a):
		print('YES')
	else:
		print('NO')


def R():
	return map(int, input().split())

def I():
	return int(input())

def S():
	return str(input())

def L():
	return list(R())
from collections import Counter
import math
import sys
from itertools import permutations
import bisect
for _ in range(I()):
	n = I()
	a = L()
	bef = [0] * 27
	aft = [0] * 27
	for i in range(27):
		bef[i] = [0] * n
		aft[i] = [0] * (n + 1)
	bef[a[0]][0] = 1
	for i in range(1, n):
		for k in range(1, 27):
			bef[k][i] = bef[k][i - 1]
		bef[a[i]][i] += 1
	for i in range(n - 1, -1, -1):
		for k in range(1, 27):
			aft[k][i] = aft[k][i + 1]
		aft[a[i]][i] += 1
	ans = 0
	for i in range(n):
		ans = max(ans, aft[a[i]][0])
		for j in range(i, n):
			for k in range(1, 27):
				if i >= 1:
					ans = max(ans, 2 * min(bef[k][i - 1], aft[k][j + 1]) + aft[a[i]][i] - aft[a[i]][j + 1])
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	shan = [-1] * (n + 1)
	siri = 0
	for i in range(n):
		if shan[l[i]] == -1:
			shan[l[i]] = i
		elif shan[l[i]] + 1 != i:
			siri = 1
			break
	if siri:
		print('YES')
	else:
		print('NO')


from collections import defaultdict
for i in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	pos = defaultdict(lambda : [])
	for i in range(len(arr)):
		pos[arr[i]].append(i)
	ans = False
	for ele in pos.values():
		if ele[-1] - ele[0] > 1:
			ans = True
			break
	if ans:
		print('YES')
	else:
		print('NO')


import bisect
for _ in range(int(input())):
	n = int(input())
	a = list(map(lambda x: int(x) - 1, input().split()))
	mxl = 1
	dp = [[0] * len(a) for _ in range(26)]
	for i in range(len(a)):
		for j in range(26):
			dp[j][i] = dp[j][i - 1]
		dp[a[i]][i] += 1
	for i in range(len(a)):
		for j in range(i + 1, len(a)):
			if a[i] == a[j]:
				mxl = max(mxl, dp[a[i]][-1])
				continue
			if dp[a[i]][i] > 0 and dp[a[j]][j] - dp[a[j]][i] > 0 and (dp[a[i]][-1] - dp[a[i]][j] > 0):
				mxl = max(2 * min(dp[a[i]][i], dp[a[i]][-1] - dp[a[i]][j]) + dp[a[j]][j] - dp[a[j]][i], mxl)
	print(mxl)


I = input
for _ in [0] * int(I()):
	n = int(I())
	a = list(map(int, I().split()))
	c = [set() for i in range(n + 1)]
	[c[i].add(p) for (p, i) in enumerate(a)]
	[exec('c[i].add(0)' * (c[i] == set())) for i in range(n + 1)]
	print(['NO', 'YES'][max([max(i) - min(i) > 1 for i in c])])


import os, sys, atexit
from io import BytesIO, StringIO
input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
_OUTPUT_BUFFER = StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
t = int(input())
while t:
	t += -1
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	for i in range(1, 27):
		for j in range(1, 27):
			p1 = 0
			p2 = n - 1
			tmp = 2
			while 1:
				while 1:
					if p1 == n:
						break
					if l[p1] == i:
						break
					p1 -= -1
				while 1:
					if p2 == -1:
						break
					if l[p2] == i:
						break
					p2 += -1
				if p1 == p2:
					ans = max(ans, tmp - 1)
					break
				if p1 == n or p2 == -1:
					break
				if p2 <= p1:
					break
				c = 0
				for ii in range(p1 + 1, p2):
					if l[ii] == j:
						c -= -1
				ans = max(ans, tmp + c)
				p1 -= -1
				p2 += -1
				tmp += 2
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	d = {}
	flag = False
	for i in range(n):
		if i == 0:
			d[A[i]] = 1
		else:
			if A[i] in d:
				if A[i - 1] == A[i]:
					if d[A[i]] >= 2:
						flag = True
				else:
					flag = True
			if A[i] not in d:
				d[A[i]] = 1
			else:
				d[A[i]] += 1
	if flag:
		print('YES')
	else:
		print('NO')


from sys import stdin
input = stdin.readline

def getAns(n, a):
	for i in range(n - 2):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				return True
	return False
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if getAns(n, a):
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline
Q = int(input())
Query = []
for _ in range(Q):
	N = int(input())
	A = list(map(int, input().split()))
	Query.append((N, A))
for (N, A) in Query:
	dic = {}
	for (i, a) in enumerate(A):
		if not a in dic:
			dic[a] = [i]
		else:
			dic[a].append(i)
	ok = False
	for L in dic.values():
		if len(L) > 1 and L[-1] - L[0] > 1:
			ok = True
			break
	print('YES' if ok else 'NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = {}
	last = -1
	f = False
	for i in range(len(a)):
		if not a[i] in m:
			m[a[i]] = 1
			last = a[i]
		elif a[i] == last:
			if m[a[i]] > 1:
				print('YES')
				f = True
				break
			m[a[i]] += 1
		else:
			print('YES')
			f = True
			break
	if not f:
		print('NO')


from sys import stdout, stdin

def palindrom(lst):
	total = len(lst)
	length = total // 2
	for i in range(length):
		if lst[i] != lst[total - i - 1]:
			return False
	return True
for i in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	ok = False
	for i in range(n):
		for j in range(i + 2, n):
			if s[i] == s[j]:
				ok = True
	print('YES' if ok else 'NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	for i in range(n):
		b.append((a[i], i))
	b.sort()
	(x, t) = b[0]
	(y, k) = b[-1]
	if x == y:
		print('YES')
		continue
	flag = False
	for i in range(n - 1):
		(x, t) = b[i]
		(y, k) = b[i + 1]
		if x == y and k - t > 1:
			flag = True
			break
		if i < n - 2:
			(z, q) = b[i + 2]
			if x == z and q - t > 1:
				flag = True
				break
	if flag:
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(i) - 1 for i in input().split()]
	dp = [[0 for _ in range(200)] for _ in range(n)]
	for i in range(n):
		for j in range(200):
			dp[i][j] = dp[i - 1][j]
		dp[i][a[i]] += 1
	ans = 1
	for i in range(200):
		(l, r, count) = (0, n - 1, 0)
		while l < r:
			while l < n and a[l] != i:
				l += 1
			while r > -1 and a[r] != i:
				r -= 1
			if l < r:
				count += 2
				for j in range(200):
					ans = max(ans, count + dp[r - 1][j] - dp[l][j])
				l += 1
				r -= 1
	sys.stdout.write(str(ans) + '\n')


def palidrome(n, array):
	seen = dict()
	for i in range(n):
		if array[i] in seen:
			if i - seen[array[i]] > 1:
				return True
		else:
			seen[array[i]] = i
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 'YES' if palidrome(n, arr) else 'NO'
	print(ans)


for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	pos = {}
	flag = False
	for i in range(n):
		try:
			location = pos[arr[i]]
			if i - location > 1:
				flag = True
				break
		except KeyError:
			pos[arr[i]] = i
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	(n, d, f) = (int(input()), {}, False)
	ls = list(map(int, input().split()))
	for el in range(n):
		if ls[el] not in d:
			d[ls[el]] = el
		elif el - d[ls[el]] > 1:
			f = True
			break
	if f:
		print('YES')
	else:
		print('NO')


from sys import stdin

def iinput():
	return int(stdin.readline())

def minput():
	return map(int, stdin.readline().split())

def linput():
	return list(map(int, stdin.readline().split()))
from collections import defaultdict
from bisect import bisect
t = iinput()
while t:
	t -= 1
	n = iinput()
	a = linput()
	d = defaultdict(list)
	for i in range(n):
		d[a[i]].append(i)
	ans = 1
	for p in d:
		ans = max(ans, len(d[p]))
		for q in d:
			if p != q:
				for i in range(len(d[p]) // 2):
					x = bisect(d[q], d[p][i])
					y = bisect(d[q], d[p][len(d[p]) - 1 - i])
					if y != x:
						ans = max(ans, y - x + 2 * (i + 1))
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	flag = 'NO'
	for x in range(2, n):
		if l[x - 2] in l[x:]:
			flag = 'YES'
			break
	print(flag)


from itertools import combinations
for _ in range(int(input())):
	f = 1
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 3:
		if arr == arr[::-1]:
			print('YES')
		else:
			print('NO')
		continue
	for i in range(n):
		for j in range(i + 2, n):
			if arr[i] == arr[j]:
				f = 0
				print('YES')
				break
		if not f:
			break
	if f:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if any((x in arr[i + 2:] for (i, x) in enumerate(arr))):
		print('YES')
	else:
		print('NO')


t = int(input())
while t > 0:
	n = int(input())
	a = [int(ele) for ele in input().split()]
	dp = []
	for i in range(n):
		lst = []
		for j in range(n):
			lst.append(0)
		dp.append(lst)
	for i in range(n):
		dp[i][i] = 1
	for l in range(1, n):
		for i in range(0, n - l):
			j = i + l
			if a[i] == a[j]:
				dp[i][j] = 2 + dp[i + 1][j - 1]
			else:
				dp[i][j] = max(dp[i + 1][j], dp[i][j - 1])
	maxi = -1
	for i in range(n):
		for j in range(n):
			maxi = max(maxi, dp[i][j])
	if maxi >= 3:
		print('YES')
	else:
		print('NO')
	t -= 1


(z, zz) = (input, lambda : list(map(int, z().split())))
zzz = lambda : [int(i) for i in stdin.readline().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
num = int(z())
ans1 = []
for _ in range(num):
	n = int(z())
	l = zzz()
	ans = 'NO'
	li = set()
	c = 0
	for i in range(n - 2):
		if c == 1:
			c = 0
			continue
		p = l[i]
		t = l[i + 1]
		f = l.count(p)
		if p == l[i + 2] or f >= 3:
			ans = 'YES'
			break
		elif f == 2 and p != t:
			ans = 'YES'
			break
		elif f == 2 and p == t:
			c = 1
	ans1.append(ans)
for i in ans1:
	print(i)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = 0
	found = False
	while l < n:
		r = n - 1
		while r >= 0 and l != r and (a[l] != a[r]):
			r -= 1
		if l + 2 <= r and a[l] == a[r]:
			found = True
			break
		elif l + 1 == r:
			l += 1
		l += 1
	if found:
		print('YES')
	else:
		print('NO')


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil
from collections import defaultdict
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
from collections import deque
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
ips = lambda : stdin.readline().rstrip()
t = ip()
for _ in range(t):
	n = ip()
	arr = list(inp())
	flag = False
	dic = defaultdict(bool)
	pos = defaultdict(int)
	ct = 0
	flag = False
	for i in range(n):
		if not dic[arr[i]]:
			dic[arr[i]] = True
			pos[arr[i]] = i
		elif i - 1 == pos[arr[i]]:
			pass
		else:
			flag = True
			break
	if flag:
		print('YES')
	else:
		print('NO')


import io
import os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def solve():
	n = int(input())
	aa = [int(a) - 1 for a in input().split()]
	counts = [[0] * 200]
	pos = [[] for _ in range(200)]
	for (i, a) in enumerate(aa):
		counts.append(counts[-1].copy())
		counts[-1][a] += 1
		pos[a].append(i)
	mx = 0
	for (i, a) in enumerate(aa):
		ca = counts[i + 1][a]
		ps = pos[a][-ca]
		if ps > i:
			for b in range(200):
				mx = max(counts[ps][b] - counts[i + 1][b] + ca * 2, mx)
		elif ps == i:
			mx = max(ca * 2 - 1, mx)
	print(mx)

def main():
	for t in range(int(input())):
		solve()
main()


def pali(n, A):
	d = {}
	for i in range(n):
		if A[i] in d:
			d[A[i]] += 1
		else:
			d[A[i]] = 1
		if d[A[i]] >= 3:
			return 'YES'
	l = [[A[i], i] for i in range(n)]
	l.sort(key=lambda x: x[0])
	for i in range(1, len(l)):
		if l[i - 1][0] == l[i][0] and abs(l[i - 1][1] - l[i][1]) > 1:
			return 'YES'
	return 'NO'
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(pali(n, arr))


from sys import stdin
inp = lambda : stdin.readline().strip()
t = int(inp())
for _ in range(t):
	n = int(inp())
	a = [int(x) for x in inp().split()]
	num = [[] for i in range(5001)]
	for i in range(n):
		num[a[i]].append(i)
	for i in num:
		if i and max(i) - min(i) > 1:
			print('YES')
			break
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	m = {}
	flag = False
	for j in range(n):
		m[a[j]] = m.get(a[j], 0) + 1
		if m[a[j]] == 2 and (j == 0 or a[j - 1] != a[j]) or m[a[j]] >= 3:
			flag = True
			break
	if flag:
		print('YES')
	else:
		print('NO')


def solve():
	for _ in range(int(input())):
		N = int(input())
		A = [int(x) for x in input().split()]
		flag = False
		if N == 3 and A == A[::-1]:
			print('YES')
			continue
		for i in range(N):
			for j in range(i + 2, N):
				if A[i] == A[j]:
					flag = True
					break
			if flag:
				break
		if flag:
			print('YES')
		else:
			print('NO')
solve()


t = int(input())
for _ in range(t):
	(n, ok) = (int(input()), 0)
	a = list(map(int, input().split()))
	id = [-1] * (n + 1)
	for i in range(n):
		if id[a[i]] == -1:
			id[a[i]] = i
		else:
			ok = ok or i - id[a[i]] > 1
	print('YES' if ok else 'NO')


import math
import sys
readln = sys.stdin.readline

def solveAll():
	nbCases = int(input())
	for _ in range(nbCases):
		case = readCase()
		print(solve(case))

def readCase():
	input()
	return [int(x) for x in readln().split()]

def solve(nums):
	indices = [[] for _ in range(201)]
	for (i, num) in enumerate(nums):
		indices[num] += [i]
	bucket = [0] * 201
	for num in nums:
		bucket[num] += 1
	ans = -math.inf
	for num in range(1, 201):
		ans = max(ans, computeThreeBlock(nums, indices[num], bucket.copy()))
	return ans

def computeThreeBlock(nums, indices, bucket):
	le = 0
	ri = len(nums) - 1
	indicesFrom = 0
	indicesTo = len(indices) - 1
	ans = -math.inf
	while indicesFrom <= indicesTo:
		while le <= indices[indicesFrom]:
			bucket[nums[le]] -= 1
			le += 1
		while ri >= indices[indicesTo]:
			bucket[nums[ri]] -= 1
			ri -= 1
		if indicesFrom < indicesTo:
			ans = max(ans, max(bucket) + 2 * (indicesFrom + 1))
		else:
			ans = max(ans, max(bucket) - 1 + 2 * (indicesFrom + 1))
		indicesFrom += 1
		indicesTo -= 1
	return ans
solveAll()


import sys
from itertools import accumulate

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
for _ in range(INT()):
	N = INT()
	A = [0] + [a - 1 for a in LIST()]
	acc = list2d(26, N + 1, 0)
	for (i, a) in enumerate(A[1:], 1):
		acc[a][i] = 1
	for a in range(26):
		acc[a] = list(accumulate(acc[a]))
	ans = 0
	for i in range(N + 1):
		for j in range(i, N + 1):
			y = 0
			for a in range(26):
				y = max(y, acc[a][j] - acc[a][i])
			x = 0
			for a in range(26):
				x = max(x, min(acc[a][i], acc[a][N] - acc[a][j]))
			ans = max(ans, x * 2 + y)
	print(ans)


q = int(input())
for i in range(q):
	n = int(input())
	a = [int(i) for i in input().split()]
	k = [0 for i in range(n + 1)]
	for i in range(len(a)):
		if k[a[i]] == 1 and a[i] != a[i - 1] or k[a[i]] > 1:
			print('YES')
			break
		k[a[i]] += 1
	else:
		print('NO')


t = int(input())
lst = list()
for l in range(t):
	n = int(input())
	a = input().split()
	flag = 0
	index = 0
	for dig in a:
		if index < len(a) - 1 and dig in a[index + 2:]:
			flag = 1
			break
		index += 1
	if flag == 0:
		lst.append('NO')
	else:
		lst.append('YES')
for i in lst:
	print(i)


def fn():
	input()
	arr = list(map(int, input().split()))
	for (i, a) in enumerate(arr):
		for b in arr[i + 2:]:
			if a == b:
				return True
	return False
for _ in range(int(input())):
	if fn():
		print('YES')
	else:
		print('NO')


import io
import os
from collections import Counter, defaultdict, deque

def solve(N, A):
	isPalindrome = False
	for i in range(N):
		for j in range(i + 2, N):
			if A[i] == A[j]:
				isPalindrome = True
	if isPalindrome:
		return 'YES'
	return 'NO'
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
T = int(input())
for t in range(T):
	(N,) = [int(x) for x in input().split()]
	A = [int(x) for x in input().split()]
	ans = solve(N, A)
	print(ans)


import sys
sys.setrecursionlimit(50000)
input = sys.stdin.readline

def input_list():
	return map(int, input().split())

def r(n_i, n_j, i, j, m=[]):
	n = n_i + n_j
	if n_i == 0:
		return n_j
	elif n_j == 0:
		return n_i
	elif m[0] == m[n - 1] and m[0] == i:
		return 2 + r(n_i - 2, n_j, i, j, m[1:n - 1])
	elif m[0] == j:
		return max(n_j, r(n_i, n_j - 1, i, j, m[1:n]))
	else:
		return max(n_j, r(n_i, n_j - 1, i, j, m[0:n - 1]))

def f():
	n = int(input())
	l = list(input_list())
	f_result = 0
	s = set(l)
	for i in s:
		for j in s:
			m = []
			n_i = 0
			n_j = 0
			for k in l:
				if k == i:
					m.append(k)
					n_i += 1
				elif k == j:
					m.append(k)
					n_j += 1
			f_result = max(f_result, r(n_i, n_j, i, j, m))
	print(f_result)

def main():
	t = int(input())
	for _ in range(t):
		f()
main()


import io
import os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
from sys import stdout
for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().strip().split()))
	b = {}
	c = {}
	mal = 0
	for i in range(n):
		if b.get(a[i]) == None:
			b[a[i]] = [i]
			c[a[i]] = []
		else:
			b[a[i]].append(i)
	for i in c:
		for j in range(n):
			if j != 0:
				c[i].append(c[i][-1] + (a[j] == int(i)))
			else:
				c[i].append(int(a[j] == int(i)))
		mal = max(mal, c[i][-1])
	if mal != n:
		for k in b:
			i = 0
			j = len(b[k]) - 1
			l = b[k][i]
			r = b[k][j]
			while l < r and i < j:
				ma = 0
				for m in b:
					if m != k:
						x = c[int(m)][l]
						y = c[int(m)][r]
						ma = max(ma, y - x)
				mal = max(2 * (i + 1) + ma, mal)
				i = i + 1
				j = j - 1
				l = b[k][i]
				r = b[k][j]
	stdout.write(str(mal) + '\n')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	for i in range(n):
		for j in range(i + 2, n):
			if l[i] == l[j]:
				ans = 1
	if ans == 1:
		print('YES')
	else:
		print('NO')


from collections import defaultdict, Counter
from functools import lru_cache
from heapq import heappush, heappop
from math import gcd, floor, ceil
from sys import stdin, stdout

def ilist():
	return [int(x) for x in stdin.readline().strip().split(' ')]

def iint():
	return int(stdin.readline().strip())

def istr():
	return stdin.readline().strip()

def diff(l2, l1):
	d = [0] * (26 + 1)
	for i in range(len(l1)):
		d[i] = l2[i] - l1[i]
	return d

def solve(a):
	n = len(a)
	count = [[0] * (26 + 1) for _ in range(n + 1)]
	for i in range(n):
		count[i + 1] = count[i][:]
		count[i + 1][a[i]] += 1
	res = 0
	for i in range(n + 1):
		for j in range(i + 1, n + 1):
			l = diff(count[i], count[0])
			m = diff(count[j], count[i])
			r = diff(count[n], count[j])
			outer = 0
			inner = max(m)
			for j in range(1, 26 + 1):
				if l[j] == r[j]:
					outer = max(outer, 2 * l[j])
			res = max(res, outer + inner)
	return res
t = iint()
for _ in range(t):
	n = iint()
	a = ilist()
	print(solve(a))


palindromes = eval(input())
palindromeList = []
for i in range(palindromes):
	extra = input()
	palindrome = input().split(' ')
	palindromeList.append(palindrome)
for palindrome in palindromeList:
	found = False
	for num in range(0, len(palindrome) - 2):
		if palindrome[num] in palindrome[num + 2:]:
			found = True
			print('YES')
			break
	if found == False:
		print('NO')


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	ok = False
	for i in range(n):
		for j in range(n - 1, i, -1):
			if A[i] == A[j] and abs(i - j) > 1:
				ok = True
				break
	if ok:
		print('YES')
	else:
		print('NO')


from math import *
import heapq
T = int(input())
for _ in range(T):
	N = map(int, input().split())
	arr = list(map(int, input().split()))
	Dict = {}
	count = 0
	flag = 1
	for i in arr:
		if i not in Dict:
			Dict[i] = count
		elif Dict[i] + 1 != count:
			print('YES')
			flag = 0
			break
		count += 1
	if flag == 1:
		print('NO')


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = defaultdict(list)
	for (i, item) in enumerate(l):
		d[item].append(i)
	for (key, locs) in d.items():
		if len(locs) > 1:
			res = [locs[i + 1] - locs[i] for i in range(len(locs) - 1)]
			res.sort(reverse=True)
			if res[0] > 1:
				print('YES')
				break
			elif len(res) >= 2:
				if res[-1] == 1 and res[-2] == 1:
					print('YES')
					break
	else:
		print('NO')


import sys
import math
input = sys.stdin.readline
from functools import cmp_to_key

def pi():
	return int(input())

def pl():
	return int(input(), 16)

def ti():
	return list(map(int, input().split()))

def ts():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
mod = 1000000007
f = []

def fact(n, m):
	global f
	f = [1 for i in range(n + 1)]
	f[0] = 1
	for i in range(1, n + 1):
		f[i] = f[i - 1] * i % m

def fast_mod_exp(a, b, m):
	res = 1
	while b > 0:
		if b & 1:
			res = res * a % m
		a = a * a % m
		b = b >> 1
	return res

def inverseMod(n, m):
	return fast_mod_exp(n, m - 2, m)

def ncr(n, r, m):
	if r == 0:
		return 1
	return f[n] * inverseMod(f[n - r], m) % m * inverseMod(f[r], m) % m

def main():
	E2()

def xdfs(root, v, sub, parent):
	st = [root]
	while len(st) > 0:
		node = st.pop()
		for i in range(len(v[node])):
			if v[node][i] != node:
				st.append(v[node][i])

def X():
	try:
		t = pi()
		while t:
			t -= 1
			n = pi()
			v = [[] for i in range(n)]
			for i in range(n - 1):
				[x, y] = ti()
				v[x - 1].append(y - 1)
				v[y - 1].append(x - 1)
			m = pi()
			p = ti()
			e = [0 for i in range(n - 1)]
			sub = [0 for i in range(n)]
			dfs(0, v, sub, -1)
			for i in range(1, n):
				e[i - 1] = sub[i] * (n - sub[i])
			if len(p) < n - 1:
				while len(p) < n - 1:
					p.append(1)
			p = sorted(p)
			if len(p) > n - 1:
				x = 1
				for i in range(n - 2, len(p)):
					x = x * p[i]
				while len(p) > n - 2:
					p.pop()
				p.append(x)
			e = sorted(e)
			res = 0
			for i in range(n - 1):
				res = (res + p[i] * e[i]) % mod
			print(res)
	except:
		print(sys.exc_info())

def bfs(v, root):
	q = [root, None]
	l = 0
	visited = [0 for i in range(len(v))]
	dist = [0 for i in range(len(v))]
	while len(q) > 0:
		node = q.pop(0)
		if node is not None:
			visited[node] = 1
			dist[node] = l
			for i in range(len(v[node])):
				if visited[v[node][i]] == 0:
					visited[v[node][i]] = 1
					q.append(v[node][i])
		else:
			l += 1
			if len(q) != 0:
				q.append(None)
	return dist

def B():
	n = pi()
	v = [[] for i in range(n)]
	for i in range(n - 1):
		[x, y] = ti()
		v[x - 1].append(y - 1)
		v[y - 1].append(x - 1)
	leafs = []
	for i in range(n):
		if len(v[i]) == 1:
			leafs.append(i)
	mn = 1
	d = bfs(v, leafs[0])
	for i in range(1, len(leafs)):
		if d[leafs[i]] % 2 != 0:
			mn = 3
			break
	count = 0
	for i in range(n):
		f = 0
		for j in range(len(v[i])):
			if len(v[v[i][j]]) == 1:
				f = 1
				break
		if f:
			count += 1
	mx = n - 1 - len(leafs) + count
	print(mn, mx)

def E2():
	t = pi()
	while t:
		t -= 1
		(n, a) = (pi(), ti())
		if n == 1:
			print(1)
			continue
		pz = [[] for i in range(200)]
		counts = [[0 for j in range(200)] for i in range(n)]
		for i in range(n):
			for j in range(200):
				if a[i] - 1 == j:
					counts[i][j] = counts[i - 1][j] + 1 if i != 0 else 1
					pz[j].append(i)
				else:
					counts[i][j] = counts[i - 1][j] if i != 0 else 0
		res = 1
		for i in range(200):
			for sz in range(1, n + 1):
				if len(pz[i]) >= 2 * sz:
					st = pz[i][sz - 1]
					en = pz[i][len(pz[i]) - sz]
					mx = 0
					for j in range(200):
						mx = max(mx, counts[en - 1][j] - counts[st][j])
					res = max(res, mx + 2 * sz)
		print(res)
main()


for turn in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	li = [[] for i in range(26)]
	for i in range(n):
		li[lis[i] - 1].append(i)
	ans = 0
	for i in range(26):
		for j in range(26):
			if i == j:
				ans = max(ans, len(li[i]))
			else:
				for k in range(len(li[i]) // 2):
					left = li[i][k]
					right = li[i][-(k + 1)]
					anss = 2 * (k + 1)
					for num in li[j]:
						if left < num < right:
							anss += 1
					ans = max(ans, anss)
	print(ans)


from sys import stdin
AI = 200
N = 2000
occ = [[0 for i in range(N + 1)] for ai in range(AI)]
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	a = [0, *(int(x) - 1 for x in input().split())]
	for ai in range(AI):
		occ[ai][0] = 0
		for i in range(1, n + 1):
			occ[ai][i] = occ[ai][i - 1] + (a[i] == ai)
	s = [[0, *(i for i in range(1, n + 1) if a[i] == ai)] for ai in range(AI)]
	t = [[n, *(i - 1 for i in range(n, 0, -1) if a[i] == ai)] for ai in range(AI)]
	ans = 0
	for ai in range(AI):
		for x in range(occ[ai][n] // 2 + 1):
			for bi in range(AI):
				ans = max(ans, 2 * x + occ[bi][t[ai][x]] - occ[bi][s[ai][x]])
	print(ans)


def solve(arr):
	data = {}
	for (i, num) in enumerate(arr):
		if num in data:
			pos = data[num]
			if i - pos > 1:
				return True and len(arr) >= 3
		else:
			data[num] = i
	return False
tc = int(input())
for _ in range(tc):
	num = int(input())
	arr = [int(i) for i in input().split()]
	assert len(arr) == num, 'Invalid Test Case'
	print('YES' if solve(arr) else 'NO')


t = int(input())

def solve():
	fr = [[0 for i in range(n + 1)] for i in range(26)]
	for i in range(1, n + 1):
		x = a[i - 1]
		fr[x - 1][i] = fr[x - 1][i - 1] + 1
		for j in range(26):
			if x - 1 == j:
				continue
			fr[j][i] = fr[j][i - 1]
	return fr
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	fr = solve()
	ans = 0
	for i in range(n):
		for j in range(i, n):
			y = 0
			x = 0
			for k in range(26):
				y = max(y, fr[k][j + 1] - fr[k][i])
			for k in range(26):
				x = max(x, min(fr[k][i] - fr[k][0], fr[k][n] - fr[k][j + 1]))
			ans = max(ans, 2 * x + y)
	print(ans)


def invert_dict(d):
	my_inverted_dict = dict()
	for (key, value) in d.items():
		my_inverted_dict.setdefault(value, []).append(key)
	return my_inverted_dict

def solve(a):
	temp = dict()
	for i in range(len(a)):
		temp[i + 1] = a[i]
	s = invert_dict(temp)
	for k in s:
		if len(s[k]) > 1:
			if max(s[k]) - min(s[k]) > 1:
				return 'YES'
	return 'NO'
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(arr))


import bisect
from collections import defaultdict
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
for _ in range(iinput()):
	n = iinput()
	a = rlinput()
	d = defaultdict(list)
	for i in range(n):
		d[a[i]].append(i)
	ans = 1
	for i in d:
		temp = d[i]
		l = len(temp)
		ans = max(ans, len(temp))
		for j in range(l // 2):
			start = temp[j]
			end = temp[l - j - 1]
			for k in d:
				if k != i:
					tt = d[k]
					val = bisect.bisect(tt, end) - bisect.bisect(tt, start)
					ans = max(ans, val + 2 * (j + 1))
	print(ans)


from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def I():
	return int(sys.stdin.readline())

def LS():
	return [list(x) for x in sys.stdin.readline().split()]

def S():
	res = list(sys.stdin.readline())
	if res[-1] == '\n':
		return res[:-1]
	return res

def IR(n):
	return [I() for i in range(n)]

def LIR(n):
	return [LI() for i in range(n)]

def SR(n):
	return [S() for i in range(n)]

def LSR(n):
	return [LS() for i in range(n)]
sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():

	def f(a, b):
		if a == b:
			return len(d[a])
		da = d[a]
		db = d[b]
		res = 0
		for x in range(len(da) >> 1):
			l = da[x]
			r = da[-1 - x]
			i = bisect.bisect_left(db, l)
			j = bisect.bisect_left(db, r)
			y = max(0, j - i)
			s = 2 * (x + 1) + y
			if res < s:
				res = s
		return res
	t = I()
	for _ in range(t):
		n = I()
		a = LI()
		m = max(a)
		d = [[] for i in range(m)]
		for i in range(n):
			ai = a[i] - 1
			d[ai].append(i)
		ans = 1
		for a in range(m):
			if not d[a]:
				continue
			for b in range(m):
				if not d[b]:
					continue
				res = f(a, b)
				if ans < res:
					ans = res
		print(ans)
	return
solve()


def solve():
	n = int(input())
	vt = []
	st = input().split(' ')
	for i in st:
		vt.append(int(i))
	g = [[] for i in range(n + 1)]
	for i in range(n):
		g[vt[i]].append(i)
	ans = 0
	for i in range(n + 1):
		print()
		if len(g[i]) == 1:
			continue
		st = -1
		for x in g[i]:
			if st == -1:
				st = x
				continue
			elif x - st > 1:
				ans = 1
				break
	if ans:
		print('YES')
	else:
		print('NO')
	return
t = int(input())
while t > 0:
	solve()
	t -= 1


n = int(input())
for k in range(n):
	j = int(input())
	z = input().split(' ')
	z = [int(i) for i in z]
	d = {}
	flag = 0
	for i in range(len(z)):
		if z[i] in d:
			if i - d[z[i]] >= 2:
				flag = 1
		else:
			d[z[i]] = i
	if flag == 1:
		print('YES')
	else:
		print('NO')


import sys
t = int(input())
for _ in range(t):
	n = int(input())
	occur = [-1] * (n + 1)
	a_list = list(map(int, sys.stdin.readline().split()))
	ok = False
	for (i, a) in enumerate(a_list):
		if occur[a] < 0:
			occur[a] = i
		elif i - occur[a] > 1:
			ok = True
			break
	if ok:
		print('YES')
	else:
		print('NO')


import collections
import time
import os
import sys
import bisect
import heapq
from typing import List

def solve(N, A):
	uniq = list(sorted(set(A)))
	vmap = {v: i for (i, v) in enumerate(uniq)}
	A = [vmap[v] for v in A]
	wc = collections.Counter(A)
	ans = max(wc.values())
	M = len(uniq)
	left = [[0 for _ in range(N)] for _ in range(M)]
	for (i, v) in enumerate(A):
		if i > 0:
			for u in range(M):
				left[u][i] = left[u][i - 1]
		left[v][i] += 1
	right = [[0 for _ in range(N)] for _ in range(M)]
	for i in range(N - 1, -1, -1):
		v = A[i]
		if i < N - 1:
			for u in range(M):
				right[u][i] = right[u][i + 1]
		right[v][i] -= 1
	for i in range(N):
		for u in range(M):
			lc = left[u][i]
			j = bisect.bisect_right(right[u], -lc) - 1
			if j - 1 <= i:
				continue
			rc = abs(right[u][j])
			for v in range(M):
				mc = left[v][j - 1] - left[v][i]
				ans = max(ans, mc + 2 * min(lc, rc))
	return ans
T = int(input())
ans = []
for ti in range(T):
	N = int(input())
	A = [int(x) for x in input().split()]
	ans.append(solve(N, A))
print('\n'.join(map(str, ans)))


t = int(input())

def check(n, arr):
	for i in range(n):
		if arr[i] in arr[i + 2:]:
			return 'YES'
	return 'NO'
while t:
	n = int(input())
	arr = input().split()
	print(check(n, arr))
	t -= 1


n = int(input())
for i in range(n):
	k = int(input())
	a = list(map(int, input().split()))
	f = 0
	for j in range(k):
		for l in range(k - 1, j + 1, -1):
			if a[j] == a[l]:
				f = 1
				break
	if f == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for g in range(t):
	n = int(input())
	L = list(map(int, input().split()))
	maxsize = 0
	for a in range(1, 27):
		for b in range(1, 27):
			Lab = list()
			aocc = 0
			bocc = 0
			for k in range(n):
				if L[k] == a:
					aocc += 1
					Lab.append(L[k])
				elif L[k] == b:
					bocc += 1
					Lab.append(L[k])
			size = aocc + bocc
			i = 0
			j = size - 1
			if i == j:
				maxsize = max(maxsize, bocc)
			x = 0
			aright = 0
			aleft = 0
			while i < j:
				if aleft < x:
					if Lab[i] == a:
						aleft += 1
						i += 1
					else:
						i += 1
						bocc -= 1
				if aright < x:
					if Lab[j] == a:
						aright += 1
						j -= 1
					else:
						j -= 1
						bocc -= 1
				if aright == x and aleft == x:
					maxsize = max(maxsize, 2 * x + bocc)
					x += 1
	print(maxsize)


for _ in [0] * int(input()):
	n = int(input())
	a = list(map(int, input().split()))
	k = True
	for i in range(n - 2):
		for j in range(n - 1, i + 1, -1):
			if a[i] == a[j]:
				print('YES')
				k = False
				break
		if not k:
			break
	if k:
		print('NO')


def solve(a, start, end):
	b = a[start:end + 1]
	c = b[::-1]
	ans = False
	for i in range(0, len(b)):
		if b[i] != c[i]:
			ans = True
	if ans == False and end - start >= 2:
		return 'YES'
	if start + 1 < end:
		return solve(a, start + 1, end)
	if end - 1 > start:
		return solve(a, start, end - 1)
	return 'NO'
t = int(input())
for i in range(0, t):
	n = int(input())
	a = input().split(' ')
	for x in range(0, n):
		a[x] = int(a[x])
	solved = False
	for x in range(0, len(a)):
		for y in range(x + 2, len(a)):
			if a[x] == a[y]:
				solved = True
	if not solved:
		print('NO')
	else:
		print('YES')


def solve():
	n = int(input())
	A = list(map(int, input().split()))
	first = {}
	for (i, a) in enumerate(A):
		if a in first:
			if i > first[a] + 1:
				print('YES')
				return
		else:
			first[a] = i
	print('NO')
t = int(input())
for i in range(t):
	solve()


import sys
import collections
import threading
import string

def sparsetable(a):
	tmp = [0] * 201
	st = []
	for i in range(len(a)):
		tmp[a[i]] += 1
		st.append(tmp[:])
	return st

def func(x, y, st):
	if x >= y:
		return 0
	ans = 0
	for i in range(201):
		ans = max(ans, st[y][i] - st[x][i])
	return ans

def main():
	testn = int(input())
	for _ in range(testn):
		n = int(input())
		a = list(map(int, input().split()))
		d = collections.defaultdict(list)
		st = sparsetable(a)
		for i in range(n):
			d[a[i]].append(i)
		ans = 1
		for key in d:
			z = len(d[key])
			l = d[key]
			for i in range(z // 2):
				ans = max(ans, (i + 1) * 2 + func(l[i], l[z - 1 - i] - 1, st))
		print(ans)
input = sys.stdin.readline
main()


t = int(input())
for i in range(t):
	n = int(input())
	ar = [int(x) for x in input().split()]
	cnt = {}
	for index in range(len(ar)):
		if ar[index] in cnt:
			cnt[ar[index]].append(index)
		else:
			cnt[ar[index]] = [index]
	check = False
	for (keys, value) in cnt.items():
		if len(value) > 2:
			print('YES')
			check = True
			break
		elif len(value) == 2 and abs(value[0] - value[1]) > 1:
			print('YES')
			check = True
			break
	if not check:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	hmap = {}
	flag = 0
	for i in range(n):
		if arr[i] in hmap:
			if i - hmap[arr[i]] + 1 >= 3:
				print('YES')
				flag = 1
				break
		else:
			hmap[arr[i]] = i
	if not flag:
		print('NO')


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	f = False
	for i in range(N):
		if A[i] in A[i + 2:]:
			f = True
			break
	print('YES') if f else print('NO')


import sys
T = int(sys.stdin.readline())
for _ in range(T):
	N = int(sys.stdin.readline())
	list_num = list(map(int, sys.stdin.readline().split()))
	list_check = [0] * 5001
	former = -1
	result = 'NO'
	for i in list_num:
		if former == i:
			former = -1
			continue
		list_check[i] += 1
		former = i
		if list_check[i] >= 2:
			result = 'YES'
			break
	print(result)


import sys
input = lambda : sys.stdin.readline().rstrip()
inp = sys.stdin.buffer.readline

def I():
	return list(map(int, inp().split()))
from collections import Counter
for _ in range(I()[0]):
	(n,) = I()
	a = [int(i) - 1 for i in inp().split()]
	ans = [0] * 26
	ansm = [0] * 26
	for p in range(26):
		i = 0
		j = n - 1
		cur = [-1, -1]
		pcur = [-1, -1]
		maxx = 0
		while i < j:
			maxx = 0
			if a[i] != p:
				i += 1
			else:
				cur[0] = i
			if a[j] != p:
				j -= 1
			else:
				cur[1] = j
			if cur[0] != pcur[0] and cur[1] != pcur[1]:
				pcur = cur[:]
				d = Counter(a[i + 1:j])
				if d:
					if p in d:
						del d[p]
					if d:
						maxx = max(d.values())
				ans[p] += 2
				ansm[p] = max(ansm[p], maxx + ans[p])
				i += 1
				j -= 1
		ansm[p] = max(ans[p], ansm[p])
	d = Counter(a)
	z = max(max(ansm), max(d.values()))
	if z == 0:
		print(1)
	else:
		print(z)


from math import *
from bisect import bisect_left, bisect_right
from queue import Queue
from sys import stdin, stdout
from collections import Counter
input = lambda : stdin.readline().strip()
fast_print = stdout.write
t = int(input())
for _ in range(t):
	n = int(input())
	ls = list(map(int, input().split()))
	pref = []
	for i in range(27):
		pref.append([])
		for j in range(n):
			pref[-1].append(0)
	for i in range(n):
		if i > 0:
			pref[ls[i]][i] = pref[ls[i]][i - 1] + 1
			for j in range(27):
				if j != ls[i]:
					pref[j][i] = pref[j][i - 1]
		else:
			pref[ls[i]][i] += 1
	ans = 0
	for i in range(n):
		for j in range(i, n):
			middle = 0
			for k in range(27):
				if i > 0:
					middle = max(middle, pref[k][j] - pref[k][i - 1])
				else:
					middle = max(middle, pref[k][j])
			front = 0
			for k in range(27):
				if i > 0:
					temp = min(pref[k][i - 1], pref[k][n - 1] - pref[k][j])
					front = max(front, temp)
				else:
					temp = 0
					front = max(front, temp)
			ans = max(ans, middle + 2 * front)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	res = False
	seen = {}
	for (idx, num) in enumerate(lst):
		if num not in seen:
			seen[num] = idx
		elif idx - seen[num] >= 2:
			res = True
	print('YES' if res else 'NO')


tc = int(input())
for ll in range(tc):
	l = int(input())
	s = list(map(int, input().split()))
	arr = [[0] * (l + 1) for i in range(27)]
	su = [[] for i in range(27)]
	for j in range(l):
		for i in range(1, 27):
			arr[i][j + 1] = arr[i][j]
		arr[s[j]][j + 1] = arr[s[j]][j] + 1
		su[s[j]].append(j + 1)
	ans = 1
	for i in range(1, 27):
		if len(su[i]) > 1:
			for k in range(len(su[i]) // 2):
				cur = (k + 1) * 2
				for j in range(1, 27):
					newans = cur + arr[j][su[i][len(su[i]) - 1 - k]] - arr[j][su[i][k]]
					if i == j:
						newans -= 1
					ans = max(newans, ans)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	count = {}
	ans = False
	for i in range(n):
		try:
			count[arr[i]].append(i)
			if len(count[arr[i]]) > 1 and abs(count[arr[i]][0] - count[arr[i]][-1]) > 1:
				ans = True
				break
		except KeyError:
			count[arr[i]] = [i]
	if ans:
		print('YES')
	else:
		print('NO')


t = int(input())
for ti in range(t):
	n = int(input())
	a = [int(i) - 1 for i in input().split()]
	dp = [[0 for i in range(200)] for i in range(n)]
	for i in range(n):
		for j in range(200):
			dp[i][j] = dp[i - 1][j]
		dp[i][a[i]] += 1
	m = 1
	for i in range(200):
		(s, e, c) = (0, n - 1, 0)
		while s < e:
			while s < e and a[s] != i:
				s += 1
			while s < e and a[e] != i:
				e -= 1
			if s < e:
				c += 2
				for j in range(200):
					m = max(m, c + dp[e - 1][j] - dp[s][j])
				s += 1
				e -= 1
	print(m)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	dp = [[0 for i in range(n + 1)] for j in range(27)]
	for i in range(n):
		dp[a[i]][i + 1] += 1
		for j in range(1, 27):
			dp[j][i + 1] += dp[j][i]
	ans = 1
	for i in range(1, 27):
		l = 0
		r = n - 1
		c = 0
		while l < r:
			while l < r and a[l] != i:
				l += 1
			while l < r and a[r] != i:
				r -= 1
			if l < r and a[l] == i and (a[r] == i):
				m = 0
				for j in range(1, 27):
					m = max(m, dp[j][r] - dp[j][l + 1])
				ans = max(c + m + 2, ans)
				c += 2
				l += 1
				r -= 1
	print(ans)


t = int(input())
while t > 0:
	n = int(input())
	a = [int(x) for x in input().split()]
	a = a[:n]
	flag = 0
	for i in range(n - 2):
		j = n - 1
		while j > i + 1:
			if a[i] == a[j]:
				flag = 1
				break
			j -= 1
		if flag == 1:
			break
	if flag:
		print('YES')
	else:
		print('NO')
	t -= 1


def e(arr, n):
	dp = [[0 for _ in range(n)] for __ in range(n)]
	for i in range(n):
		dp[i][i] = 1
	for d in range(1, n):
		for i in range(n - d):
			j = i + d
			if arr[i] == arr[j]:
				dp[i][j] = dp[i + 1][j - 1] + 2
				if dp[i][j] >= 3:
					print('YES')
					return
			else:
				dp[i][j] = max(dp[i + 1][j], dp[i][j - 1])
	print('YES' if dp[0][-1] >= 3 else 'NO')
n = int(input())
while n:
	length = int(input())
	array = [int(el) for el in input().split(' ')]
	e(array, length)
	n -= 1


import sys, math
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
t = 1
t = inp()
for tt in range(t):
	n = inp()
	a = inlt()
	q = 'NO'
	for i in range(len(a) - 2):
		if a[i + 2] == a[i]:
			q = 'YES'
			break
	for i in range(len(a)):
		if i - a.index(a[i]) >= 2:
			q = 'YES'
			break
	print(q)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	c = 0
	g = [-1] * 6000
	for j in range(n):
		if g[l[j]] != -1:
			if abs(g[l[j]] - j) > 1:
				c = 1
				break
		else:
			g[l[j]] = j
	if c == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	ok = False
	for i in range(n):
		if a[i] in a[i + 2:]:
			ok = True
			break
	if ok == True:
		print('YES')
	else:
		print('NO')


T = int(input())
for _ in range(T):
	n = int(input())
	arr = list(map(int, input().split()))
	ht = {}
	dups = -1
	found = False
	for (i, a) in enumerate(arr):
		if a in ht:
			if ht[a] < i - 1 or ht[a] < dups:
				found = True
				break
			else:
				dups = i
		else:
			ht[a] = i
	if found:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = tuple(map(int, input().split()))
	uniq_arr = set(arr)
	pal = False
	for ele in uniq_arr:
		if arr.count(ele) > 2:
			pal = True
			break
		elif arr.count(ele) == 2:
			ind = arr.index(ele)
			if arr[ind] != arr[ind + 1]:
				pal = True
				break
	if pal:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split(' ')))
	m = [0 for i in range(n + 1)]
	for i in l:
		m[i] += 1
	if max(m) >= 3:
		print('YES')
	else:
		s = 0
		for i in range(n):
			a = l[i]
			if m[a] == 2 and a in l[i + 1:]:
				if l[i + 1:].index(a) != 0:
					s = -1
					break
		if s == -1:
			print('YES')
		else:
			print('NO')


import sys
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LI1():
	return list(map(int1, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]

def main():
	for _ in range(II()):
		n = II()
		aa = LI1()
		cs = [[0] * (n + 1) for _ in range(200)]
		pos = [[] for _ in range(200)]
		for (i, a) in enumerate(aa):
			cs[a][i + 1] += 1
			pos[a].append(i)
		for a in range(200):
			csa = cs[a]
			for i in range(n):
				csa[i + 1] += csa[i]
		ans = max((cs[a][-1] for a in range(200)))
		for a in range(200):
			posa = pos[a]
			an = len(posa)
			for i in range(an // 2):
				(l, r) = (posa[i], posa[an - 1 - i])
				cur = (i + 1) * 2 + max((cs[a][r] - cs[a][l + 1] for a in range(200)))
				if cur > ans:
					ans = cur
		print(ans)
main()


t = int(input())

def solve(n, nums):
	cnts = {}
	ids = {}
	for (i, num) in enumerate(nums):
		if num not in cnts.keys():
			cnts[num] = 0
			ids[num] = (n + 1, -1)
		cnts[num] += 1
		mx = max(ids[num][1], i)
		mn = min(ids[num][0], i)
		ids[num] = (mn, mx)
	for k in cnts.keys():
		if cnts[k] > 1 and ids[k][1] - ids[k][0] > 1:
			return True
	return False
for _ in range(t):
	n = int(input())
	nums = map(int, input().split())
	print('YES' if solve(n, nums) else 'NO')


N = int(input())
for _ in range(N):
	n = int(input())
	lst = list(map(int, input().split()))
	s = set(lst)
	if len(s) == len(lst):
		print('NO')
	else:
		d = {}
		for i in range(n):
			cur = lst[i]
			if cur in d:
				d[cur].append(i)
			else:
				d[cur] = [i]
		found = False
		for num in d:
			ind = d[num]
			if ind[0] + 1 != ind[-1] and len(ind) > 1:
				print('YES')
				found = True
				break
		if not found:
			print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	LEK = list(map(int, input().split()))
	ANS = 'NO'
	for i in range(n):
		if ANS == 'NO':
			for j in range(n - 1, i + 1, -1):
				if ANS == 'NO' and LEK[i] == LEK[j]:
					ANS = 'YES'
		else:
			i = n - 1
	print(ANS)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
from collections import defaultdict
from itertools import permutations
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	pre = [[0 for i in range(n + 1)] for j in range(200)]
	pos = [[] for j in range(200)]
	for i in range(n):
		for j in range(200):
			if a[i] == j + 1:
				pre[j][i + 1] = pre[j][i] + 1
			else:
				pre[j][i + 1] = pre[j][i]
		pos[a[i] - 1].append(i)
	mx = 0
	for i in range(200):
		mx = max(mx, pre[i][-1])
		k = len(pos[i]) // 2
		for j in range(k):
			l = pos[i][j] + 1
			r = pos[i][len(pos[i]) - 1 - j]
			curr = 0
			for x in range(200):
				curr = max(curr, pre[x][r] - pre[x][l])
			mx = max(mx, curr + 2 * (j + 1))
	print(mx)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	flag = 'NO'
	for x in range(2, n):
		if arr[x - 2] in arr[x:]:
			flag = 'YES'
			break
	print(flag)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for (i, item) in enumerate(a):
		if item in d:
			d[item] += 1
			if d[item] == 2:
				if a[i - 1] != a[i]:
					print('YES')
					break
			elif d[item] > 2:
				print('YES')
				break
		else:
			d[item] = 1
	else:
		print('NO')


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)

def in_int():
	return int(input())

def in_list():
	return list(map(int, input().split()))

def in_str():
	s = input()
	return list(s[:len(s) - 1])

def in_ints():
	return map(int, input().split())
t = in_int()
for tt in range(t):
	n = in_int()
	nums = in_list()
	count = []
	tmp = [0] * 27
	a = [[] for i in range(27)]
	i = 0
	for xx in nums:
		tmp[xx] += 1
		count.append(list(tmp))
		a[xx].append(i)
		i += 1

	def rc(i, j, c):
		if i > j:
			return 0
		if i == 0:
			return count[j][c]
		else:
			return count[j][c] - count[i - 1][c]

	def mc(i, j):
		if i == 0:
			mx = 0
			for xx in range(1, 27):
				mx = max(mx, count[j][xx])
			return mx
		else:
			mx = 0
			for c in range(1, 27):
				mx = max(mx, count[j][c] - count[i - 1][c])
			return mx
	ans = mc(0, n - 1)
	for aa in a:
		i = 0
		j = len(aa) - 1
		while i < j:
			ans = max(ans, 2 * (i + 1) + mc(aa[i] + 1, aa[j] - 1))
			i += 1
			j -= 1
	print(ans)


T = int(input())
ans = ''
for _ in range(T):
	n = int(input())
	a = [int(i) for i in input().split()]
	s = [0 for i in range(n)]
	for i in range(n):
		s[a[i] - 1] += 1
		if s[a[i] - 1] == 3:
			ans += 'YES'
			break
		elif s[a[i] - 1] == 2 and a[i] != a[i - 1]:
			ans += 'YES'
			break
		elif i == n - 1:
			ans += 'NO'
	ans += '\n'
print(ans)


class UnionFind:

	def __init__(self):
		self.size = 0
		self.numComponents = 0
		self.szs = {}
		self.ids = {}
		self.last = -1

	def add(self, p):
		self.size += 1
		self.numComponents += 1
		self.szs[p] = 1
		self.ids[p] = p
		self.last = p

	def mylast(self):
		return self.last

	def plus(self, p):
		self.szs[p] += 1
		return self.szs[p]

	def contains(self, p):
		return self.ids.get(p, None) != None

	def find(self, p):
		r = p
		while r != self.ids[r]:
			r = self.ids[r]
		while p != r:
			pp = self.ids[p]
			self.ids[p] = r
			p = pp
		return r

	def connected(self, p, q):
		return self.find(p) == self.find(q)

	def componentsSize(self, p):
		return self.szs[self.find(p)]

	def size(self):
		return self.size

	def components(self):
		return self.numComponents

	def unify(self, p, q):
		root1 = self.find(p)
		root2 = self.find(q)
		if root1 == root2:
			return
		if self.szs[root1] < self.szs[root2]:
			self.szs[root2] = self.szs[root2] + self.szs[root1]
			self.ids[root1] = root2
		else:
			self.szs[root1] = self.szs[root1] + self.szs[root2]
			self.ids[root2] = root1
		self.numComponents -= 1

	def minmax(self):
		mini = self.size
		maxi = 0
		for i in self.ids:
			if i == self.ids[i]:
				mini = min(mini, self.szs[i])
				maxi = max(maxi, self.szs[i])
		return (mini, maxi)
t = int(input())
i = 0
while i < t:
	a = int(input())
	x = list(map(int, str(input()).split()))
	uf = UnionFind()
	j = 0
	while j < a:
		if uf.contains(x[j]):
			if uf.mylast() != x[j]:
				print('YES')
				break
			else:
				cc = uf.plus(x[j])
				if cc > 2:
					print('YES')
					break
		else:
			uf.add(x[j])
		j += 1
	else:
		print('NO')
	i += 1


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	P = [[] for _ in range(27)]
	S = set(A)
	for (i, a) in enumerate(A):
		P[a].append(i)
	ans = 0
	for a in S:
		for i in range(0, len(P[a]) // 2):
			minp = P[a][i]
			maxp = P[a][-(1 + i)]
			s = 0
			for b in S:
				if a == b:
					continue
				s = max(s, sum([p > minp and p < maxp for p in P[b]]))
			ans = max(ans, (i + 1) * 2 + s)
	ans = max(ans, max([len(p) for p in P]))
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	ans = 'NO'
	for i in range(n - 2):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				ans = 'YES'
	print(ans)


for t in range(int(input())):
	n = int(input())
	flag = 0
	a = list(map(int, input().split()))
	for i in range(1, n - 1):
		x = set()
		y = set()
		for j in range(0, i):
			x.add(a[j])
		for j in range(i + 1, n):
			y.add(a[j])
		for m in range(1, n + 1):
			if m in x and m in y:
				flag = 1
				break
		if flag:
			break
	if flag == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	l1 = list(map(int, input().split(' ')))
	l2 = l1.copy()
	l1.reverse()
	count = 0
	for k in range(len(l2)):
		idx = l1.index(l2[k])
		indexx = len(l1) - 1 - idx
		if indexx == k:
			pass
		elif indexx - k >= 2 or k - indexx >= 2:
			count = count + 1
	if count > 0:
		print('YES')
	else:
		print('NO')


test = int(input())
for _ in range(test):
	n = int(input())
	flag = 0
	arr = list(map(int, input().split()))
	for i in range(n - 1):
		for j in range(i + 1, n):
			if arr[i] == arr[j]:
				diff = abs(j - i)
				if diff > 1:
					flag = 1
					break
	if flag == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	pos = {}
	for i in range(n):
		if a[i] not in pos:
			pos[a[i]] = []
			pos[a[i]].append(i)
		else:
			pos[a[i]].append(i)
	ok = False
	for i in pos:
		arr = pos[i]
		if len(arr) > 2:
			ok = True
		for j in range(1, len(arr)):
			if arr[j] - arr[j - 1] > 1:
				ok = True
	if ok:
		print('YES')
	else:
		print('NO')


import sys
import math
import copy
from collections import Counter
debug = 1
readln = sys.stdin.readline

def readint():
	return int(readln().strip())

def readints():
	return map(int, readln().split())

def readstr():
	return readln().strip()

def readstrs():
	return readln().split()

def dbg(*args):
	if debug:
		print(' '.join(map(str, args)))

def subr(i, j, c, appears):
	if j < i:
		return 0
	return appears[j][c] - (appears[i - 1][c] if i > 0 else 0)

def solve(n, A):
	pos = [[] for _ in range(201)]
	appears = []
	for (i, a) in enumerate(A):
		pos[a].append(i)
		if i > 0:
			appears.append(appears[i - 1].copy())
		else:
			appears.append([0] * 201)
		appears[i][a] += 1
	sortpos = sorted([(len(x), i, x) for (i, x) in enumerate(pos)], reverse=True)
	ans = 1
	for (xlen, x, posx) in sortpos:
		if xlen <= 0:
			break
		for i in range(len(posx) // 2):
			xnum = (i + 1) * 2
			(left, right) = (posx[i] + 1, posx[len(posx) - i - 1] - 1)
			for (yposes, y, _) in sortpos:
				if xnum + yposes <= ans:
					break
				ynum = subr(left, right, y, appears)
				ans = max(ans, xnum + ynum)
	return ans
t = readint()
for _ in range(t):
	n = readint()
	A = readints()
	print(solve(n, A))


for test in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.append(10000000009)
	c = 0
	for i in range(n - 2):
		if l[i] in l[i + 2:]:
			print('YES')
			c = 1
			break
	if c == 0:
		print('NO')


from collections import Counter, OrderedDict
from itertools import permutations as perm
from collections import deque
from sys import stdin
from bisect import *
from heapq import *
import math
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1000000000.0) + 7
inf = float('inf')
(t,) = gil()
for q in range(t):
	(n,) = gil()
	a = gil()
	b = [[0 for _ in range(n)] for _ in range(27)]
	f = [[0 for _ in range(n)] for _ in range(27)]
	ans = 1
	for (k, v) in Counter(a).items():
		ans = max(ans, v)
	for i in range(n):
		f[a[i]][i] = 1
		b[a[i]][i] = 1
	for i in range(1, 27):
		for j in range(1, n):
			f[i][j] += f[i][j - 1]
		for j in reversed(range(n - 1)):
			b[i][j] += b[i][j + 1]
	for i in range(n):
		for j in range(i, n):
			emax = 0
			mmax = 0
			if i and j + 1 < n:
				for k in range(1, 27):
					emax = max(emax, min(f[k][i - 1], b[k][j + 1]))
			for k in range(1, 27):
				l = f[k][i - 1] if i else 0
				r = b[k][j + 1] if j + 1 < n else 0
				mmax = max(mmax, b[k][0] - l - r)
			ans = max(ans, mmax + 2 * emax)
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	m = 0
	for j in range(n - 1):
		if a.count(a[j]) > 2:
			print('YES')
			break
		elif a[j] != a[j + 1] and a[j] != a[j - 1] and (a.count(a[j]) == 2) and (j >= 1):
			print('YES')
			break
		elif a[j] != a[j + 1] and a.count(a[j]) == 2 and (j == 0):
			print('YES')
			break
		else:
			m += 1
			continue
	if m == n - 1:
		print('NO')


T = int(input())
for test in range(T):
	n = int(input())
	lst = list(map(int, input().split()))
	flag = False
	for i in range(len(lst) - 2):
		for j in range(i + 2, len(lst)):
			if lst[i] == lst[j]:
				flag = True
				break
	if flag == True:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	cnt = [[] for i in range(n + 2)]
	for i in range(n):
		cnt[arr[i]].append(i)
	for el in cnt:
		if len(el) > 2:
			print('YES')
			break
		if len(el) < 2:
			continue
		if abs(el[1] - el[0]) != 1:
			print('YES')
			break
	else:
		print('NO')


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	c = 0
	k = 0
	for i in range(n):
		if a[i] in a[i + 2:]:
			c = 1
	if c == 1:
		print('YES')
	else:
		print('NO')
	t -= 1


import sys
input = sys.stdin.buffer.readline

def getCnts(j, l, r):
	if l > r:
		return 0
	if l == 0:
		return prefCnt[r][j]
	else:
		return prefCnt[r][j] - prefCnt[l - 1][j]
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	MAXSIZE = max(a)
	prefCnt = [[0 for _a in range(MAXSIZE + 1)] for _b in range(n)]
	posJ = [[-1] for _a in range(MAXSIZE + 1)]
	for j in range(1, MAXSIZE + 1):
		cnts = 0
		for i in range(n):
			if a[i] == j:
				cnts += 1
				posJ[j].append(i)
			prefCnt[i][j] = cnts
	ans = 0
	for l in range(-1, n):
		if l == -1:
			leftCnts = 0
			r = n
		else:
			j = a[l]
			leftCnts = getCnts(j, 0, l)
			rPosJIdx = len(posJ[j]) - leftCnts
			if rPosJIdx < 1:
				continue
			r = posJ[j][rPosJIdx]
			if r <= l:
				continue
		for inner in range(1, MAXSIZE + 1):
			innerLen = getCnts(inner, l + 1, r - 1)
			ans = max(ans, leftCnts * 2 + innerLen)
	print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	st = []
	for i in range(1, 27):
		b = [0] * n
		b[0] = 0 if a[0] != i else 1
		for j in range(1, n):
			b[j] = b[j - 1] + 1 if a[j] == i else b[j - 1]
		st += [b]
	ans = 0
	for i in range(26):
		ans = max(ans, st[i][n - 1])
	for i in range(1, n - 1):
		for j in range(i, n - 1):
			mx = 0
			mx2 = 0
			for k in range(26):
				(a, b, c) = (st[k][i - 1], st[k][j] - st[k][i - 1], st[k][n - 1] - st[k][j])
				mx = max(mx, min(a, c))
				mx2 = max(mx2, b)
			ans = max(ans, mx2 + 2 * mx)
	print(ans)


T = int(input())
for _ in range(T):
	n = int(input())
	li = input().split()
	di = {}
	di[int(li[0])] = 1
	flag = 0
	for i in range(1, n):
		if int(li[i]) not in di:
			di[int(li[i])] = 1
		elif int(li[i]) == int(li[i - 1]):
			di[int(li[i])] += 1
		else:
			flag = 1
			break
	flag1 = 0
	for i in di:
		if di[i] > 2:
			flag1 = 1
			break
	if flag == 1 or flag1 == 1:
		print('yes')
	else:
		print('no')


qq = int(input())
for _ in range(qq):
	n = int(input())
	dat = list(map(int, input().split()))
	f = False
	d = dict()
	for i in range(n):
		if dat[i] in d:
			if i - d[dat[i]] != 1:
				f = True
		else:
			d[dat[i]] = i
	print('YES' if f else 'NO')


for t in range(int(input())):
	(n, s) = (input(), set())
	a = map(int, input().split())
	(x, y) = (next(a), None)
	for num in a:
		if y != None:
			s.add(y)
		if num in s:
			print('YES')
			break
		y = x
		x = num
	else:
		print('NO')


n = int(input())
for i in range(0, n):
	o = int(input())
	G = 0
	p = input().rstrip().split(' ')
	for j in range(0, len(p)):
		A = int(p[j])
		B = p[j + 2:]
		if str(A) in B:
			G = 1
			print('YES')
			break
	if G == 0:
		print('NO')


import io
import os
from collections import Counter, defaultdict, deque

def solve(N, A):
	cumCounts = [[0 for i in range(26)]]
	for x in A:
		temp = list(cumCounts[-1])
		temp[x] += 1
		cumCounts.append(temp)

	def rangeSum(i, j):
		return [y - x for (x, y) in zip(cumCounts[i], cumCounts[j])]
	best = 0
	for i in range(N):
		for j in range(i + 1, N + 1):
			left = rangeSum(0, i)
			mid = rangeSum(i, j)
			right = rangeSum(j, N)
			lenB = max(mid)
			for (c1, c2) in zip(left, right):
				lenA = min(c1, c2)
				best = max(best, 2 * lenA + lenB)
	return best
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
T = int(input())
for t in range(T):
	(N,) = [int(x) for x in input().split()]
	A = [int(x) - 1 for x in input().split()]
	ans = solve(N, A)
	print(ans)


from collections import defaultdict
for T in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	d = defaultdict(list)
	for i in range(n):
		d[arr[i]].append(i)
	res = 'NO'
	for j in d:
		if d[j][-1] - d[j][0] > 1:
			res = 'YES'
			break
	print(res)


from sys import stdin
T = int(stdin.readline().strip())
for casos in range(T):
	n = int(stdin.readline().strip())
	s = list(map(int, stdin.readline().strip().split()))
	arr = [[] for i in range(n + 2)]
	for i in range(n):
		arr[s[i]].append(i)
	flag = False
	for i in range(n + 2):
		if len(arr[i]) > 0 and arr[i][-1] - arr[i][0] >= 2:
			flag = True
			break
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
for r in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	mat = []
	for i in range(n):
		temp = []
		for j in range(n):
			temp.append(0)
		mat.append(temp)
	for i in range(n):
		mat[i][i] = 1
	for cl in range(2, n + 1):
		for i in range(n - cl + 1):
			j = i + cl - 1
			if l[i] == l[j] and cl == 2:
				mat[i][j] = 2
			elif l[i] == l[j]:
				mat[i][j] = mat[i + 1][j - 1] + 2
			else:
				mat[i][j] = max(mat[i][j - 1], mat[i + 1][j])
	if mat[0][n - 1] >= 3:
		print('YES')
	else:
		print('NO')


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	arr = tuple(map(int, input().split()))
	c = Counter(arr)
	mx = max(arr) + 1
	cnt = [0] * mx
	cnt_id = [[0] * (n + 1) for i in range(mx)]
	for i in range(n):
		cnt[arr[i]] += 1
		cnt_id[arr[i]][cnt[arr[i]]] = i
	loc = []
	for i in range(1, max(cnt) // 2 + 1):
		t = (0, 0)
		for a in range(1, mx):
			if cnt_id[a][cnt[a] - i + 1] > cnt_id[a][i]:
				t = (cnt_id[a][i], cnt_id[a][cnt[a] - i + 1])
				loc.append((t, i))
	loc.sort(key=lambda x: x[0][1])
	loc.sort(key=lambda x: x[0][0])
	start = 0
	end = -1
	cnt = [0] * mx
	ans = 1
	for (st, t) in loc:
		(i, j) = st
		if i != j:
			while start <= i:
				cnt[arr[start]] -= 1
				start += 1
			if end < j:
				end += 1
				while end < j:
					cnt[arr[end]] += 1
					end += 1
				end -= 1
			else:
				while end >= j:
					cnt[arr[end]] -= 1
					end -= 1
			ans = max(ans, 2 * t + max(cnt))
	print(ans)


from bisect import bisect_left, bisect_right
from typing import List
import collections
import bisect
import itertools
import functools
from fractions import gcd
import heapq
from math import ceil, sqrt, floor
import sys
sys.setrecursionlimit(50000)
import collections
from collections import defaultdict, deque
import collections
T = int(input())
for _ in range(T):
	n = int(input())
	A = list(map(int, input().split()))
	d = {}
	result = 'NO'
	for (i, v) in enumerate(A):
		if v not in d:
			d[v] = i
		elif d[v] + 2 <= i:
			result = 'YES'
			break
	print(result)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def pr_list(a):
	print(*a, sep=' ')

def main():
	tests = inp()
	for test in range(tests):
		n = inp()
		arr = inlt()
		hashm = {}
		flag = 0
		for i in range(len(arr)):
			if arr[i] in hashm and abs(i - hashm[arr[i]]) > 1:
				flag = 1
				break
			else:
				hashm[arr[i]] = i
		if flag == 1:
			print('Yes')
		else:
			hashm = {}
			for i in arr:
				if i in hashm and hashm[i] >= 2:
					flag = 1
					break
				elif i in hashm:
					hashm[i] = hashm[i] + 1
				else:
					hashm[i] = 1
			if flag == 1:
				print('Yes')
			else:
				print('No')
	return 0
main()


from sys import stdin
import collections

def main():
	readline = stdin.readline
	t = int(readline())
	res = []
	for i in range(t):
		n = int(readline())
		a = list(map(int, readline().split()))
		d = dict()
		flag = False
		for j in range(n):
			if str(a[j]) not in d:
				d[str(a[j])] = 1
			else:
				d[str(a[j])] += 1
				if d[str(a[j])] == 3:
					flag = True
					break
		if flag:
			res.append('YES')
		else:
			j = 0
			while j <= n - 1:
				if d[str(a[j])] == 1:
					j += 1
				else:
					if a[j + 1] != a[j]:
						res.append('YES')
						break
					j += 2
			else:
				res.append('NO')
	for i in range(t):
		print(res[i])
main()


def solve():
	n = int(input())
	arr = list(map(int, input().split()))
	d = {}
	for (k, v) in enumerate(arr):
		if v not in d:
			d[v] = k
		elif k - d[v] > 1:
			return 'YES'
	return 'NO'
t = int(input())
for _ in range(t):
	print(solve())


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = [0 for i in range(n + 1)]
	ans = False
	for i in range(2, n):
		l[a[i - 2]] = 1
		if l[a[i]] == 1:
			ans = True
			break
	if ans:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = dict()
	for i in range(n):
		if a[i] in c:
			c[a[i]].append(i)
		else:
			c[a[i]] = [i]
	flag = 0
	for i in c:
		if len(c[i]) >= 3:
			flag = 1
			break
		elif len(c[i]) == 2:
			if abs(c[i][0] - c[i][1]) >= 2:
				flag = 1
				break
	if flag == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split(' ')))
	cnt = -1
	k = []
	freq = []
	for i in range(n + 1):
		freq.append(0)
	for i in arr:
		freq[i] += 1
	for i in range(len(arr)):
		if arr[i] in k:
			if arr[i] != arr[i - 1]:
				cnt = 1
				break
		else:
			k.append(arr[i])
		if freq[arr[i]] > 2:
			cnt = 1
			break
	if cnt == 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = [[0 for i in range(n)]]
	for i in range(1, 27):
		k = []
		if a[0] == i:
			k.append(1)
			count = 1
		else:
			k.append(0)
			count = 0
		for j in range(1, n):
			if a[j] == i:
				count += 1
			k.append(count)
		c.append(k[:])
	maxx = 0
	for i in range(1, 27):
		maxxi = 0
		for j in range(1, 27):
			count = c[j][n - 1]
			if count == 0 or c[i][n - 1] == 0:
				continue
			else:
				for pi in range(0, n):
					if a[pi] == i:
						break
				for pj in range(n - 1, -1, -1):
					if a[pj] == i:
						break
				nu = 2
				while pj - pi > 1:
					count = max(count, nu + c[j][pj - 1] - c[j][pi])
					for o in range(pi + 1, pj):
						if a[o] == i:
							break
					pi = o
					for o in range(pj - 1, pi, -1):
						if a[o] == i:
							break
					pj = o
					nu += 2
			maxxi = max(maxxi, count)
		maxx = max(maxx, maxxi)
	print(maxx)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	s = set()
	f = 0
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = [i]
		else:
			d[a[i]].append(i)
		if len(d[a[i]]) >= 2:
			s.add(a[i])
	for c in s:
		if d[c][-1] - d[c][0] > 1:
			f = 1
			break
	if f:
		print('YES')
	else:
		print('NO')


from collections import defaultdict

def solve():
	_ = input()
	dic = defaultdict(list)
	for (idx, e) in enumerate(map(int, input().split())):
		dic[e].append(idx)
	for key in dic:
		l = len(dic[key])
		if l > 2:
			return 'YES'
		elif l == 2:
			if abs(dic[key][0] - dic[key][1]) > 1:
				return 'YES'
	return 'NO'
for _ in range(int(input())):
	print(solve())


a = int(input())
for x in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	k = [0] * b
	d = list(set(c))
	for y in d:
		k[y - 1] += c.count(y)
	if max(k) == 2:
		if c[0] == c[len(c) - 1]:
			print('YES')
		else:
			t = 0
			for y in range(len(k)):
				if k[y] == 2 and y == 0:
					if c[c.index(1)] == c[c.index(1) + 1]:
						continue
					else:
						t += 1
				elif k[y] == 2:
					if c[c.index(y + 1)] == c[c.index(y + 1) + 1] or c[c.index(y + 1)] == c[c.index(y + 1) - 1]:
						continue
					else:
						t += 1
			if t > 0:
				print('YES')
			else:
				print('NO')
	elif max(k) >= 3:
		print('YES')
	else:
		print('NO')


from bisect import bisect_left
for x in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if a[i] in d:
			d[a[i]].append(i)
		else:
			d[a[i]] = [i]
	ma = 0
	for i in d.keys():
		for j in d.keys():
			if i != j:
				for k in range(1, len(d[i]) // 2 + 1):
					ma = max(ma, 2 * k - bisect_left(d[j], d[i][k - 1]) + bisect_left(d[j], d[i][-k]))
	for i in d.keys():
		ma = max(ma, len(d[i]))
	print(ma)
a = []
for i in range(10000):
	a.append(i)


def fonk(x):
	return x[0]
n = int(input())
for k in range(n):
	l = int(input())
	A = [[int(i), j] for (i, j) in zip(input().split(), range(l))]
	A.sort(key=fonk)
	durum = False
	for i in range(len(A) - 1):
		if A[i][0] == A[i + 1][0]:
			if abs(A[i][1] - A[i + 1][1]) != 1:
				durum = True
				break
			elif i + 2 < len(A):
				if A[i][0] == A[i + 2][0]:
					durum = True
					break
	if durum:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	lens = int(input())
	arrs = [int(x) for x in input().split()]
	sarr = arrs[::-1]
	ans = 'NO'
	for i in arrs:
		if arrs.count(i) > 1 and lens - sarr.index(i) - arrs.index(i) > 2:
			ans = 'YES'
			break
	print(ans)


import sys
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
ni = lambda : int(ns())
nin = lambda y: [ni() for _ in range(y)]
na = lambda : list(map(int, stdin.readline().split()))
nan = lambda y: [na() for _ in range(y)]
nf = lambda : float(ns())
nfn = lambda y: [nf() for _ in range(y)]
nfa = lambda : list(map(float, stdin.readline().split()))
nfan = lambda y: [nfa() for _ in range(y)]
ns = lambda : stdin.readline().rstrip()
nsn = lambda y: [ns() for _ in range(y)]
ncl = lambda y: [list(ns()) for _ in range(y)]
nas = lambda : stdin.readline().split()

def sol():
	n = ni()
	a = na()
	t = n
	cnt = 0
	flag = False
	for i in range(n):
		for j in range(i + 1, n):
			if abs(i - j) >= 2 and a[i] == a[j]:
				flag = True
	print('YES' if flag else 'NO')
for i in range(ni()):
	sol()


t = int(input())
while t:
	asda = 1
	garbage = int(input())
	z = list(map(int, input().rstrip().split(' ')))
	l = set(z)
	mil = min(l)
	mal = max(l)
	if mil < 1 or mal > garbage:
		print('NO')
	elif garbage < 4:
		if z[0] == z[2]:
			print('YES')
		else:
			print('NO')
	else:
		r = 'NO'
		i = 0
		while i < len(z) - 2:
			for f in range(i + 2, len(z)):
				if z[i] == z[f]:
					r = 'YES'
					break
			i = i + 1
		print(r)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(d, f) = (dict(), False)
	for (i, e) in enumerate(a):
		if e in d:
			if i - d[e] > 1:
				f = True
				break
		else:
			d[e] = i
	print('YES' if f else 'NO')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = input().split()
	arr = [int(x) for x in arr]
	dp = [0] * (max(arr) + 1)
	flag = False
	prev = -1
	for c in arr:
		dp[c] += 1
		if dp[c] >= 2:
			if prev != c:
				flag = True
				break
		if dp[c] == 3:
			flag = True
			break
		prev = c
	if flag:
		print('YES')
	else:
		print('NO')


from collections import defaultdict
t = int(input())
for j in range(t):
	matriz = defaultdict(list)
	n = int(input())
	lista = list(map(int, input().split()))
	cont = 0
	for item in lista:
		matriz[item].append(cont)
		cont += 1
	ans = False
	for i in range(1, n + 1):
		if len(matriz[i]) > 2:
			ans = True
		elif len(matriz[i]) == 2:
			if abs(matriz[i][1] - matriz[i][0]) > 1:
				ans = True
	if ans:
		print('YES')
	else:
		print('NO')


from sys import stdin
n = int(stdin.readline())
for i in range(n):
	k = int(stdin.readline())
	L = [[-1] for i in range(5001)]
	I = list(map(int, stdin.readline().split()))
	for (i, j) in enumerate(I):
		L[j].append(i)
	check = False
	for i in range(1, 5001):
		if len(L[i]) > 2:
			if L[i][-1] - L[i][1] >= 2:
				check = True
	if check:
		print('YES')
	else:
		print('NO')


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	d = defaultdict()
	for i in range(n):
		d[lis[i]] = i
	for i in range(n):
		if i < d[lis[i]] - 1:
			print('YES')
			break
	else:
		print('NO')
		continue


import sys
sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]

def main():

	def ok(aa):
		pos = [-1] * 5005
		for (i, a) in enumerate(aa):
			if pos[a] == -1:
				pos[a] = i
			elif i > pos[a] + 1:
				return True
		return False
	t = II()
	for _ in range(t):
		II()
		if ok(LI()):
			print('YES')
		else:
			print('NO')
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n - 1):
		if a.count(a[i]) > 2:
			print('YES')
			break
		if a.count(a[i]) == 2:
			if a[i] in a[i + 1:] and a.index(a[i], i + 1) - i > 1:
				print('YES')
				break
	else:
		print('NO')


from collections import defaultdict
from sys import stdin
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, stdin.readline().split()))
	c = defaultdict(lambda : -1)
	zz = False
	for i in range(n):
		if c[a[i]] == -1:
			c[a[i]] = i
		elif i - c[a[i]] > 1:
			print('YES')
			zz = True
			break
	if not zz:
		print('NO')


n = int(input())
for i in range(n):
	l = int(input())
	a = list(map(int, input().split()))
	k = [[] for _ in range(5100)]
	for j in range(l):
		k[a[j]].append(j)
	ok = 0
	for x in range(5100):
		if len(k[x]) == 0:
			continue
		h = k[x][0]
		for y in range(len(k[x])):
			if abs(h - k[x][y]) > 1:
				ok = 1
	print('YES' if ok else 'NO')


from bisect import bisect_left as lower_bound, bisect_right as upper_bound
from sys import stdin, stdout

def main():
	t = int(stdin.readline())
	for _ in range(t):
		n = int(stdin.readline())
		a = list(map(int, stdin.readline().strip().split()))
		(pre, su) = ({i: [] for i in range(27)}, {i: [] for i in range(27)})
		for (i, num) in enumerate(a):
			su[num].append(i)
		ret = 1
		for (i, num) in enumerate(a):
			pre[num].append(i)
			su[num].pop(0)
			x = min(len(pre[num]), len(su[num]))
			if x == 0:
				continue
			(lo, hi) = (pre[num][x - 1], su[num][len(su[num]) - x])
			y = 0
			for j in range(27):
				p = lower_bound(su[j], hi)
				q = upper_bound(su[j], lo)
				y = max(y, p - q)
			ret = max(ret, 2 * x + y)
		print(ret)
main()


from collections import defaultdict as dd
from bisect import bisect_left as bl
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = dd(list)
	for i in range(n):
		c[a[i]].append(i)
	ans = 0
	for i in c.keys():
		for j in c.keys():
			if i != j:
				for k in range(1, len(c[i]) // 2 + 1):
					l = c[i][k - 1]
					r = c[i][-k]
					m = bl(c[j], r) - bl(c[j], l)
					ans = max(ans, 2 * k + m)
	for i in c.keys():
		ans = max(ans, len(c[i]))
	print(ans)


def solve():
	n = int(input())
	array = list(map(int, input().split()))
	for i in range(n):
		for j in range(n - 1, i + 1, -1):
			if array[i] == array[j]:
				print('YES')
				return
	print('NO')
for _ in range(int(input())):
	solve()


from collections import defaultdict
t = int(input())
for _ in range(t):
	d = {}
	d = defaultdict(lambda : 0, d)
	n = int(input())
	a = list(map(int, input().split()))
	flag = 0
	k = 1
	for i in a:
		if d[i] == 0:
			d[i] = k
		elif k - d[i] > 1:
			flag = 1
			break
		k += 1
	if flag == 1:
		print('YES')
	else:
		print('NO')


def f(i, j, n, ar):
	for k in range(j, n):
		if ar[k] == ar[i]:
			return True
	return False
t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	a = input()
	A = list(map(int, list(a.split())))
	flag = 0
	for i in range(n - 2):
		if f(i, i + 2, n, A) == True:
			print('YES')
			flag = 1
			break
	if flag == 0:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	data = tuple(map(int, input().split()))
	met = []
	for i in data:
		if i in met[:-1]:
			print('YES')
			break
		met.append(i)
	else:
		print('NO')


def solve(n, s):
	if n <= 2:
		return False
	s = str(s)
	a = list(map(int, s.split()))
	for i in range(n - 2):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				return True
	return False
n = int(input())
for i in range(n):
	le = int(input())
	str1 = input()
	if solve(le, str1):
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
from math import *
input = stdin.readline
for i in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	d1 = []
	d2 = []
	for i in range(n):
		d1.append([0] * 27)
		d2.append([0] * 27)
	d1[0][ar[0]] += 1
	for i in range(1, n):
		for j in range(27):
			d1[i][j] = d1[i - 1][j]
		d1[i][ar[i]] += 1
	d2[-1][ar[-1]] += 1
	for i in range(n - 2, -1, -1):
		for j in range(27):
			d2[i][j] = d2[i + 1][j]
		d2[i][ar[i]] += 1
	ans = 1
	for i in range(n):
		for j in range(i + 1, n):
			a = [0] * 27
			b = [0] * 27
			for k in range(27):
				x = d1[i][k]
				y = d2[j][k]
				a[k] = min(x, y) * 2
				b[k] = d1[j - 1][k] - d1[i][k]
			if ans < max(a) + max(b):
				ans = max(a) + max(b)
	print(ans)


def main():
	for i in range(int(input())):
		n = input()
		a = [int(x) for x in input().split()]
		(flag, pos) = (False, {})
		for j in range(len(a)):
			if a[j] not in pos:
				pos[a[j]] = j
			elif j - pos[a[j]] > 1:
				print('YES')
				flag = True
				break
			elif j - pos[a[j]] == 1:
				if j != len(a) - 1 and j > 1:
					if a[j + 1] == a[j - 2]:
						print('YES')
						flag = True
						break
			else:
				pos[a[j]] = j
		if flag == False:
			print('NO')
main()


for _ in range(int(input())):
	count = int(input())
	arr = list(map(int, input().split()))
	if len(arr) < 3:
		print('NO')
		continue
	res = 'NO'
	for i in range(len(arr)):
		for j in range(i, len(arr)):
			if i < j:
				if arr[i] == arr[j] and i + 1 != j:
					res = 'YES'
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if a == a[::-1]:
		print('YES')
	else:
		ab = list(set(a))
		flag = 0
		for i in ab:
			c = a.count(i)
			j = a.index(i)
			if c == 2 and a[j] != a[j + 1]:
				flag = 1
				break
			elif c > 2:
				flag = 1
				break
		if flag == 1:
			print('YES')
		else:
			print('NO')


import sys
input = sys.stdin.buffer.readline

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		A = list(map(int, input().split()))
		C = [[0] * (n + 1) for _ in range(26)]
		for i in range(n):
			C[A[i] - 1][i + 1] = 1
		for i in range(26):
			for j in range(1, n + 1):
				C[i][j] += C[i][j - 1]
		M = [[0] * (n + 1) for _ in range(n + 1)]
		for l in range(26):
			for i in range(n + 1):
				for j in range(i, n + 1):
					if M[i][j] <= C[l][j] - C[l][i]:
						M[i][j] = C[l][j] - C[l][i]
		ans = 0
		for k in range(26):
			for i in range(n + 1):
				for j in range(i, n + 1):
					v = min(C[k][i] - C[k][0], C[k][n] - C[k][j]) * 2
					v += M[i][j]
					if v >= ans:
						ans = v
		print(ans)
main()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	p = []
	found = False
	for i in range(0, len(l)):
		if not found and l.count(l[i]) > 1:
			for j in range(i + 1, n):
				if l[j] == l[i] and j - i > 1:
					found = True
					break
	if not found:
		print('NO')
	else:
		print('YES')


t = int(input())
for im in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	flag = 0
	d = {}
	for i in range(n):
		if str(a[i]) not in d:
			d[str(a[i])] = i
		elif i >= d[str(a[i])] + 2:
			print('YES')
			flag = 1
			break
	if not flag:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	fg = 0
	x = list(map(int, input().split()))
	for i in range(n):
		for j in range(i + 2, n):
			if x[i] == x[j]:
				fg = 1
	print('YES' if fg else 'NO')


t = int(input())
for _ in range(t):
	n = map(int, input().split())
	arr = list(map(int, input().split()))
	found = False
	s = set(arr)
	for i in s:
		x = arr.index(i)
		if i in arr[x + 2:]:
			found = True
			print('YES')
			break
	if not found:
		print('NO')


def main():
	import sys
	input = sys.stdin.readline
	for _ in range(int(input())):
		N = int(input())
		A = list(map(int, input().split()))
		dic = {}
		ok = 0
		for (i, a) in enumerate(A):
			if a not in dic:
				dic[a] = i
			elif i - dic[a] > 1:
				ok = 1
				break
		if ok:
			print('YES')
		else:
			print('NO')
main()


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	if n < 3:
		print('NO')
	else:
		s = set(arr)
		ls = []
		flag = 1
		for i in s:
			if arr.count(i) == 2:
				ls.append(i)
			if arr.count(i) >= 3:
				flag = 0
				break
		if flag == 0:
			print('YES')
		elif len(ls) > 1:
			flag = 1
			for i in ls:
				if arr[arr.index(i) + 1] != i:
					flag = 0
					break
			if flag == 0:
				print('YES')
			else:
				print('NO')
		elif len(ls) == 1:
			ind = arr.index(ls[0])
			if arr[ind + 1] != ls[0]:
				print('YES')
			else:
				print('NO')
		else:
			print('NO')


for _ in range(int(input())):
	n = int(input())
	list1 = list(map(int, input().split()))
	c = 0
	for i in range(n):
		for j in range(i + 2, n):
			if list1[i] == list1[j]:
				c = 1
	if c == 1:
		print('YES')
	else:
		print('NO')


import bisect, collections

def solution(T):
	n = int(input().strip())
	a = [int(x) for x in input().strip().split(' ')]
	d = {}
	for i in range(len(a)):
		d[a[i]] = i
	for i in range(len(a)):
		if d[a[i]] > i + 1:
			print('YES')
			return
	print('NO')

def main():
	T = int(input().strip())
	for _ in range(T):
		solution(T)
main()


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	count = 0
	val = 0
	for i in range(n - 2):
		val = ar[i]
		for j in range(i + 2, n):
			if val == ar[j]:
				print('YES')
				count = 1
				break
		if count == 1:
			break
	if count == 0:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	visited = [False] * 5001
	count = [0] * 5001
	for i in range(n):
		if not visited[a[i]]:
			visited[a[i]] = True
			if i - 1 >= 0:
				count[a[i - 1]] += 1
		else:
			if count[a[i]] != 0:
				print('YES')
				break
			count[a[i]] += 1
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split(' ')))
	mp = dict()
	flag = False
	for j in range(n):
		try:
			if j - mp[a[j]] > 1:
				flag = True
				break
		except:
			mp[a[j]] = j
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	le = 0
	re = n - 1
	l1 = []
	a = False
	for i in range(n):
		if l[i] in l1 and len(l) > 0:
			if l[i] != l1[-1]:
				a = True
			else:
				l1.append(-2)
		else:
			l1.append(l[i])
	if a:
		print('YES')
	else:
		print('NO')


from sys import stdin
from collections import Counter
input = stdin.buffer.readline
T = int(input())
for _ in range(T):
	n = int(input())
	ls = list(map(int, input().split()))
	p = [[] for _ in range(201)]
	sm = [[0] * n for _ in range(201)]
	for (i, u) in enumerate(ls):
		p[u].append(i)
	for i in range(1, 201):
		for (j, u) in enumerate(ls):
			if j > 0:
				sm[i][j] += sm[i][j - 1]
			sm[i][j] += i == u

	def max_al(fm, to):
		mx = 0
		for al in range(1, 201):
			mx = max(mx, sm[al][to] - sm[al][fm - 1])
		return mx
	mx = max([len(p[i]) for i in range(1, 201)])
	for al in range(1, 201):
		for i in range(len(p[al]) // 2):
			(fm, to) = (p[al][i] + 1, p[al][-i - 1] - 1)
			almx = max_al(fm, to)
			mx = max(mx, almx + (i + 1) * 2)
	print(mx)


import sys, math
sys.setrecursionlimit(10 ** 5 + 10)

def input():
	return sys.stdin.readline()[:-1]

def main():
	q = int(input())
	for _ in range(q):
		n = int(input())
		a = list(map(int, input().split()))
		f = 0
		for k in range(n - 2):
			if f == 1:
				break
			for l in range(k + 2, n):
				if a[k] == a[l]:
					f = 1
					break
		if f == 1:
			print('YES')
		else:
			print('NO')
main()


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(item) for item in input().split()]
	ok = False
	for i in range(n - 2):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				ok = True
	if ok:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	flag = 0
	for i in range(n - 1):
		if a[i] in a[i + 2:]:
			flag += 1
	if flag >= 1:
		print('Yes')
	else:
		print('No')


testcase = int(input())
while testcase > 0:
	n = int(input())
	x = input()
	b2 = list(map(int, x.split(' ')))
	b1 = list(b2)
	b2.reverse()
	flag = 0
	for i in range(1, n + 1):
		if b1.count(i) != 0:
			indmin = b1.index(i)
			indmax = n - b2.index(i) - 1
			if indmax - indmin > 1:
				flag = 1
				break
	if flag == 1:
		print('YES')
	else:
		print('NO')
	testcase -= 1


import sys
input = sys.stdin.readline
from math import *
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = [int(x) for x in input().split()]
	d = {}
	flag = 0
	for i in range(n):
		if a[i] in d and i - d[a[i]] > 1:
			flag = 1
			break
		elif a[i] not in d:
			d[a[i]] = i
	print('YES' if flag else 'NO')


t = int(input())
for i in range(t):
	n = int(input())
	ch = input()
	L = [int(j) for j in ch.split()]
	test = False
	s = set()
	for i in range(n - 1):
		for j in range(i + 1, n):
			if L[i] == L[j] and j > i + 1:
				test = True
				break
	if test:
		print('YES')
	else:
		print('NO')


x = int(input())
for _ in range(x):
	y = int(input())
	xs = list(map(int, input().split()))
	res = False
	for i in range(len(xs)):
		d = xs[i]
		if d in xs[i + 2:]:
			res = True
			break
	if res:
		print('YES')
	else:
		print('NO')


T = int(input())
n = [0] * T
k = [0] * T
ns = [0] * T
for t in range(T):
	n[t] = int(input())
	ns[t] = [int(i) for i in input().split(' ')]
for t in range(T):
	seen = []
	d = 0
	m = 0
	for i in ns[t]:
		if i in seen and (seen[-1] != i or (seen[-1] == i and i in seen[:len(seen) - 1])):
			print('YES')
			d = True
			break
		seen.append(i)
	if not d:
		print('NO')


def match(a, x, s, e):
	matches = []
	while s <= e:
		while s <= e and a[s] != x:
			s += 1
		while s <= e and a[e] != x:
			e -= 1
		if s <= e:
			matches.append((s, e))
			s += 1
			e -= 1
	return matches

def solve(a):
	ps = [[0] * 27]
	for x in a:
		p = ps[-1][:]
		p[x] += 1
		ps.append(p)
	sol = 1
	for first in range(1, 27):
		matches = match(a, first, 0, len(a) - 1)
		for (i, (s, e)) in enumerate(matches):
			if s != e:
				sol = max(sol, 2)
			for second in range(1, 27):
				total2 = ps[e][second] - ps[s + 1][second]
				if total2 > 0:
					sol = max(sol, (i + 1) * 2 + total2)
	return sol
t = int(input())
for _ in range(t):
	input()
	a = list(map(int, input().split()))
	print(solve(a))


n = int(input())
for i in range(n):
	m = int(input())
	seq = list(map(int, input().strip().split()))
	flag = False
	dic = dict()
	for k in range(len(seq)):
		if seq[k] in dic.keys():
			if k - dic[seq[k]] >= 2:
				flag = True
		else:
			dic[seq[k]] = k
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	b = []
	xx = -1
	chk = 0
	for x in a:
		if a.count(x) >= 3:
			chk = 1
			continue
	for x in a:
		if x in b:
			if b[xx] != x:
				chk = 1
				break
			b.append(x)
			xx = xx + 1
		else:
			b.append(x)
			xx = xx + 1
	if chk == 0:
		print('NO')
	else:
		print('YES')


import math
import itertools
gh = lambda : map(int, input().split())
mod = 998244353
mod = int(mod)

def main():
	t = int(input())
	while t:
		t -= 1
		input()
		d = [-1] * 5005
		fl = 1
		a = list(gh())
		for i in range(len(a)):
			x = a[i]
			if d[x] == -1:
				d[x] = i
			elif i - d[x] > 1:
				fl = 0
				break
		print('YNEOS'[fl::2])
main()


import sys
import math
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	c = 0
	d = {}
	for i in range(n):
		if d.get(l[i], 0) == 0:
			d[l[i]] = []
		d[l[i]].append(i)
	for i in range(n):
		if d[l[i]][-1] - d[l[i]][0] >= 2:
			c = 1
			break
	if c == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	ind1 = 0
	ind2 = 0
	n = int(input())
	a = list(map(int, input().strip().split()))[:n]
	flag = 0
	for i in range(-1, -n, -1):
		co = a.count(a[i])
		if co >= 2:
			index1 = a.index(a[i])
			index2 = n + i
			if abs(index2 - index1) >= 2:
				print('YES')
				flag = 1
				break
	if flag == 0:
		print('NO')


import sys
input = sys.stdin.readline

def getInt():
	return int(input())

def getVars():
	return map(int, input().split())

def getList():
	return list(map(int, input().split()))

def getStr():
	return input().strip()
t = getInt()
for _ in range(t):
	n = getInt()
	a = getList()
	res = 'NO'
	d = {}
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = i
		elif d[a[i]] < i - 1:
			res = 'YES'
			break
	print(res)


import os
import sys
from io import BytesIO, IOBase

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()

def test():
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n):
		if a[i] in a[i + 2:]:
			return 'YES'
	return 'NO'
t = int(input())
for _ in range(t):
	print(test())


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = {}
	p = a[0]
	for v in a:
		if v not in c:
			c[v] = 1
		else:
			c[v] += 1
			if c[v] > 1 and v != p or c[v] == 3:
				print('YES')
				break
		p = v
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	g = {}
	f = 0
	cnt = 0
	for i in a:
		cnt = cnt + 1
		if i in d:
			d[i] += 1
			g[i].insert(1, cnt)
		else:
			d[i] = 1
			g[i] = [cnt]
		if d[i] >= 3:
			f = 1
			break
		if d[i] == 2:
			if g[i][1] - g[i][0] > 1:
				f = 1
				break
	if f == 1:
		print('YES')
	else:
		print('NO')


t = int(input())

def contain_palindrom(n, l=[]):
	if n < 3:
		return False
	for i in range(n - 2):
		if l[i] in l[i + 2:]:
			return True
	return False
for row in range(t):
	n = int(input())
	c = list(map(int, input().split()))
	if contain_palindrom(n, c):
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	isset = []
	deff = 0
	indexs = []
	for i in range(n):
		if a[i] in isset:
			if abs(indexs[isset.index(a[i])] - i) > 1:
				print('YES')
				deff = 1
				break
		else:
			isset.append(a[i])
			indexs.append(i)
	if deff == 0:
		print('NO')


import sys
T = int(input())
t = 0
while t < T:
	N = int(input())
	L = list(map(int, input().split()))
	P = []
	for n in range(N + 1):
		P.append(-1)
	out = False
	n = 0
	while n < N:
		a = L[n]
		if P[a] == -1:
			P[a] = n
		elif n - P[a] > 1:
			out = True
		n += 1
	if out:
		print('YES')
	else:
		print('NO')
	t += 1


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	flag = False
	s = set()
	for i in range(n - 2):
		if lst[i] not in s and lst[i] in lst[i + 2:]:
			flag = True
			break
		s.add(lst[i])
	if flag:
		print('YES')
	else:
		print('NO')


n = int(input())
out = ''

def subpalin(nums, k):
	i = True
	occur = {}
	for x in range(0, k):
		num = nums[x]
		try:
			occur[num] += 1
			if occur[num] > 2:
				i = False
				return 'YES'
			if occur[num] == 2 and nums[x - 1] != nums[x]:
				i = False
				return 'YES'
		except:
			occur[num] = 1
	if i:
		return 'NO'
for k in range(0, n):
	k = int(input())
	a = input().split()
	print(subpalin(a, k) + '\n')


import sys
input = sys.stdin.buffer.readline
t = int(input())
for j in range(t):
	n = int(input())
	data = [int(i) for i in input().split()]
	for i in range(n):
		found = False
		for k in range(i + 1, n):
			if data[k] == data[i] and k - i != 1:
				print('YES')
				found = True
				break
		if found:
			break
	else:
		print('NO')


from math import sqrt
from collections import defaultdict
t = int(input())
while t:
	t = t - 1
	n = int(input())
	a = list(map(int, input().split()))
	index = defaultdict(list)
	flag = False
	for (i, tmp) in enumerate(a):
		index[tmp].append(i)
	for (k, v) in index.items():
		if len(v) >= 2:
			v.sort()
			if v[-1] - v[0] >= 2:
				print('YES')
				flag = True
				break
	if not flag:
		print('NO')


t = int(input())
for i in range(t):
	MYLIST = []
	for j in range(5001):
		MYLIST.append(0)
	n = int(input())
	l = list(map(int, input().split()))
	var = 1
	MYLIST[l[0]] += 1
	for j in range(1, len(l)):
		if MYLIST[l[j]] == 0:
			MYLIST[l[j]] += 1
		elif MYLIST[l[j]] != 0 and l[j] == l[j - 1]:
			if j > 1 and l[j] == l[j - 2]:
				var = 0
				break
			else:
				var = 1
		elif MYLIST[l[j]] != 0 and l[j] != l[j - 1]:
			var = 0
			break
	if var == 0:
		print('YES')
	else:
		print('NO')


from collections import deque, defaultdict, Counter
from bisect import bisect_left, bisect
import math

def solve(inf):
	n = inf[0]
	a = list(map(int, input().split()))
	uni = list(set(a))
	if len(uni) == 1:
		return n
	ans = 0
	indxs = [[] for _ in range(201)]
	pre = [[0 for _ in range(201)] for j in range(n + 1)]
	for i in range(n):
		for j in range(201):
			pre[i][j] = pre[i - 1][j]
		pre[i][a[i]] += 1
		indxs[a[i]].append(i)
	for key in uni:
		l = 0
		r = len(indxs[key]) - 1
		ans = max(ans, r + 1)
		for i in range((r + 1) // 2):
			p = indxs[key][i]
			q = indxs[key][r - i]
			for newkey in uni:
				if newkey == key:
					continue
				ans = max(ans, pre[q - 1][newkey] - pre[p][newkey] + 2 * (i + 1))
	return ans
for _ in range(int(input())):
	inf = list(map(int, input().split()))
	print(solve(inf))


alphabet_len = 200
tc = int(input())
while tc > 0:
	n = int(input())
	a = list(map(int, input().split()))
	cnt = [[0] * alphabet_len]
	pos = [[] for _ in range(alphabet_len)]
	for i in range(1, n + 1):
		cnt.append(cnt[i - 1].copy())
		cnt[i][a[i - 1] - 1] += 1
		pos[a[i - 1] - 1].append(i - 1)
	chars = set(filter(lambda c: cnt[n][c] > 0, range(alphabet_len)))
	ans = 0
	for (i, v) in enumerate(pos):
		if not v:
			continue
		ans = max(ans, len(v))
		for p in range(len(v) // 2):
			l = v[p] + 1
			r = v[len(v) - p - 1] - 1
			for c in chars:
				summa = cnt[r + 1][c] - cnt[l][c]
				ans = max(ans, (p + 1) * 2 + summa)
	print(ans)
	tc -= 1


from sys import stdin
from itertools import groupby

def solve(arr):
	return any(map(lambda x: x > 1, map(lambda p: max(p) - min(p), map(lambda p: list(map(lambda q: q[0], p)), filter(lambda p: len(p) > 1, map(lambda ps: list(ps[1]), groupby(sorted(enumerate(arr), key=lambda p: p[1]), key=lambda p: p[1])))))))
input()
for (_, line) in zip(*[iter(stdin)] * 2):
	arr = line.split()
	print('YES' if solve(arr) else 'NO')


import sys
input = sys.stdin.readline
t = int(input())
from collections import defaultdict
from collections import Counter
import bisect
for _ in range(t):
	ans = 1
	n = int(input())
	a = input().rstrip().split()
	d = defaultdict(list)
	for i in range(n):
		d[a[i]].append(i)
	for j in d.keys():
		l = int(0)
		r = int(len(d[j]) - 1)
		for k in range(len(d[j]) // 2):
			left = d[j][l]
			right = d[j][r]
			tmp = a[left + 1:right]
			c = Counter(tmp)
			c_max = 0
			for m in c.keys():
				c_max = max(c_max, c[m])
			ans = max(ans, c_max + (k + 1) * 2)
			l += 1
			r -= 1
	print(ans)


from sys import stdin
from itertools import groupby
from collections import defaultdict

def solve(arr):
	D = defaultdict()
	for (idx, k) in enumerate(arr):
		if k not in D:
			D[k] = idx
		elif idx - D[k] > 1:
			return True
	return False
input()
for (_, line) in zip(*[iter(stdin)] * 2):
	arr = line.split()
	print('YES' if solve(arr) else 'NO')


l = ' abcdefghijklmnopqrstuvwxyz'
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = ''.join((l[i] for i in a))
	q = set(s)
	m = 0
	for i in q:
		for j in q:
			if i == j:
				m = max(m, s.count(i))
				continue
			t = ''.join((k for k in s if k == i or k == j))
			for k in range(t.count(i) // 2):
				t = t[t.index(i) + 1:t.rindex(i)]
				m = max(m, t.count(j) + 2 * (k + 1))
	print(m)


t = int(input())
while t != 0:
	c = int(input())
	a = list(map(int, input().split()))
	temp_a = a
	found = False
	while temp_a and (not found):
		e = temp_a[0]
		count = a.count(e) - 1
		temp_a.remove(e)
		if count > 0:
			if count == 1 and temp_a.index(e) != 0:
				found = True
			elif count > 1:
				found = True
		temp_a = [x for x in temp_a if x != e]
	if not found:
		print('NO')
	else:
		print('YES')
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	c = [-1 for _ in range(n)]
	p = False
	for i in range(n):
		if c[a[i] - 1] == -1:
			c[a[i] - 1] = i
		elif i - c[a[i] - 1] > 1:
			p = True
			break
	if p:
		print('YES')
	else:
		print('NO')


import sys
import math

def read_line():
	return sys.stdin.readline()[:-1]

def read_int():
	return int(sys.stdin.readline())

def read_int_line():
	return [int(v) for v in sys.stdin.readline().split()]

def bw(l, r, c, pref):
	return pref[c][r] - pref[c][l - 1]
t = read_int()
for i in range(t):
	n = read_int()
	a = read_int_line()
	pref = [[0 for i in range(n + 1)] for j in range(27)]
	d = {}
	for i in range(n):
		pref[a[i]][i] = 1
	for i in range(1, 27):
		for j in range(1, n):
			pref[i][j] += pref[i][j - 1]
	ans = -1
	for i in range(1, 27):
		ans = max(ans, pref[i][n - 1])
	for l in range(1, n):
		for r in range(l, n - 1):
			v = -1
			for i in range(1, 27):
				if bw(l, r, i, pref) >= v:
					v = bw(l, r, i, pref)
			y = v
			v = -1
			for i in range(1, 27):
				if min(bw(0, l - 1, i, pref), bw(r + 1, n - 1, i, pref)) >= v:
					v = min(bw(0, l - 1, i, pref), bw(r + 1, n - 1, i, pref))
			x = v
			ans = max(ans, 2 * x + y)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	flag = 0
	for i in range(n):
		if l.count(l[i]) >= 3:
			flag = 1
			break
		for j in range(i + 2, n):
			if l[i] == l[j]:
				flag = 1
				break
	if flag == 1:
		print('YES')
	else:
		print('NO')


a = int(input())
for _ in range(a):
	k = 0
	b = int(input())
	c = list(input().split())
	for i in range(b - 2):
		for j in range(i + 2, b):
			if c[i] == c[j]:
				k += 1
	if k >= 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = 0
	if len(a) == 3:
		b = [] + a
		a.reverse()
		if a == b:
			print('YES')
		else:
			print('NO')
	else:
		for i in range(len(a) - 2):
			b = a[i + 2:]
			if a[i] in b:
				d = 1
				break
		if d == 1:
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	a = [[] for i in range(5000 + 1)]
	for (i, j) in enumerate(arr):
		a[j].append(i)
	b = False
	for lis in a:
		if len(lis) and lis[-1] - lis[0] > 1:
			b = True
			break
	if b:
		print('YES')
	else:
		print('NO')


import math
t = int(input())
for y in range(t):
	n = int(input())
	num = list(map(int, input().split()))
	minp = {}
	maxp = {}
	for i in range(n):
		if num[i] not in minp:
			minp[num[i]] = i
		maxp[num[i]] = i
	f = 0
	for i in range(n):
		if maxp[num[i]] - minp[num[i]] >= 2:
			f = 1
	if f == 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	length = len(l)
	if length < 3:
		print('NO')
		continue
	else:
		f = False
		k = -1
		while f == False and k < length - 3:
			k += 1
			p = k + 2
			while l[k] != l[p] and p <= length - 2:
				p += 1
			if l[k] == l[p]:
				f = True
		if f == False:
			print('NO')
		else:
			print('YES')


import sys
input = lambda : sys.stdin.readline().rstrip()
from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	A = [int(i) for i in input().split()]
	D = defaultdict(int)
	for (i, a) in enumerate(A):
		if D[a] != 0 and i + 1 > D[a] + 1:
			print('YES')
			break
		elif D[a] == 0:
			D[a] = i + 1
	else:
		print('NO')


T = int(input())
for i in range(T):
	flag = 0
	n = int(input())
	L = list(map(int, input().split()))
	for t in range(n):
		if L.count(L[t]) >= 3:
			flag = 1
			break
		elif L.count(L[t]) == 2:
			if t == 0:
				if L[t] == L[t + 1]:
					continue
			if t == n - 1:
				if L[t] == L[t - 1]:
					continue
			elif L[t + 1] == L[t] or L[t] == L[t - 1]:
				continue
			flag = 1
			break
	if flag:
		print('YES')
	else:
		print('NO')


import sys
I = sys.stdin.readline
ans = ''
for _ in range(int(I())):
	n = int(I())
	arr = list(map(int, I().split()))
	flag = 1
	for i in range(n - 2):
		x = arr[i]
		if x in arr[i + 2:]:
			ans += 'YES\n'
			flag = 0
			break
	if flag:
		ans += 'NO\n'
print(ans)


import sys
input = sys.stdin.readline
read = lambda : map(int, input().split())
read_float = lambda : map(float, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()

def binary_search(low, high, w, h, n):
	while low < high:
		mid = low + (high - low) // 2
		if check(mid, w, h, n):
			low = mid + 1
		else:
			high = mid
	return low

def check(councils, a, k):
	summ = 0
	for x in a:
		summ += min(x, councils)
	return summ // councils >= k
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
from itertools import permutations
import math
import bisect as bis
import random
import sys

def solve():
	n = int(input())
	sequence = list(read())
	d = {}
	for x in sequence:
		a = d.get(x, 0)
		d[x] = a + 1
	val = list(d.values())
	m = max(val)
	if m >= 3:
		print('YES')
		return
	if m == 1:
		print('NO')
		return
	for i in range(1, n - 1):
		z = sequence[i]
		if d[z] == m and sequence[i - 1] != sequence[i] and (sequence[i + 1] != sequence[i]):
			print('YES')
			return
	if sequence[0] == sequence[-1]:
		print('YES')
		return
	print('NO')
for _ in range(int(input())):
	solve()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	flag = False
	d = {}
	for i in range(n):
		if d.get(a[i]) == None:
			d[a[i]] = [i]
		else:
			d[a[i]].append(i)
	for i in d:
		if len(d[i]) < 2:
			continue
		elif len(d[i]) == 2:
			if d[i][1] - d[i][0] > 1:
				flag = True
				break
		elif len(d[i]) > 2:
			flag = True
			break
	if flag:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	r = list(map(int, input().split()))
	d = {i: [] for i in range(1, 27)}
	for i in range(n):
		d[r[i]].append(i)
	ans = 1
	for end in range(1, 27):
		c = 1
		while c * 2 <= len(d[end]):
			(ll, rr) = (d[end][c - 1] + 1, d[end][-c] - 1)
			dd = [0] * 27
			for p in range(ll, rr + 1):
				dd[r[p]] += 1
			ans = max(ans, max(dd) + 2 * c)
			c += 1
	print(ans)


t = int(input())
for w in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	d = {}
	k = 1
	for i in range(n):
		if l[i] not in d:
			d[l[i]] = i
		elif i - d[l[i]] >= 2:
			print('YES')
			k = 0
			break
	if k == 1:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = [int(i) for i in input().split()]
	bool1 = True
	if n == 2:
		print('NO')
		continue
	for x in range(0, n - 2):
		if l[x] in l[x + 2:]:
			print('YES')
			bool1 = False
			break
	if bool1:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	al = list(map(int, input().split()))
	flag = False
	for l in range(n):
		if not flag:
			for r in range(l, n):
				if al[l] == al[r] and r - l > 1:
					print('YES')
					flag = True
					break
	if not flag:
		print('NO')


import sys
input = lambda : sys.stdin.readline().rstrip()
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	flag = 0
	d = {}
	flag = 0
	for i in range(len(a)):
		if a[i] in d:
			d[a[i]].append(i)
		else:
			d[a[i]] = [i]
	for i in d:
		if len(d[i]) > 1 and d[i][-1] - d[i][0] > 1:
			flag = 1
			break
	if flag == 1:
		print('YES')
	else:
		print('NO')


import math as m

def f1():
	Testcases = int(input())
	for i in range(0, Testcases):
		Columns = int(input())
		InAi = [int(j) for j in input().split(' ')]
		Oddf = False
		Evenf = False
		for k in InAi:
			if k % 2 == 0:
				Evenf = True
			else:
				Oddf = True
			if Oddf == True and Evenf == True:
				print('NO')
				break
		if Oddf == False or Evenf == False:
			print('YES')

def f2():
	Testcases = int(input())
	for i in range(0, Testcases):
		N = int(input())
		Array = [int(j) for j in input().split(' ')]
		dict1 = dict()
		flag = False
		for k in range(0, len(Array)):
			if Array[k] in dict1:
				if k - dict1[Array[k]][0] >= 2:
					flag = True
					print('YES')
					break
				dict1[Array[k]].append(k)
			else:
				dict1[Array[k]] = list()
				dict1[Array[k]].append(k)
		if flag == False:
			print('NO')
f2()


import sys
input = sys.stdin.readline

def fastio():
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
INF = 10 ** 20
MOD = 998244353
I = lambda : list(map(int, input().split()))
from math import gcd
from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br
(t,) = I()
while t:
	t -= 1
	(n,) = I()
	a = I()
	d = dd(list)
	ans = 'NO'
	for i in range(n):
		d[a[i]].append(i)
		if len(d[a[i]]) >= 2:
			if d[a[i]][-1] - d[a[i]][0] >= 2:
				ans = 'YES'
	print(ans)


import math

class Read:

	@staticmethod
	def string():
		return input()

	@staticmethod
	def int():
		return int(input())

	@staticmethod
	def list(sep=' '):
		return input().split(sep)

	@staticmethod
	def list_int(sep=' '):
		return list(map(int, input().split(sep)))
result = math.inf

def solve():
	n = Read.int()
	a = Read.list_int()
	for i in range(n):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				print('YES')
				return
	print('NO')
query_count = Read.int()
while query_count:
	query_count -= 1
	solve()


t = int(input())
for q in range(t):
	n = int(input())
	s = list(map(str, input().split()))
	flag = False
	i = 0
	if s[::-1] == s:
		print('YES')
		continue
	for x in range(n):
		for y in range(x + 2, n):
			if s[x] == s[y]:
				flag = True
				break
		if flag:
			print('YES')
			break
	if not flag:
		print('NO')


def solve(ar):
	d = {}
	p = None
	for i in ar:
		if i in d:
			d[i] += 1
			if d[i] > 2 or i != p:
				return 'YES'
		if i not in d:
			d[i] = 1
		p = i
	return 'NO'
T = int(input())
for t in range(T):
	n = int(input())
	ar = list(map(int, input().split()))
	print(solve(ar))


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	c = Counter(a)
	ans = ''
	for i in c:
		if c[i] >= 3:
			ans = 'YES'
	if not ans:
		b = [a[0]]
		for i in range(1, n):
			if a[i] != a[i - 1]:
				b.append(a[i])
		d = Counter(b)
		for i in d:
			if d[i] >= 2:
				ans = 'YES'
	print(ans if ans else 'NO')


import sys
input = sys.stdin.readline
MOD = 1000000007
MOD2 = 998244353
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
let = 'abcdefghijklmnopqrstuvwxyz'
for _ in range(ii()):
	n = ii()
	l = il()
	cntr = dict()
	mxcnt = 0
	dp = [[0 for i in range(201)] for j in range(n + 1)]
	for i in range(n - 1, -1, -1):
		for j in range(1, 201):
			dp[i][j] = dp[i + 1][j] + (l[i] == j)
		mxcnt = max(dp[i][l[i]], mxcnt)
		x = str(dp[i][l[i]]) + ',' + str(l[i])
		cntr[x] = i
	cntr2 = dict()
	for i in range(n):
		cntr2[l[i]] = cntr2.get(l[i], 0) + 1
		x = str(cntr2[l[i]]) + ',' + str(l[i])
		if x in cntr and cntr[x] > i:
			for j in range(1, 201):
				mxcnt = max(cntr2[l[i]] * 2 + (dp[i + 1][j] - dp[cntr[x]][j]), mxcnt)
	print(mxcnt)


t = int(input())
while t > 0:
	n = int(input())
	arr = list(map(int, input().split()))
	f = 0
	arr1 = []
	for i in range(n):
		arr1.append((arr[i], i))
	arr1 = sorted(arr1)
	for i in range(n - 1):
		if arr1[i][0] == arr1[i + 1][0] and arr1[i + 1][1] - arr1[i][1] > 1:
			f = 1
			break
		if i < n - 2:
			if arr1[i][0] == arr1[i + 1][0] and arr1[i + 1][0] == arr1[i + 2][0]:
				f = 1
				break
	if f > 0:
		print('YES')
	else:
		print('NO')
	t = t - 1


from collections import Counter
t = int(input())
for wer in range(t):
	n = int(input())
	f = 0
	li = list(map(int, input().split()))
	freq = Counter(li)
	for x in freq:
		if freq[x] >= 2:
			p1 = li.index(x)
			li1 = li.copy()
			li1 = li1[::-1]
			p2 = li1.index(x)
			p2 = n - 1 - p2
			if p2 - p1 >= 2:
				f = 1
				break
	if f == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
z = 0
while z < t:
	n = int(input())
	c = 0
	x = 1
	a = list(map(int, input().split()))
	if len(a) < 3:
		print('NO')
	else:
		x = 0
		c = 0
		while x < len(a):
			y = len(a) - 1
			while y > x:
				if a[x] == a[y] and y > x + 1:
					c += 1
					break
				y -= 1
			if c > 0:
				break
			x += 1
		if c > 0:
			print('YES')
		else:
			print('NO')
	z += 1


def ans(l):
	n = len(l)
	for i in range(n - 2):
		for j in range(i + 2, n):
			if l[i] == l[j]:
				return True
	return False
t = int(input())
while t:
	t = t - 1
	m = int(input())
	l = list(map(int, input().split()))
	if ans(l):
		print('YES')
	else:
		print('NO')


from collections import defaultdict
import bisect
from sys import stdin
input = stdin.readline

def check(i, j, d):
	maxi = 0
	for k in d:
		k = d[k]
		a = bisect.bisect_right(k, i)
		b = bisect.bisect_left(k, j)
		maxi = max(maxi, b - a)
	return maxi
t = int(input())
for _ in range(t):
	n = int(input())
	A = [int(j) for j in input().split()]
	maxi = 0
	d = defaultdict(list)
	for i in range(n):
		d[A[i]].append(i)
	for k in d:
		k = d[k]
		i = 0
		j = len(k) - 1
		travel = 0
		while i <= j:
			if i == j:
				travel += 1
				maxi = max(maxi, travel)
				break
			a = k[i]
			b = k[j]
			travel += 2
			temp = check(a, b, d)
			maxi = max(maxi, temp + travel)
			i += 1
			j -= 1
	print(maxi)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	st = set(l)
	dc = Counter(l)
	ma = 0
	for t in dc:
		ma = max(ma, dc[t])
	dp = [[0 for tg in range(n + 1)] for ty in range(201)]
	pos = [[] for pl in range(201)]
	for i in range(1, 201):
		for j in range(1, n + 1):
			dp[i][j] = dp[i][j - 1]
			if l[j - 1] == i:
				pos[i].append(j)
				dp[i][j] += 1

	def getto(x, s1, e1):
		return dp[x][e1] - dp[x][s1 - 1]

	def max_X(ss, ee):
		aq = 0
		for ft in st:
			aq = max(aq, getto(ft, ss, ee))
		return aq
	ans = 0
	for i in st:
		for t in range(len(pos[i]) // 2):
			u = pos[i][t] + 1
			v = pos[i][-(t + 1)] - 1
			z = max_X(u, v)
			ans = max(ans, z + 2 * (t + 1))
	print(max(ans, ma))


import sys
from sys import stdin, stdout
import bisect
import math
import heapq

def modinv(n, p):
	return pow(n, p - 2, p)

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())
import math

def Divisors(n):
	l = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l
import statistics

def main():
	t = inin()
	for _ in range(t):
		n = inin()
		a = ain()
		d = {}
		e = {}
		for i in range(n):
			if a[i] not in d:
				d[a[i]] = i
			e[a[i]] = i
		ans = 'NO'
		for i in d:
			if abs(d[i] - e[i]) > 1:
				ans = 'YES'
				break
		print(ans)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'

def readnumbers(zero=0):
	conv = ord if py2 else lambda x: x
	A = []
	numb = zero
	sign = 1
	i = 0
	s = sys.stdin.buffer.read()
	try:
		while True:
			if s[i] >= b'0'[0]:
				numb = 10 * numb + conv(s[i]) - 48
			elif s[i] == b'-'[0]:
				sign = -1
			elif s[i] != b'\r'[0]:
				A.append(sign * numb)
				numb = zero
				sign = 1
			i += 1
	except:
		pass
	if s and s[-1] >= b'0'[0]:
		A.append(sign * numb)
	return A
main()


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	d = defaultdict(lambda : [], d)
	k = 0
	for i in a:
		d[i].append(k)
		k += 1
	arr = []
	for i in d.keys():
		if len(d[i]) > 1:
			arr.append(i)
	ans1 = 0
	for i in d.keys():
		if ans1 < len(d[i]):
			ans1 = len(d[i])
	ans = 0
	if len(arr) == 0:
		print(1)
	else:
		for i in arr:
			w = 0
			e = len(d[i]) - 1
			while w < e:
				m1 = d[i][w]
				m2 = d[i][e]
				c = sorted(a[m1 + 1:m2])
				d1 = {}
				d1 = defaultdict(lambda : 0, d1)
				maax = 0
				for q in a[m1 + 1:m2]:
					d1[q] += 1
					if maax < d1[q]:
						maax = d1[q]
				if ans < maax + 2 * (w + 1):
					ans = maax + 2 * (w + 1)
				w += 1
				e -= 1
		print(max(ans, ans1))


from itertools import combinations
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	f = 0
	for i in range(n):
		if l[i] in l[i + 2:]:
			print('YES')
			break
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	length = 0
	if n == 3:
		if a == a[::-1]:
			print('YES')
		else:
			print('NO')
	else:
		for j in range(n):
			if a.count(a[j]) > 2:
				print('YES')
				length = 1
				break
			elif a.count(a[j]) == 2:
				if a[j + 1:].count(a[j]) > 0 and a[j + 1:].index(a[j]):
					length = 1
					print('YES')
					break
		if length == 0:
			print('NO')


t = int(input())
for loop in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	lis = [None] * (n + 1)
	ans = 'NO'
	for i in range(n):
		if lis[a[i]] == None:
			lis[a[i]] = i
		elif lis[a[i]] + 1 < i:
			ans = 'YES'
			break
	print(ans)


cases = int(input())
while cases > 0:
	arr = []
	n = int(input())
	arr = list(map(int, input().split()))
	can = False
	for i in range(0, n):
		for j in range(i + 2, n):
			if arr[i] == arr[j]:
				can = True
	if can == True:
		print('YES')
	else:
		print('NO')
	cases -= 1


for _ in ' ' * int(input()):
	g = 0
	input()
	a = ' ' + input() + ' '
	for i in set(a.split()):
		s = a.find(i + ' ')
		if a.find(' ' + i + ' ', s + len(i) + 2, len(a)) > 0:
			g = 1
			break
	print('NYOE S'[g::2])


import math
t = int(input())
for test in range(t):
	n = int(input())
	seq = [int(i) for i in input().split(' ')]
	doable = False
	pr = []
	for i in range(n):
		pr.append((seq[i], i))
	pr.sort()
	for i in range(n - 1):
		(p1, x1) = pr[i]
		if i < n - 2:
			(p2, x2) = pr[i + 2]
			if p1 == p2:
				doable = True
				break
		(p2, x2) = pr[i + 1]
		if p1 == p2 and abs(x1 - x2) > 1:
			doable = True
			break
	if doable:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	k = int(input())
	a = list(map(int, input().split()))
	c = set(a)
	n = 0
	k = 0
	d = []
	b = {}
	for i in c:
		b[i] = []
		if a.count(i) > 1:
			d.append(i)
	for i in range(len(a)):
		b[a[i]].append(i)
	for i in d:
		n = b[i]
		for j in range(len(n) - 1):
			for m in range(j + 1, len(n)):
				if n[m] - n[j] > 1:
					k = 1
					break
	if k == 1:
		print('YES')
	else:
		print('NO')


def solve(n, a):
	for i in range(n):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				return True
	return False

def main():
	inp = lambda : [int(x) for x in input().split()]
	i = lambda : int(input())
	tc = i()
	for _ in range(tc):
		n = i()
		a = inp()
		ret = 'YES' if solve(n, a) else 'NO'
		print(ret)
main()


import io, os
import sys
from functools import lru_cache
from heapq import merge, heapify, heappop, heappush
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
from collections import defaultdict as dd, deque, Counter as c
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var))

def outln(var):
	sys.stdout.write(str(var) + '\n')

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for _ in range(int(data())):
	n = int(data())
	arr = l()
	dp = [[0 for i in range(201)] for j in range(n)]
	for i in range(n):
		for j in range(1, 201):
			dp[i][j] = dp[i - 1][j]
		dp[i][arr[i]] += 1
	answer = 1
	for i in set(arr):
		(low, high, cnt) = (0, n - 1, 0)
		while low < high:
			while low < high and arr[low] != i:
				low += 1
			while low < high and arr[high] != i:
				high -= 1
			if low < high:
				cnt += 2
				for j in range(1, 201):
					answer = max(answer, dp[high - 1][j] - dp[low][j] + cnt)
				low += 1
				high -= 1
	outln(answer)


def check(array):
	ar = []
	flag = False
	for i in range(len(array)):
		for j in range(i + 2, len(array)):
			if array[i] == array[j]:
				flag = True
	return flag
T = int(input())
for _ in range(T):
	size = int(input())
	ar = list(map(int, input().split()))
	if check(ar):
		print('YES')
	else:
		print('NO')


T = int(input())
from collections import defaultdict
for i in range(T):
	n = int(input())
	l = list(map(int, input().split()))
	d = defaultdict(list)
	for j in range(n):
		d[l[j]].append(j)
	flag1 = 0
	flag = 0
	for j in d:
		if len(d[j]) == 2:
			for k in d[j]:
				if k - 1 in d[j] or k + 1 in d[j]:
					break
				else:
					flag1 = 1
					break
		if len(d[j]) > 2:
			flag1 = 1
		if flag1 == 1:
			flag = 1
			break
	if flag == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
count = 0
answer = []
while count < t:
	n = int(input())
	ai = list(map(int, input().split()))
	flag = False
	left_set = set()
	for i in range(1, len(ai)):
		if flag == True:
			break
		left_set.add(ai[i - 1])
		for j in ai[i + 1:]:
			if j in left_set:
				answer.append('YES')
				flag = True
				break
	if not flag:
		answer.append('NO')
	count += 1
for i in answer:
	print(i)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def gift():
	for _ in range(t):
		n = int(input())
		lst = list(map(int, input().split()))
		dic = {}
		ans = False
		for i in range(n):
			ina = dic.get(lst[i], [])
			if ina and i != 0:
				if i - 1 not in ina or len(ina) >= 2:
					ans = True
					break
			ina.append(i)
			dic[lst[i]] = ina
		if ans:
			yield 'YES'
		else:
			yield 'NO'
t = int(input())
ans = gift()
print(*ans, sep='\n')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for i1 in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 1
	for i in range(1, 27):
		for j in range(1, 27):
			a = []
			for k in range(n):
				if l[k] == i or l[k] == j:
					a.append(l[k])
			f = 1
			c = 0
			if a == []:
				continue
			ind = []
			for t in range(len(a)):
				if a[t] == i:
					ind.append(t)
				if a[len(a) - 1 - t] == i and t != len(a) - 1 - t:
					ind.append(len(a) - 1 - t)
			ind = list(set(ind))
			ind.sort()
			for t in range(len(ind) // 2):
				s = a[ind[t] + 1:ind[-1 - t]].count(j)
				ans = max(ans, 2 * (t + 1) + s)
	print(ans)


input = __import__('sys').stdin.readline
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if len(a) < 3:
		print('NO')
		continue
	yep = False
	if len(a) == 3:
		if a[0] == a[2]:
			print('YES')
			continue
		else:
			print('NO')
			continue
	for j in range(n):
		for z in range(j + 2, n):
			if a[j] == a[z]:
				print('YES')
				yep = True
				break
		if yep:
			break
	if not yep:
		print('NO')


for _ in range(int(input())):
	answered = False
	n = int(input())
	a = list(map(int, input().split()))
	b = {}
	indexses = {}
	for i in range(len(a)):
		b[a[i]] = b.get(a[i], 0) + 1
		indexses[a[i]] = indexses.get(a[i], []) + [i]
	for i in b:
		if b[i] >= 3:
			print('YES')
			answered = True
			break
		elif b[i] == 2:
			if abs(indexses[i][0] - indexses[i][-1]) != 1:
				print('YES')
				answered = True
				break
	if not answered:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	flag = False
	gap = 0
	for i in range(n):
		if d.get(a[i]) == None:
			d[a[i]] = [1, i]
		else:
			d[a[i]][0] += 1
			if i - d[a[i]][1] >= 2:
				flag = True
				break
			d[a[i]][1] = i
		if d[a[i]][0] >= 3:
			flag = True
			break
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	lenght = int(input())
	table = input().split(' ')
	p = 0
	for (i, x) in enumerate(table):
		if x in table[i + 2:]:
			p = 1
			break
	if p == 1:
		print('YES')
	else:
		print('NO')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 1000000007
INF = float('inf')

def main():
	for _ in range(N()):
		n = N()
		arr = list(RL())
		sm = [[0] * 27]
		for i in arr:
			now = sm[-1].copy()
			now[i] += 1
			sm.append(now)
		res = 0
		for i in range(n):
			for j in range(i, n):
				pre = sm[i]
				ind = [sm[j + 1][ii] - sm[i][ii] for ii in range(27)]
				suf = [sm[-1][ii] - sm[j + 1][ii] for ii in range(27)]
				mp = 0
				for k in range(1, 27):
					mp = max(mp, min(pre[k], suf[k]))
				res = max(res, max(ind) + mp * 2)
		print(res)
main()


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	lset = defaultdict(int)
	rset = defaultdict(int)
	for p in arr[1:]:
		rset[p] += 1
	ans = 'NO'
	for i in range(n - 1):
		for l in lset:
			if l in rset:
				ans = 'YES'
				break
		if i < n - 1:
			rset[arr[i + 1]] -= 1
			if rset[arr[i + 1]] == 0:
				rset.pop(arr[i + 1])
		lset[arr[i]] += 1
	print(ans)


from sys import stdin, stdout
for i1 in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	b = False
	for i in range(n - 2):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				b = True
				break
	if b:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(lambda x: int(x) - 1, input().split()))
	pref = {i: [] for i in range(200)}
	cnt = [0 for i in range(200)]
	counter = []
	for i in range(n):
		pref[l[i]].append(i)
		cnt[l[i]] += 1
		counter.append(cnt.copy())
	ans = 0
	for (idx, val) in enumerate(l):
		j = counter[idx][val]
		r = pref[val][-j]
		if r > idx:
			for x in range(200):
				ans = max(ans, j * 2 + (counter[r - 1][x] - counter[idx][x]))
		if r == idx:
			ans = max(ans, j * 2 - 1)
	print(ans)


for i in range(int(input())):
	n = int(input())
	v = [int(i) for i in input().split()]
	mapa = [-1] * (n + 1)
	for i in range(n):
		if mapa[v[i]] != -1:
			if abs(mapa[v[i]] - i) > 1:
				print('YES')
				break
		else:
			mapa[v[i]] = i
	else:
		print('NO')


import bisect
from collections import defaultdict
from itertools import permutations
from typing import List

def solve(n: int, s: List[int]):
	pos = defaultdict(list)
	for (i, e) in enumerate(s):
		pos[e].append(i)
	max_val = max((len(l) for l in pos.values()))
	for (a, b) in permutations(pos.keys(), 2):
		pa = pos[a]
		pb = pos[b]
		pos_dict = {}
		for i in range(len(pb)):
			pos_dict[i] = bisect.bisect(pa, pb[i])
		for pb_start in range(len(pb)):
			for pb_end in range(pb_start, len(pb)):
				left_len = pos_dict[pb_start]
				right_len = len(pa) - pos_dict[pb_end]
				v = pb_end - pb_start + 1 + min(left_len, right_len) * 2
				max_val = max(max_val, v)
	return max_val
for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	r = solve(n, s)
	print(r)


a = int(input())
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
for i in range(a):
	leng = int(input())
	arr = list(input().split())
	flag = 0
	for k in range(leng):
		c = arr.count(arr[k])
		if c > 2:
			flag = 1
		elif c == 2:
			flag2 = 0
			for j in range(leng - 1):
				if arr[j] == arr[j + 1] and arr[j] == arr[k]:
					flag2 = 1
			if flag2 == 0:
				flag = 1
	if flag == 1:
		print('YES')
	else:
		print('NO')


from collections import defaultdict, deque
from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	mtrx = []
	for _ in range(n):
		mtrx.append([0 for _ in range(201)])
	dct = defaultdict(lambda : deque([]))
	for (i, x) in enumerate(arr):
		mtrx[i][x] += 1
		dct[x].append(i)
	for i in range(1, n):
		for j in range(201):
			mtrx[i][j] += mtrx[i - 1][j]
	msf = 0
	for (k, q) in dct.items():
		occ = 0
		while len(q) > 1:
			l = q.popleft()
			r = q.pop()
			occ += 2
			gb = 0
			if r > l + 1:
				for j in range(201):
					gb = max(gb, mtrx[r - 1][j] - mtrx[l][j])
			msf = max(msf, occ + gb)
		msf = max(msf, occ + len(q))
	print(msf)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	f = 0
	d = dict()
	for i in range(n):
		if a[i] in d and d[a[i]] <= i - 2:
			f = 1
			break
		elif not a[i] in d:
			d[a[i]] = i
	if f == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	c = {}
	for i in range(n):
		a_i = a[i]
		if a_i in c:
			c[a_i].append(i)
		else:
			c[a_i] = [i]
	for (_, v) in c.items():
		if len(v) == 2 and v[0] + 1 == v[1]:
			v.pop()
	has_palindrome = False
	for (_, v) in c.items():
		if len(v) > 1:
			has_palindrome = True
			break
	if has_palindrome:
		print('YES')
	else:
		print('NO')


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	count = Counter(arr)
	temp = list(count.values())
	if max(temp) >= 3:
		print('YES')
	else:
		for i in range(n - 1):
			if arr[i] == arr[i + 1]:
				count[arr[i]] -= 1
		lis = list(count.values())
		m = max(lis)
		if m >= 2:
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	val = False
	for i in range(1, n - 1):
		if a[i - 1] in a[i + 1:n]:
			val = True
			break
	if val:
		print('YES')
	else:
		print('NO')


import collections
import time
import os
import sys
import bisect
import heapq
from typing import List

def solve(N, A):
	uniq = list(sorted(set(A)))
	vmap = {v: i for (i, v) in enumerate(uniq)}
	A = [vmap[v] for v in A]
	wc = collections.Counter(A)
	ans = max(wc.values())
	M = len(uniq)
	for u in range(M):
		wc = collections.Counter(A)
		(l, r) = (-1, N)
		(lc, rc) = (0, 0)
		while l < r:
			l += 1
			lc += 1 if A[l] == u else 0
			wc[A[l]] -= 1
			while l < r and rc < lc:
				r -= 1
				wc[A[r]] -= 1
				rc += 1 if A[r] == u else 0
			if lc == rc and l < r:
				ans = max(ans, lc + rc + max(wc.values()))
	return ans
T = int(input())
ans = []
for ti in range(T):
	N = int(input())
	A = [int(x) for x in input().split()]
	ans.append(solve(N, A))
print('\n'.join(map(str, ans)))


import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub
sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2 ** 62 - 1

def read_int():
	return int(input())

def read_int_n():
	return list(map(int, input().split()))

def read_float():
	return float(input())

def read_float_n():
	return list(map(float, input().split()))

def read_str():
	return input().strip()

def read_str_n():
	return list(map(str, input().split()))

def error_print(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time

	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		error_print(e - s, 'sec')
		return ret
	return wrap

@mt
def slv(N, A):
	d = defaultdict(list)
	for (i, a) in enumerate(A):
		d[a].append(i)
	for v in d.values():
		if len(v) > 2:
			return 'Yes'
		for i in range(len(v) - 1):
			if v[i + 1] - v[i] > 1:
				return 'Yes'
	return 'No'

def main():
	T = read_int()
	for _ in range(T):
		N = read_int()
		A = read_int_n()
		print(slv(N, A))
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	fo = dict()
	lo = dict()
	for i in range(n):
		try:
			fo[a[i]] = min(fo[a[i]], i)
		except KeyError:
			fo[a[i]] = i
		try:
			lo[a[i]] = max(lo[a[i]], i)
		except KeyError:
			lo[a[i]] = i
	f = 0
	for i in a:
		if lo[i] - fo[i] >= 2:
			f = 1
			break
	if f:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	found = False
	for i in range(n):
		if s[i] in s[i + 2:]:
			found = True
			break
	print('YES' if found else 'NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(s) for s in input().split()]
	freq = [-1] * (n + 1)
	dp = [0] * (n + 1)
	flag = False
	for i in range(len(a)):
		x = a[i]
		if freq[x] == -1:
			freq[x] = i
			dp[x] += 1
		elif i - freq[x] > 1:
			flag = True
			break
	if flag:
		print('YES')
	else:
		print('NO')


import sys
R = lambda : map(int, sys.stdin.readline().split())
(t,) = R()
for _ in range(t):
	(n,) = R()
	a = list(R())
	f = False
	d = {}
	for (i, j) in enumerate(a):
		if j not in d.keys():
			d[j] = i
		elif i - d[j] > 1:
			f = True
			break
	print('YES' if f else 'NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	left = {}
	for i in range(n):
		if a[i] not in left:
			left[a[i]] = i
	found = False
	for i in range(n - 1, -1, -1):
		if i - left[a[i]] > 1:
			found = True
	print('YES' if found else 'NO')


queries = int(input())

def solution(array):
	value_dict = {}
	for (index, value) in enumerate(array):
		if value in value_dict:
			if value_dict[value] != index - 1:
				print('YES')
				return
		else:
			value_dict[value] = index
	print('NO')
for _ in range(queries):
	_ = int(input())
	array = [int(item) for item in input().split()]
	solution(array)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
tests = inp()
testcount = 0
while testcount < tests:
	lenseq = inp()
	seq = inlt()
	i = 1
	arr = [seq[0]]
	while i < lenseq:
		if seq[i] in arr:
			if arr.index(seq[i]) != i - 1:
				i += 33 * lenseq
			else:
				arr.append(seq[i])
		else:
			arr.append(seq[i])
		i += 1
	if i > 33 * lenseq:
		print('YES')
	else:
		print('NO')
	testcount += 1


def frea(arr):
	arr.sort()
	ans = [0]
	for i in range(0, len(arr)):
		if i == 0:
			ans[-1] += 1
		elif arr[i] == arr[i - 1]:
			ans[-1] += 1
		else:
			ans.append(1)
	return max(ans)
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = []
	cnt = [0] * 27
	end = [0] * n
	end[n - 1] = [a[n - 1], 1]
	for i in range(0, n):
		s.append([a[i], cnt[a[i]] + 1])
		cnt[a[i]] += 1
	cnt = [0] * 27
	for i in range(n - 1, -1, -1):
		end[i] = [a[i], cnt[a[i]] + 1]
		cnt[a[i]] += 1
	ans = []
	for i in range(n):
		first = s[i]
		for j in range(i + 1, n):
			if end[j][0] == first[0] and end[j][1] == first[1]:
				m = frea(a[i + 1:j])
				ans.append(2 * s[i][1] + m)
	if len(ans) == 0:
		print(frea(a))
	else:
		print(max(ans))


import sys
inf = sys.stdin
input = inf.readline

def read_one_int():
	return int(input().rstrip('\n'))

def read_list_of_ints():
	res = [int(val) for val in input().rstrip('\n').split(' ')]
	return res

def check_seq(seq):
	check_list = [[0, 0] for i in range(len(seq) + 1)]
	for i in range(0, len(seq)):
		if not check_list[seq[i]][0]:
			check_list[seq[i]][0] = i + 1
		else:
			check_list[seq[i]][1] = i + 1
			if check_list[seq[i]][1] - check_list[seq[i]][0] >= 2:
				return 'YES'
				pass
	return 'NO'

def main():
	n = read_one_int()
	for _ in range(n):
		cur_n = read_one_int()
		cur_seq = read_list_of_ints()
		print(check_seq(cur_seq))
main()


def ii():
	return int(input())

def ai():
	return list(map(int, input().split()))

def mi():
	return map(int, input().split())
for _ in range(ii()):
	n = ii()
	lst = ai()
	ans = 'NO'
	for i in range(n):
		ele = lst[i]
		for j in range(i + 2, n):
			if ele == lst[j]:
				ans = 'YES'
				break
	print(ans)


def find_leftmost_right(a, k):
	return indices[a][-k]

def most_repeats(j1, j2):
	if j2 >= j1:
		return max((repeats[j0][j2] - repeats[j0][j1 - 1] for j0 in range(200)))
	return 0
T = int(input())
for t in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	repeats = [[0] * N for _ in range(200)]
	indices = [[] for _ in range(200)]
	for i in range(0, N):
		if i > 0:
			for j in range(200):
				repeats[j][i] = repeats[j][i - 1]
		repeats[A[i] - 1][i] += 1
		indices[A[i]].append(i)
	result = 1
	for i1 in range(len(A)):
		c = repeats[A[i1] - 1][i1]
		i2 = find_leftmost_right(A[i1], c)
		if i2 > i1:
			result = max(result, 2 * c + most_repeats(i1 + 1, i2 - 1))
	print(result)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	checked = []
	ans = False
	for i in range(n):
		if a[i] not in checked:
			if a[i] in a[i + 2:]:
				ans = True
				break
			else:
				checked.append(a[i])
	print('YES' if ans else 'NO')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	was = {arr[0]}
	for i in range(2, len(arr)):
		if arr[i] in was:
			print('YES')
			break
		was.add(arr[i - 1])
	else:
		print('NO')


import sys

def solve():
	n = int(input())
	v = list(map(int, input().split(' ')))
	prev = {}
	for i in range(n):
		temp = v[i]
		if temp in prev and prev[temp] < i - 1:
			return 'YES'
		if temp not in prev:
			prev[temp] = i
	return 'NO'
t = int(input())
while t > 0:
	print(solve())
	t -= 1


it = int(input())
for _ in range(0, it):
	c = int(input())
	a = list(map(int, input().split()))
	i = 0
	flag = 0
	for k in a:
		if i + 2 > c:
			print('NO')
			break
		if flag == 1:
			break
		for h in a[i + 2:]:
			if h == k:
				print('YES')
				flag = 1
				break
		i = i + 1


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	p = 0
	for i in range(len(l)):
		if l[i] in l[i + 2:]:
			p = 1
			print('YES')
			break
	if p == 0:
		print('NO')


n = int(input())
a = []
for i in range(n):
	t = input()
	b = list(map(int, input().split()))
	a.append(b)
for i in a:
	k = len(i)
	num = [-1] * (k + 1)
	c = [0] * (k + 1)
	p = 0
	for j in range(len(i)):
		if num[i[j]] == -1:
			num[i[j]] = j
		else:
			if j - num[i[j]] > 1:
				p = 1
				break
			else:
				num[i[j]] = j
				c[i[j]] += 1
			if c[i[j]] == 2:
				p = 1
				break
		if p == 1:
			break
	if p == 0:
		print('NO')
	else:
		print('YES')


from sys import stdin, stdout
input = stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	pre = [[] for i in range(201)]
	for i in range(1, 201):
		s = 0
		for j in a:
			if j == i:
				s += 1
			pre[i].append(s)
	pos = [[] for i in range(201)]
	ans = 0
	for i in range(n):
		pos[a[i]].append(i)
	for i in range(1, 201):
		ans = max(ans, len(pos[i]))
		for j in range(len(pos[i]) // 2):
			count1 = 2 * (j + 1)
			l = pos[i][j] + 1
			r = pos[i][-(j + 1)] - 1
			count2 = 0
			for k in range(1, 201):
				if l == 0:
					count2 = max(count2, pre[k][r])
				else:
					count2 = max(count2, -pre[k][l - 1] + pre[k][r])
			ans = max(ans, count1 + count2)
	print(ans)


I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	(n,) = I()
	count = [[0] * 201 for i in range(n)]
	a = I()
	for i in range(n):
		for j in range(1, 201):
			count[i][j] = count[i - 1][j]
		count[i][a[i]] += 1
	ans = 1
	for i in range(1, 201):
		s = 0
		e = n - 1
		an = 0
		while s < e:
			while s < n and a[s] != i:
				s += 1
			while e >= 0 and a[e] != i:
				e -= 1
			if s < e:
				an += 2
				temp = [0] * 201
				for j in range(201):
					temp[j] = count[e - 1][j] - count[s][j]
				ans = max(ans, an + max(temp))
			s += 1
			e -= 1
	print(ans)


from sys import stdin, stdout

def write(_s):
	stdout.write(_s)

def writeint(_s):
	stdout.write(str(_s))

def writelist(_l):
	stdout.write(' '.join(_l))

def read():
	return stdin.readline().strip().split(' ')

def readint():
	return int(stdin.readline().strip())

def readints():
	return map(lambda x: int(x) - 1, stdin.readline().strip().split(' '))
limit = 200

def solve():
	n = readint()
	a = list(readints())
	indices = [[] for _ in range(limit)]
	prefs = [[0] * limit]
	for (i, el) in enumerate(a):
		indices[el].append(i)
		pref = list(prefs[-1])
		pref[el] += 1
		prefs.append(pref)
	longest = max(prefs[-1])
	for (i, el) in enumerate(a):
		el = a[i]
		current_len = prefs[i][el] + 1
		other = indices[el][-current_len]
		if other <= i:
			continue
		for el2 in range(limit):
			if el == el2:
				continue
			total = prefs[other][el2]
			if total > 0:
				elements_range = total - prefs[i + 1][el2]
				longest = max(longest, current_len * 2 + elements_range)
	return longest
t = readint()
while t:
	res = solve()
	writeint(res)
	write('\n')
	t -= 1


for _ in range(int(input())):
	n = int(input())
	liStr = input().split()
	arr = []
	for i in liStr:
		arr.append(int(i))
	f = 0
	di = {}
	for i in range(len(arr)):
		if arr[i] in di and di[arr[i]] <= i - 2:
			f = 1
			break
		if arr[i] in di:
			continue
		else:
			di[arr[i]] = i
	if f == 0:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	minIdx = [-1] * 5001
	maxIdx = [-1] * 5001
	for i in range(n):
		if minIdx[a[i]] == -1:
			minIdx[a[i]] = i
		maxIdx[a[i]] = i
	result = 'NO'
	for i in range(5001):
		if minIdx[i] != -1 and minIdx[i] + 1 < maxIdx[i]:
			result = 'YES'
			break
	print(result)


for i in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	d = {}
	st = {}
	j = 0
	f = 0
	for i in l:
		if i in d:
			d[i] += 1
			if d[i] % 2 == 0 and d[i] >= 4:
				f = 1
			if d[i] == 2 and j - st[i] >= 2:
				f = 1
			elif d[i] % 2 != 0 and d[i] >= 3:
				f = 1
		else:
			st[i] = j
			d[i] = 1
		if f:
			break
		j += 1
	if f:
		print('YES')
	else:
		print('NO')


from collections import defaultdict as dd
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = dd(list)
	for i in range(0, n):
		if a[i] not in d:
			d[a[i]] = [i]
		else:
			d[a[i]] += [i]
	fg = 0
	for i in d.keys():
		if len(d[i]) == 2:
			for j in range(1, len(d[i])):
				if d[i][j] > d[i][j - 1] + 1:
					fg = 1
					break
			if fg == 1:
				break
		elif len(d[i]) > 2:
			fg = 1
			break
		else:
			continue
	if fg == 0:
		print('NO')
	else:
		print('YES')


n = int(input())
for i in range(n):
	k = int(input())
	a = [int(x) for x in input().split()]
	s = set()
	prev = -1
	f = False
	for x in a:
		if x in s:
			f = True
			break
		s.add(prev)
		prev = x
	print('YES' if f else 'NO')


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	d = {lst[0]: 1}
	flag = False
	for i in range(1, len(lst)):
		if lst[i] in d.keys():
			if d[lst[i]] > 1 or lst[i] != lst[i - 1]:
				flag = True
				break
			else:
				d[lst[i]] += 1
		else:
			d[lst[i]] = 1
	if flag:
		print('YES')
	else:
		print('NO')


import sys

def solve():
	input = sys.stdin.readline
	T = int(input())
	for _ in range(T):
		N = int(input())
		A = [int(a) for a in input().split()]
		appear = dict()
		for (i, a) in enumerate(A):
			if a in appear:
				appear[a].append(i)
			else:
				appear[a] = [i]
		for key in appear:
			if len(appear[key]) > 2:
				print('YES')
				break
			elif len(appear[key]) == 2:
				if appear[key][0] + 1 < appear[key][1]:
					print('YES')
					break
		else:
			print('NO')
	return 0
solve()


def main():
	t = int(input())
	for __ in range(t):
		n = int(input())
		a = input().split()
		flag = False
		for i in range(n):
			for j in range(i + 1, n):
				if a[i] == a[j]:
					if j - i >= 2:
						flag = True
						break
			if flag:
				break
		if flag:
			print('YES')
		else:
			print('NO')
main()


import math
import bisect
from itertools import groupby

def rints():
	return list(map(int, input().split()))

def solve():
	n = int(input())
	arr = rints()
	first = {}
	for i in range(n):
		if arr[i] not in first:
			first[arr[i]] = i
		elif first[arr[i]] < i - 1:
			return 'YES'
	return 'NO'
t = int(input())
for _ in range(t):
	print(solve())


def rr():
	return input().rstrip()

def rri():
	return int(rr())

def rrl():
	return list(map(int, rr().split()))

def rrt():
	return tuple(map(int, rr().split()))
from collections import defaultdict

def mus(d=0):
	return defaultdict(defaultdict(d))

def ms(x, y, d=0):
	return [[0] * y for i in range(x)]

def ar(x, d=0):
	return [d] * x

def ppm(m, n=0, x=0, y=0):
	print('\n'.join(('\t'.join((str(m[j][i]) for j in range(y or n))) for i in range(x or n))))

def ppa(a, n):
	print('\t'.join(map(str, a[0:n])))

def ppl(a=0, x=[1]):
	x[0] = a or x[0] + 1
	print('-' + '- -' * 14 + '-# ' + str(x[0] - 2) + ' #-' + '- -' * 14 + '-')

def yn(x):
	return 'YES' if x else 'NO'

def rnar():
	return (rri(), rrl())

def r2nar():
	return (rri(), rri(), rrl())

def read():
	return rnar()

def solve(n, arr):
	n = len(arr)
	for i in range(n):
		for j in range(i + 2, n):
			if arr[i] == arr[j]:
				return yn(True)
	return yn(False)
test_count = rri()
for _ in range(test_count):
	input_data = read()
	result = solve(*input_data)
	print(result)


n = int(input())
for _ in range(n):
	t = input()
	(*s,) = map(int, input().split())
	dic = {}
	for (i, a) in enumerate(s):
		if a in dic:
			dic[a].append(i)
		else:
			dic[a] = [i]
	flag = False
	for k in dic:
		if len(dic[k]) > 2:
			print('YES')
			flag = True
			break
		elif len(dic[k]) == 1:
			continue
		elif dic[k][1] - dic[k][0] > 1:
			print('YES')
			flag = True
			break
	if flag:
		continue
	else:
		print('NO')


from sys import stdin
t = int(stdin.readline().rstrip())
for i in range(t):
	N = int(stdin.readline().rstrip())
	data = [int(x) for x in stdin.readline().rstrip().split()]
	f = False
	for i in range(0, N - 2):
		if data[i] in set(data[i + 2:]):
			f = True
	if f:
		print('YES')
	else:
		print('NO')


import collections as c
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = c.Counter(a)
	tr = 0
	for i in b:
		if b[i] > 1:
			if b[i] > 2:
				tr = 1
				break
			else:
				c1 = a.index(i)
				a.remove(i)
				c2 = a.index(i)
				if c1 != c2:
					tr = 1
					break
	if tr == 1:
		print('YES')
	else:
		print('NO')


numbers = 200
for _ in range(int(input())):
	n = int(input())
	seq = [int(x) - 1 for x in input().split()]
	pre = [(n + 1) * [0] for i in range(numbers)]
	pos = [[] for i in range(numbers)]
	for (j, val) in enumerate(seq, 1):
		for i in range(numbers):
			if val == i:
				pre[i][j] = pre[i][j - 1] + 1
				pos[i].append(j)
			else:
				pre[i][j] = pre[i][j - 1]
	ans = 1
	for i in range(numbers):
		max_val = pre[i][-1] // 2
		for x in range(max_val):
			bmin = pos[i][x]
			bmax = pos[i][-x - 1]
			ymax = 0
			if bmax - bmin > 1:
				for j in range(numbers):
					ymax = max(ymax, pre[j][bmax - 1] - pre[j][bmin])
			ans = max(ans, ymax + 2 * (x + 1))
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	l = map(int, list(input().split()))
	d = {}
	done = 0
	for (i, j) in enumerate(l, 0):
		if j not in d.keys():
			d[j] = i
		elif i - d[j] > 1:
			done = 1
			break
	if done:
		print('YES')
	else:
		print('NO')


def f(n, l):
	d = dict()
	for (i, el) in enumerate(l):
		if el in d:
			d[el].append(i)
		else:
			d[el] = [i]
	for k in d:
		if len(d[k]) >= 3:
			return 'YES'
		elif len(d[k]) == 2:
			(a, b) = (d[k][0], d[k][1])
			if b - a >= 2:
				return 'YES'
	return 'NO'
t = int(input())
for test in range(t):
	n = int(input())
	l = list(map(int, input().rstrip().split()))
	print(f(n, l))


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	flag = False
	n = int(input())
	a = list(map(int, input().split()))
	memo = [0] * n
	ng = []
	for i in range(n):
		memo[a[i] - 1] += 1
		if i != 0:
			if a[i - 1] == a[i]:
				ng.append(a[i] - 1)
	for i in range(n):
		if memo[i] >= 2 and flag == False:
			if i not in ng:
				print('YES')
				flag = True
			elif memo[i] >= 3:
				print('YES')
				flag = True
	if flag == False:
		print('NO')


import sys
import math
from itertools import permutations
input = lambda : sys.stdin.readline().strip()
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	pre = [[0] * 201 for _ in range(n + 1)]
	st = [[] for _ in range(201)]
	for i in range(1, n + 1):
		st[a[i - 1]].append(i)
		for j in range(1, 201):
			pre[i][j] = pre[i - 1][j]
		pre[i][a[i - 1]] += 1
	ans = 0
	for i in range(1, 201):
		sz = len(st[i])
		ans = max(ans, sz)
		for j in range(sz // 2):
			idx1 = st[i][j]
			idx2 = st[i][sz - 1 - j]
			mid = 0
			for k in range(1, 201):
				if i == k:
					continue
				mid = max(mid, pre[idx2][k] - pre[idx1][k])
			ans = max(ans, 2 * (j + 1) + mid)
	print(ans)


for _ in range(int(input())):
	N = int(input())
	L = list(map(int, input().split()))
	k = 0
	for i in range(N):
		for j in range(N - i - 2):
			if L[i] == L[N - j - 1]:
				k = 1
				break
	print('Yes' if k else 'No')


import io
import os
from collections import Counter, defaultdict, deque
from bisect import bisect_left, bisect_right, bisect

def solve(N, A):
	indices = defaultdict(list)
	cumCounts = [[0 for i in range(200)]]
	for (i, x) in enumerate(A):
		cumCounts.append(list(cumCounts[-1]))
		cumCounts[-1][x] += 1
		indices[x].append(i)
	best = max(Counter(A).values())
	for x in indices.keys():
		L = len(indices[x])
		aLen = 0
		left = indices[x][aLen]
		right = indices[x][L - 1 - aLen]
		while left < right:
			for y in indices.keys():
				if x != y:
					bLen = cumCounts[right][y] - cumCounts[left][y]
					best = max(best, 2 * (aLen + 1) + bLen)
			aLen += 1
			left = indices[x][aLen]
			right = indices[x][L - 1 - aLen]
	return best
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
T = int(input())
for t in range(T):
	(N,) = [int(x) for x in input().split()]
	A = [int(x) - 1 for x in input().split()]
	ans = solve(N, A)
	print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import copy
t = int(input())
for _ in range(t):
	n = int(input())
	li = [int(i) for i in input().split(' ')]
	da = [[] for __ in range(200)]
	pref = [[0 for i in range(200)]]
	for (p, j) in enumerate(li):
		da[j - 1].append(p)
		pref[-1][j - 1] += 1
		pref.append(copy.deepcopy(pref[-1]))
	ans = 1
	ps = [0 for i in range(200)]
	for (p, i) in enumerate(li):
		ps[i - 1] += 1
		if da[i - 1][ps[i - 1] - 1] < da[i - 1][-ps[i - 1]]:
			for c in range(200):
				if c != i - 1:
					ans = max(ans, ps[i - 1] * 2 + pref[da[i - 1][-ps[i - 1]]][c] - pref[da[i - 1][ps[i - 1] - 1]][c])
				else:
					ans = max(ans, len(da[c]))
	print(ans)


for _ in [0] * int(input()):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	c = []
	for i in a:
		if i not in b:
			b.append(i)
		elif i not in c:
			c.append(i)
	if len(a) > len(b + c):
		print('YES')
	else:
		z = 0
		for i in c:
			k = a.index(i)
			if a[k] != a[k + 1]:
				z = 1
				break
		if z == 1:
			print('YES')
		else:
			print('NO')


import sys
input = sys.stdin.readline

def main():
	t = int(input())
	for _ in range(t):
		N = int(input())
		A = [int(x) for x in input().split()]
		ans = [-1] * (N + 1)
		f = False
		for (i, a) in enumerate(A):
			if ans[a] == -1:
				ans[a] = i
			elif abs(ans[a] - i) >= 2:
				f = True
		if f:
			print('YES')
		else:
			print('NO')
main()


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	if len(arr) < 3:
		print('NO')
	elif len(arr) == 3:
		if arr == arr[::-1]:
			print('YES')
		else:
			print('NO')
	elif arr == arr[::-1]:
		print('YES')
	else:
		for i in range(len(arr)):
			if arr[i] in arr[i + 2:]:
				print('YES')
				break
		else:
			print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	i = 0
	j = n - 1
	d = dict()
	flag = False
	for i in range(n):
		if flag == True:
			break
		for j in range(i + 1, n):
			if l[j] == l[i]:
				if j - i >= 2:
					flag = True
					break
	if flag == False:
		print('NO')
	else:
		print('YES')


T = int(input())
for _ in range(T):
	n = int(input())
	ar = list(map(int, input().split()))
	ok = 0
	for i in range(n):
		(u, v) = (ar[i], 0)
		for j in range(i + 1, n):
			if ar[j] == u and v:
				ok = 1
				break
			v = ar[j]
		if ok:
			break
	print('YES' if ok else 'NO')


import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__

def go():
	n = int(input())
	a = list(map(int, input().split()))
	d = list(set(a))
	rd = {aa: i for (i, aa) in enumerate(d)}
	a = [rd[aa] for aa in a]
	m = len(d)
	cnt = {}
	cnt_loc = {}
	cum = [0] * m
	cums = []
	for (i, aa) in enumerate(a):
		cnt[aa] = cnt.get(aa, 0) + 1
		cum[aa] += 1
		cnt_loc.setdefault(aa, {})[cnt[aa]] = i
		cums.append(cum.copy())
	best = 1
	for aa in range(m):
		cnt_a = cnt[aa]
		for ca in range(1, cnt_a // 2 + 1):
			left = cnt_loc[aa][ca]
			right = cnt_loc[aa][cnt_a - ca + 1]
			betw = max((rr - ll for (rr, ll) in zip(cums[right - 1], cums[left])))
			best = max(best, 2 * ca + betw)
	return best
t = int(input())
ans = []
for _ in range(t):
	ans.append(str(go()))
print('\n'.join(ans))


def solve(a):
	prev = {}
	for (i, x) in enumerate(a):
		if x in prev:
			if prev[x] != i - 1:
				return True
		else:
			prev[x] = i
	return False
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if solve(a):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = [int(x) - 1 for x in input().split()]
	id = [[] for i in range(200)]
	cnt = [[0] * n for i in range(200)]
	for (i, x) in enumerate(a):
		cnt[x][i] += 1
		id[x].append(i)
	for i in range(200):
		for j in range(1, n):
			cnt[i][j] += cnt[i][j - 1]
	res = 1
	for a in range(200):
		(l, r, t) = (0, len(id[a]) - 1, 2)
		while l < r:
			for b in range(200):
				res = max(res, t + cnt[b][id[a][r] - 1] - cnt[b][id[a][l]])
			(l, r, t) = (l + 1, r - 1, t + 2)
	print(res)


n = int(input())
m = [list(map(int, input().split())) for i in range(n * 2)]
m = [item for (i, item) in enumerate(m) if i % 2 == 1]
k = []
for i in m:
	for (j, item) in enumerate(i):
		if len(i) - i[::-1].index(item) - 1 >= j + 2:
			k.append(item)
			break
	print('YES' if len(k) == 1 else 'NO')
	k.clear()


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	prev = 0
	se = set()
	h = 0
	cc = 0
	for i in range(len(l)):
		sdf = len(se)
		se.add(l[i])
		nlen = len(se)
		if nlen == sdf:
			cc += 1
			if prev == l[i]:
				nothing = 0
				if cc >= 2:
					print('YES')
					h = 1
					break
			else:
				print('YES')
				h = 1
				break
		else:
			cc = 0
		prev = l[i]
	if h == 0:
		print('NO')


t = int(input(''))
for i in range(t):
	n = int(input(''))
	a = list(map(int, input('').split(' ')))
	pref = [[0] * (n + 1) for i in range(26)]
	a = [0] + a
	for c in range(26):
		for i in range(1, n + 1):
			if a[i] == c + 1:
				pref[c][i] = pref[c][i - 1] + 1
			else:
				pref[c][i] = pref[c][i - 1]
	ans = 0
	for aa in range(26):
		l = 1
		r = n
		cnt = 0
		for c in range(26):
			ans = max(ans, 2 * cnt + pref[c][n])
		while l < r:
			while l <= n and a[l] != aa + 1:
				l += 1
			if r <= l:
				break
			while r >= 1 and a[r] != aa + 1:
				r -= 1
			if r <= l:
				break
			cnt += 1
			for c in range(26):
				ans = max(ans, 2 * cnt + (pref[c][r - 1] - pref[c][l]))
			l += 1
			r -= 1
	print(ans)


from sys import stdin

def inp():
	return stdin.buffer.readline()

def itg():
	return int(inp())

def mpint():
	return map(int, inp().split())

def main():
	n = itg()
	arr = list(mpint())
	d = dict()
	for i in range(n):
		if arr[i] in d:
			if d[arr[i]] != i - 1:
				return 'YES'
		else:
			d[arr[i]] = i
	return 'NO'
for case in range(itg()):
	print(main())


cases = int(input())
for i in range(cases):
	l = int(input())
	a = input().split(' ')
	arr = []
	for ele in a:
		arr.append(int(ele))
	boo = False
	for j in range(len(arr)):
		left = arr[j]
		for k in range(j + 2, len(arr)):
			right = arr[k]
			if left == right:
				boo = True
				break
	if boo:
		print('YES')
	else:
		print('NO')


import sys
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 998244353
for _ in range(int(ri())):
	n = int(ri())
	a = Ri()
	count = list2d(27, len(a) + 1, 0)
	for i in range(len(a)):
		for j in range(1, 27):
			count[j][i + 1] = count[j][i]
		count[a[i]][i + 1] += 1
	ans = -1
	for i in range(len(a)):
		for j in range(i + 1, len(a)):
			if a[i] == a[j]:
				left = count[a[i]][i + 1]
				right = count[a[i]][len(a)] - count[a[i]][j]
				maxx = 0
				for k in range(1, 27):
					if k != a[i]:
						cnt = count[k][j + 1] - count[k][i]
						maxx = max(maxx, cnt)
				ans = max(ans, min(left, right) * 2 + maxx)
	for i in range(1, 27):
		ans = max(ans, count[i][n])
	print(ans)


import math
n = int(input())
for i in range(n):
	flag = 0
	m = int(input())
	a = [int(i) for i in input().split()]
	for j in range(0, len(a) - 1):
		pos = j
		for k in range(j + 1, len(a)):
			if a[k] == a[j]:
				pos = k
		if abs(pos - j) >= 2:
			flag = 1
	if flag == 0:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	res = 'NO'
	for i in range(n):
		x = a[i]
		if n - i - a[::-1].index(x) > 2:
			res = 'YES'
			break
	print(res)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {i: [] for i in range(1, 27)}
	i = 0
	for x in a:
		d[x].append(i)
		i += 1
	ans = 1
	for x in d:
		b = d[x]
		l = len(b)
		for i in range(0, l // 2):
			(x, y) = (b[i] + 1, b[-i - 1])
			f = [0] * 27
			for j in range(x, y):
				f[a[j]] += 1
			ans = max(ans, max(f) + 2 * (i + 1))
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	for i in range(n - 2):
		if arr[i] in arr[i + 2:]:
			print('YES')
			break
	else:
		print('NO')


from sys import stdin
t = int(input())
for _ in range(t):
	n = int(stdin.readline())
	nums = list(map(int, stdin.readline().strip().split()))
	d = dict()
	ans = 'NO'
	for (i, num) in enumerate(nums):
		if num in d:
			if i - d[num] > 1:
				ans = 'YES'
				break
		else:
			d[num] = i
	print(ans)


import sys
t = int(input())
for _ in range(t):
	n = int(input())
	nums = [int(x) for x in input().split(' ')]
	nums_map = {}
	for num in nums:
		if num not in nums_map:
			nums_map[num] = 1
		else:
			nums_map[num] += 1
	bad = True
	for i in range(n - 1):
		if nums_map[nums[i]] == 2:
			if nums[i + 1] != nums[i]:
				bad = False
				break
			else:
				nums_map[nums[i]] = 0
		elif nums_map[nums[i]] >= 3:
			bad = False
			break
	if bad:
		print('NO')
	else:
		print('YES')


def f(l):
	d = {}
	for i in l:
		try:
			d[i] += 1
		except:
			d[i] = 1
	maxm = 0
	for i in d:
		if d[i] > maxm:
			maxm = d[i]
	return maxm
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	m = list(set(l))
	ans = {}
	for i in m:
		ans[i] = 0
	for k in ans:
		i = 0
		j = n - 1
		count = 0
		while i <= j:
			b = 0
			while l[i] != k and i < n:
				i += 1
			while l[j] != k and j >= 0:
				j -= 1
			if i < j and i < n and (j >= 0):
				count += 1
			elif i == j:
				b = 1
			else:
				break
			ans[k] = max(ans[k], b + 2 * count + f(l[i + 1:j]))
			i += 1
			j -= 1
	maxm = 0
	for i in ans:
		if ans[i] > maxm:
			maxm = ans[i]
	print(maxm)


for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	flag = False
	for i in range(n):
		if arr[i] in arr[i + 2:]:
			flag = True
	if flag == True:
		print('YES')
	else:
		print('NO')


import sys

def zap():
	n = int(input())
	a = list(map(int, input().split()))
	c = a[::-1]
	for i in range(n):
		l = a.index(a[i])
		r = n - 1 - c.index(a[i])
		if l != r and r - l > 1:
			return 'YES'
	return 'NO'
for _ in range(int(input())):
	print(zap())


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	d = dict()
	for i in range(n):
		if a[i] in d:
			d[a[i]].append(i)
		else:
			d[a[i]] = [i]
	flag = 1
	for i in d:
		if len(d[i]) >= 3:
			print('YES')
			break
	else:
		for i in d:
			if len(d[i]) >= 2 and d[i][-1] - d[i][0] >= 2:
				print('YES')
				break
		else:
			print('NO')


import sys
from collections import defaultdict
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	arr = list(map(int, sys.stdin.readline().split()))
	dic = defaultdict(list)
	for i in range(n):
		dic[arr[i]].append(i)
	z = False
	for i in dic:
		(a, b) = (max(dic[i]), min(dic[i]))
		if a - b > 1:
			z = True
	if z:
		print('YES')
	else:
		print('NO')


import pdb
n = int(input())
case_set = {}
for i in range(n):
	num_of_input = int(input())
	seq = input()
	seq = list(map(int, seq.split(' ')))
	case_set[i] = seq
for i in range(n):
	case = case_set[i]
	leng_of_case = len(case)
	occur_book = {}
	for j in range(leng_of_case + 1):
		if j == leng_of_case:
			print('NO')
			break
		if j >= 2 and case[j] == case[j - 2]:
			print('YES')
			break
		elif j >= 1 and case[j] == case[j - 1]:
			continue
		elif case[j] in occur_book.keys():
			occur_book[case[j]] += 1
		else:
			occur_book[case[j]] = 1
		if occur_book[case[j]] >= 2:
			print('YES')
			break


from collections import defaultdict

def find_num(arr):
	store = defaultdict(int)
	max_count = 0
	for num in arr:
		store[num] += 1
		if max_count < store[num]:
			max_count = store[num]
	return max_count
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))[:n]
	visited = defaultdict(int)
	last_seen = defaultdict(list)
	ans = 0
	i = 0
	flag = 0
	while i < n:
		num = arr[i]
		if visited[num] == False:
			start = i
			while i < n and arr[i] == num:
				i += 1
			end = i
			last_seen[num].append((start, end))
			if end - start >= 3:
				print('YES')
				flag = 1
				break
			visited[num] = True
		elif visited[num] == True:
			print('YES')
			flag = 1
			break
	if flag == 0:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	for (i, num) in enumerate(lst[::-1]):
		if n - lst.index(num) - i > 2:
			print('YES')
			break
	else:
		print('NO')


def three_blocks_length(sequence, outer, inner):
	filtered = list(filter(lambda x: x == outer or x == inner, sequence))
	max_length = 0
	outer_indices = []
	inner_indices = []
	for (i, item) in enumerate(filtered):
		if item == outer:
			outer_indices.append(i)
		if item == inner:
			inner_indices.append(i)
	for i in range(len(outer_indices) // 2):
		inner_count = filtered[outer_indices[i]:outer_indices[len(outer_indices) - i - 1]].count(inner)
		max_length = max(max_length, 2 * (i + 1) + inner_count)
	return max_length

def solve(sequence):
	distinct = set(sequence)
	max_length = 0
	for outer in distinct:
		for inner in distinct:
			if outer != inner:
				max_length = max(max_length, three_blocks_length(sequence, outer, inner))
			else:
				max_length = max(max_length, sequence.count(outer))
	return max_length
for i in range(int(input())):
	n = int(input())
	sequence = [int(x) for x in input().split()]
	print(solve(sequence))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = [[0 for i in range(n + 1)] for i in range(200)]
	pos = [[] for i in range(200)]
	for i in range(n):
		for j in range(200):
			cnt[j][i + 1] = cnt[j][i]
		cnt[a[i] - 1][i + 1] += 1
		pos[a[i] - 1].append(i)
	ans = 0
	for i in range(200):
		ans = max(ans, len(pos[i]))
		for j in range(int(len(pos[i]) / 2)):
			l = pos[i][j] + 1
			r = pos[i][len(pos[i]) - j - 1] - 1
			for k in range(200):
				s = cnt[k][r + 1] - cnt[k][l]
				ans = max(ans, (j + 1) * 2 + s)
	print(ans)


T = int(input())
Q = []
for t in range(T):
	N = int(input())
	A = [int(_) for _ in input().split()]
	Q.append((N, A))
R = []
for (N, A) in Q:
	prefC = [[0] * 26 for _ in range(N + 1)]
	for c in range(26):
		for i in range(N):
			prefC[i + 1][c] = prefC[i][c]
			if A[i] == c + 1:
				prefC[i + 1][c] += 1
	answer = 0
	for l in range(N):
		for r in range(l, N):
			cntin = 0
			cntout = 0
			for c in range(26):
				cntin = max(cntin, prefC[r + 1][c] - prefC[l][c])
				cntout = max(cntout, min(prefC[l][c], prefC[N][c] - prefC[r + 1][c]))
			answer = max(answer, 2 * cntout + cntin)
	R.append(answer)
print('\n'.join(map(str, R)))


import sys
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]
for _ in range(int(ri())):
	n = int(ri())
	a = Ri()
	count = list2d(201, len(a) + 1, 0)
	for i in range(len(a)):
		for j in range(1, 201):
			count[j][i + 1] = count[j][i]
		count[a[i]][i + 1] += 1
	dic = {}
	for i in range(len(a)):
		if a[i] in dic:
			dic[a[i]].append(i)
		else:
			dic[a[i]] = [i]
	ans = -1
	for i in dic:
		lis = dic[i]
		l = 0
		r = len(lis) - 1
		tans = 1
		while l < r:
			left = lis[l]
			right = lis[r]
			tmax = 0
			for j in range(1, 201):
				if i != j:
					cnt = count[j][right + 1] - count[j][left]
					tmax = max(tmax, cnt)
			tans = max(tans, (l + 1) * 2 + tmax)
			l += 1
			r -= 1
		ans = max(ans, tans)
	for i in range(1, 201):
		ans = max(ans, count[i][n])
	print(ans)


def f(arr, width):
	p = False
	for i in range(width - 2):
		j = width - 1
		while j - i > 1:
			if arr[i] == arr[j]:
				p = True
				break
			j -= 1
		if p:
			break
	if p:
		return 'YES'
	else:
		return 'NO'
t = int(input())
for x in range(t):
	n = int(input())
	l1 = list(map(int, input().split()))
	print(f(l1, n))


t = int(input())
for _ in range(t):
	n = input()
	n = int(n)
	L = list(input().split(' '))
	A = [int(x) for x in L]
	D = {}
	ram = 0
	for i in range(n):
		if not A[i] in D:
			D[A[i]] = [i]
		else:
			D[A[i]].append(i)
	for itm in D:
		if len(D[itm]) == 2:
			if D[itm][1] - D[itm][0] > 1:
				ram = 1
				break
		if len(D[itm]) > 2:
			ram = 1
			break
	if ram == 1:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
import math, sys
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import bisect as bi
import heapq

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return map(int, stdin.readline().split())

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def main():
	try:
		n = I()
		l = list(In())
		d = defaultdict(list)
		flag = 0
		for x in range(n):
			if d.get(l[x], -1) != -1:
				if len(d[l[x]]) >= 2:
					flag = 1
					break
				elif x - d[l[x]][-1] >= 2:
					flag = 1
					break
				else:
					d[l[x]].append(x)
			else:
				d[l[x]].append(x)
		if flag:
			yes()
		else:
			no()
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(I()):
	main()


for _ in range(int(input())):
	n = int(input())
	p = None
	prev = set()
	for d in map(int, input().split()):
		if p is not None:
			if d in prev:
				print('YES')
				break
		prev.add(p)
		p = d
	else:
		print('NO')


from collections import defaultdict
t = int(input())
for _ in range(t):
	counts = defaultdict(int)
	n = int(input())
	a = list(map(int, input().split()))
	prev = 0
	for x in a:
		if x != prev:
			counts[x] += 1
			prev = x
		else:
			prev = 0
	if max(counts.values()) > 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for test in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = set(a)
	c = {}
	for x in b:
		c[x] = []
	for i in range(n):
		c[a[i]].append(i)
	flag = 0
	for x in b:
		if len(c[x]) >= 2:
			d = c[x][len(c[x]) - 1] - c[x][0]
			if d > 1:
				flag = 1
				break
	if flag == 0:
		print('NO')
	else:
		print('YES')


t = int(input())
while t > 0:
	n = int(input())
	a = [int(i) for i in input().split()]
	cnt = [[0 for i in range(n + 1)] for i in range(26)]
	for i in range(0, n):
		for j in range(0, 26):
			cnt[j][i + 1] = cnt[j][i]
		cnt[a[i] - 1][i + 1] += 1
	ans = 0
	for i in range(0, 26):
		ans = max(ans, cnt[i][n - 1])
	for l in range(0, n):
		for r in range(l, n):
			cntin = 0
			cntout = 0
			for el in range(0, 26):
				cntin = max(cntin, cnt[el][r + 1] - cnt[el][l])
				cntout = max(cntout, min(cnt[el][l], cnt[el][n] - cnt[el][r + 1]) * 2)
			ans = max(ans, cntin + cntout)
	print(ans)
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	m = 0
	l1 = [0] * 26
	for j in range(len(l1)):
		l1[j] = [0] * n
	for j in range(n):
		l1[l[j] - 1][j] += 1
	for j in range(26):
		for k in range(1, n):
			l1[j][k] += l1[j][k - 1]
	for j in range(1, 27):
		for k in range(1, 27):
			if l1[k - 1][n - 1] == 0 and l1[j - 1][n - 1] == 0:
				continue
			if j == k:
				m = max(m, l1[k - 1][n - 1])
				continue
			m = max(m, l1[k - 1][n - 1])
			a = 0
			b = n - 1
			v = 0
			while a < b:
				if v != 0:
					a += 1
					b -= 1
				while a < n and l[a] != j:
					a += 1
				while b >= 0 and l[b] != j:
					b -= 1
				v += 1
				if a == n or b == -1:
					break
				if a >= b:
					break
				m = max(m, 2 * v + l1[k - 1][b] - l1[k - 1][a])
	print(m)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	d = {i: 0 for i in range(1, n + 1)}
	s = 0
	for i in range(n):
		d[a[i]] += 1
		if d[a[i]] >= 2 and a[i - 1] != a[i]:
			s = 1
		if d[a[i]] > 2:
			s = 1
			break
	if s == 0:
		print('NO')
	else:
		print('YES')


from collections import defaultdict

def solve(n, a):
	m = defaultdict(list)
	for i in range(n):
		m[a[i]].append(i)
	ans = 'NO'
	for (i, v) in m.items():
		if len(v) >= 3:
			ans = 'YES'
			break
	for (i, v) in m.items():
		if len(v) >= 2 and v[-1] - v[0] > 1:
			ans = 'YES'
			break
	return ans
t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	print(solve(n, a))
	t -= 1


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	sol = False
	for (i, a_i) in enumerate(a):
		(mini, maxi) = d.get(a_i, (n * 10, -n * 10))
		mini = min(mini, i)
		maxi = max(maxi, i)
		diff = abs(mini - maxi)
		if diff > 1 and diff < n:
			sol = True
			break
		d[a_i] = (mini, maxi)
	if sol:
		print('YES')
	else:
		print('NO')

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()


T = int(input())
for t in range(T):
	n = int(input())
	A = [int(i) for i in input().split()]
	B = dict()
	for i in range(n):
		if A[i] in B:
			B[A[i]].append(i)
			if len(B[A[i]]) >= 2 and B[A[i]][-1] - B[A[i]][-2] > 1 or (len(B[A[i]]) >= 3 and B[A[i]][-1] - B[A[i]][-3] > 1):
				print('YES')
				break
		else:
			B[A[i]] = [i]
	else:
		print('NO')


import sys

def data():
	return sys.stdin.buffer.readline().strip()
out = sys.stdout.write

def mdata():
	return map(int, data().split())
from collections import defaultdict as dd
for t in range(int(data())):
	n = int(data())
	A = list(mdata())
	s = set(A)
	m = 1
	dp = [[0] * 201 for i in range(n)]
	for i in range(n):
		for j in range(1, 201):
			dp[i][j] = dp[i - 1][j]
		dp[i][A[i]] += 1
	for i in s:
		(start, end) = (0, n - 1)
		cnt = 0
		while start < end:
			while start < end and A[start] != i:
				start += 1
			while start < end and A[end] != i:
				end -= 1
			if start < end:
				cnt += 2
				for j in range(1, 201):
					m = max(m, cnt + dp[end - 1][j] - dp[start][j])
				start += 1
				end -= 1
	out(str(m) + '\n')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().strip().split(' ')))
	f = [-1 for _ in range(5001)]
	k1 = 0
	for j in range(n):
		if f[a[j]] == -1:
			f[a[j]] = j
		elif abs(j - f[a[j]]) > 1:
			k1 = 1
			break
	if k1 == 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	pr = 0
	n = int(input())
	a = [int(i) for i in input().split()]
	b = []
	z = 0
	zch = 0
	for i in range(n):
		if a[i] in b and i - a.index(a[i]) > 1:
			pr = 1
			break
		elif a[i] not in b:
			b.append(a[i])
	print(['NO', 'YES'][pr])


for t in range(int(input())):
	n = int(input())
	a = input().split()
	b = a[::-1]
	dp = [[0] * (n + 1) for i in range(n + 1)]
	ans = 'NO'
	for i in range(1, n + 1):
		for j in range(1, n + 1):
			dp[i][j] = max(dp[i - 1][j - 1] + (a[i - 1] == b[j - 1]), dp[i - 1][j], dp[i][j - 1])
			if dp[i][j] >= 3:
				ans = 'YES'
				break
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	maxi = 0
	index = {}
	s = list(set(a))
	n1 = len(s)
	index = [[] for i in range(201)]
	for i in range(n):
		index[a[i]].append(i)
	maxi = 0
	pref = [[0] * n for i in range(201)]
	for i in range(201):
		suma = 0
		for j in range(n):
			if a[j] == i:
				suma += 1
			pref[i][j] = suma
	for i in range(201):
		j1 = 0
		n2 = len(index[i])
		j2 = n2 - 1
		for j in range(n2 // 2):
			cnt = 2 * (j + 1)
			maxi1 = 0
			for itr in range(201):
				maxi1 = max(maxi1, pref[itr][index[i][j2] - 1] - pref[itr][index[i][j1]])
			maxi = max(maxi, maxi1 + cnt)
			j1 += 1
			j2 -= 1
	for i in range(201):
		maxi = max(maxi, len(index[i]))
	sys.stdout.write(str(maxi) + '\n')


t = int(input())
ans = [False for i in range(t)]
for i in range(t):
	n = int(input())
	a = [int(j) for j in input().split()]
	a1 = [[0, -1] for j in range(5001)]
	for j in range(n):
		a1[a[j]][0] += 1
		if a1[a[j]][1] == -1:
			a1[a[j]][1] = j
		if a1[a[j]][0] > 1:
			if j - a1[a[j]][1] > 1:
				ans[i] = True
				break
for i in ans:
	if i == True:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	k = 1
	n = int(input())
	st = list(map(int, input().split()))
	for i in range(n):
		if i != n - 2:
			if st[i + 2:].count(st[i]):
				print('YES')
				k = 0
				break
	if k:
		print('NO')


t = int(input())
for k in range(t):
	n = int(input())
	a = list(map(int, input().split()))[:n]
	f = True
	for i in range(n):
		j = i + 1
		for j in range(j, n):
			if a[i] == a[j]:
				if i + 1 != j:
					f = False
					break
	if f == False:
		print('YES')
	else:
		print('NO')


t = int(input())
for j in range(t):
	n = int(input())
	array = [int(x) for x in input().split(' ')]
	occurance = [0] * (n + 1)
	values = set()
	previous = -1
	finished = False
	for i in range(n):
		values.add(array[i])
		occurance[array[i]] += 1
		if occurance[array[i]] >= 2:
			if array[i - 1] != array[i] and len(values) >= 2:
				finished = True
				print('YES')
				break
	factor = [x for x in occurance if x >= 3]
	if not finished:
		if len(factor) > 0:
			print('YES')
		else:
			print('NO')


t = int(input())

def checkPlindrome(arr, n):
	dic = {}
	last = arr[0]
	for k in range(n):
		if arr[k] in dic:
			dic[arr[k]] += 1
		else:
			dic[arr[k]] = 1
		if dic[arr[k]] == 2 and arr[k] != last:
			return 1
		elif dic[arr[k]] > 2:
			return 1
		last = arr[k]
		k += 1
	return 0
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	x = checkPlindrome(a, n)
	if x == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	isPalindrome = False
	visited = {}
	for i in range(n):
		char = s[i]
		if char not in visited:
			visited[char] = i
		elif i - visited[char] > 1:
			isPalindrome = True
	if isPalindrome:
		print('YES')
	else:
		print('NO')


n = int(input())
i = 0
while i < n:
	q = int(input())
	mas = [int(j) for j in input().split()]
	flag = False
	for k in range(len(mas) - 2):
		try:
			if mas.index(mas[k], k + 2) > 0:
				print('YES')
				flag = True
				break
		except:
			flag = False
	if not flag:
		print('NO')
	i += 1


import operator
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	c = dict()
	ans = 1
	for a in range(0, n, 1):
		if arr[a] not in c:
			c[arr[a]] = 0
		c[arr[a]] += 1
		ind = -1
		co = c[arr[a]]
		for b in range(n - 1, a + 1, -1):
			if arr[b] == arr[a]:
				co -= 1
			if co == 0:
				ind = b
				break
		ans = max(ans, c[arr[a]])
		if ind != -1:
			d = dict()
			for e in range(a + 1, b, 1):
				if arr[e] not in d:
					d[arr[e]] = 0
				d[arr[e]] += 1
			temp = 0
			if max(d, key=d.get) in d:
				temp = d[max(d, key=d.get)] + 2 * c[arr[a]]
			ans = max(ans, temp)
	print(ans)


def solve():
	n = int(input())
	arr = list(map(int, input().split()))
	hm = {}
	for i in range(n):
		hm.setdefault(arr[i], [])
		hm[arr[i]].append(i)
		if len(hm[arr[i]]) >= 3:
			return 'YES'
		if len(hm[arr[i]]) == 2 and abs(hm[arr[i]][0] - hm[arr[i]][1]) > 1:
			return 'YES'
	return 'NO'
t = int(input())
for _ in range(t):
	print(solve())


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = {}
	e = {}
	for (i, x) in enumerate(a):
		e[x] = i
	for i in range(n - 1, -1, -1):
		b[a[i]] = i
	for x in b:
		if e[x] - b[x] + 1 >= 3:
			print('YES')
			break
	else:
		print('NO')


t = int(input())
ans = []
c = 0
for i in range(t):
	ans.append('NO')
while c < t:
	n = int(input())
	Arrs = input().split()
	for i in range(n):
		Arrs[i] = int(Arrs[i])
	First = []
	for i in range(n):
		First.append(-1)
	for i in range(n):
		if First[Arrs[i] - 1] == -1:
			First[Arrs[i] - 1] = i
		if First[Arrs[i] - 1] != -1:
			if i - First[Arrs[i] - 1] > 1:
				ans[c] = 'YES'
	c = c + 1
for i in range(len(ans)):
	print(ans[i])


def main():
	import sys
	input = sys.stdin.buffer.readline
	ans = []
	for _ in range(int(input())):
		n = int(input())
		(*a,) = map(int, input().split())
		s = set(a)
		M = 0
		for x in s:
			for y in s:
				t = [t for t in a if t == x or t == y]
				if x == y:
					tmp = len(t)
					if M < tmp:
						M = tmp
					continue
				(cx, cy) = ([0], [0])
				c = 0
				for i in t:
					if i == x:
						c += 1
						cx += (cx[-1] + 1,)
						cy += (cy[-1],)
					else:
						cx += (cx[-1],)
						cy += (cy[-1] + 1,)
				m = len(t)
				i = 0
				j = m
				for k in range(m):
					while i <= m and cx[i] < k:
						i += 1
					while j > 0 and c - cx[j - 1] < k:
						j -= 1
					if j <= i:
						break
					tmp = k * 2 + cy[j - 1] - cy[i]
					if M < tmp:
						M = tmp
		ans += (str(M),)
	print('\n'.join(ans))
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(set(a))
	c = a.copy()[::-1]
	f = 0
	for i in range(len(b)):
		if a.count(b[i]) >= 2:
			i1 = a.index(b[i])
			i2 = n - 1 - c.index(b[i])
			if i2 - i1 >= 2:
				print('YES')
				break
	else:
		print('NO')


a = int(input())
for j in range(a):
	s = int(input())
	k = list(map(int, input().strip(' ').split(' ')))
	if k == k[::-1]:
		print('YES')
	else:
		for (i, l) in enumerate(k):
			s1 = False
			try:
				if k[i + 1] != l and l in k[i + 1:]:
					s1 = True
				if k[i + 1] == l and l in k[i + 2:]:
					s1 = True
			except:
				pass
			if s1:
				print('YES')
				s1 = False
				break
			if i == len(k) - 1:
				print('NO')


t = int(input())
for i in range(0, t):
	n = input()
	lista = list(map(int, input().split()))
	tabela = []
	najdeno = False
	for i in range(len(lista)):
		if lista[i] not in tabela:
			for j in range(i + 2, len(lista)):
				if lista[j] == lista[i]:
					najdeno = True
					break
		elif najdeno:
			break
		else:
			tabela.append(lista[i])
	if najdeno:
		print('Yes')
	else:
		print('No')


for _ in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	li.append(10000000002)
	fg = 0
	for i in range(n - 2):
		if li[i] in li[i + 2:]:
			print('YES')
			fg = 1
			break
	if fg == 0:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a1 = []
	for i in range(n + 1):
		a2 = (n + 1) * [0]
		a1.append(a2)
	for i in range(1, n + 1):
		a1[a[i - 1]][i] = 1
	f = 0
	for i in range(1, n + 1):
		k = sum(a1[i])
		if k > 2:
			f = 1
			break
		elif k == 2:
			f1 = 1
			for j in range(1, n):
				if a1[i][j] == 1 and a1[i][j + 1] == 1:
					f1 = 0
					break
			if f1:
				f = 1
	if f == 1:
		print('YES')
	else:
		print('NO')


def R():
	return map(int, input().split())

def I():
	return int(input())

def S():
	return str(input())

def L():
	return list(R())
from collections import Counter
import math
import sys
from itertools import permutations
import bisect
for _ in range(I()):
	n = I()
	a = L()
	labbeg = [0] * (n + 1)
	labend = [0] * (n + 1)
	for i in range(n):
		if labbeg[a[i]] == 0:
			labbeg[a[i]] = i + 1
			labend[a[i]] = i + 1
		else:
			labend[a[i]] = i + 1
	fl = False
	for i in range(n + 1):
		if labbeg[i] > 0 and -labbeg[i] + labend[i] > 1:
			fl = True
			break
	print(['NO', 'YES'][fl])


t = int(input())
u = 0
while u < t:
	n = int(input())
	a = list(map(int, input().split()))
	rama = [[0] * n for i in range(201)]
	sita = [[] for i in range(201)]
	for i in range(n):
		if i > 0:
			s = a[i]
			for j in range(201):
				if j == s:
					rama[a[i]][i] = rama[a[i]][i - 1] + 1
					sita[a[i]].append(i)
				else:
					rama[j][i] = rama[j][i - 1]
		else:
			rama[a[i]][i] += 1
			sita[a[i]].append(i)
	result = 0
	for i in range(n):
		d = rama[a[i]][i]
		e = sita[a[i]][-d]
		if e > i:
			maxi = 0
			for j in range(201):
				maxi = max(maxi, rama[j][e - 1] - rama[j][i])
				result = max(2 * d + maxi, result)
	if result == 0:
		print('1')
	else:
		print(result)
	u += 1


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	di = {}
	count = [0] * 5001
	mx = 0
	for j in range(n):
		i = a[j]
		di[i] = [j] if i not in di else di[i] + [j]
		count[i] += 1
		mx = max(mx, count[i])
	if mx >= 3:
		print('YES')
		continue
	poss = False
	for each in di:
		if len(di[each]) == 2 and di[each][1] != di[each][0] + 1:
			poss = True
			break
	print('YES' if poss else 'NO')


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict

def L():
	return list(map(int, stdin.readline().split()))

def In():
	return map(int, stdin.readline().split())

def I():
	return int(stdin.readline())
P = 1000000007
for t in range(I()):
	n = I()
	arr = L()
	flag = 0
	m1 = max(arr)
	for i in range(n):
		if arr[i] in arr[i + 2:]:
			flag = 1
			break
	if flag:
		print('YES')
	else:
		print('NO')


from math import *
mod = 1000000007
inf = 10000000000000000

def binpow(a, b):
	global mod
	if b == 1:
		return a % mod
	u = binpow(a, b // 2)
	if b % 2 == 0:
		return u * u % mod
	else:
		return u * u * a % mod

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
temp = [0] * 500
used = [0] * 500

def bs(low, high, target):
	if low == high:
		return low
	elif low + 1 == high:
		if temp[high] > target:
			return low
		else:
			return high
	else:
		mid = (low + high) // 2
		if temp[mid] == target:
			return mid
		elif temp[mid] > target:
			return bs(low, mid - 1, target)
		else:
			return bs(mid, high, target)
for _ in range(int(input())):
	dic = {}
	n = int(input())
	arr = [int(i) for i in input().split()]
	b = False
	for i in range(len(arr)):
		if arr[i] not in dic:
			dic[arr[i]] = i
		elif dic[arr[i]] != i - 1:
			b = True
			break
	if b:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	nums = list(map(int, input().split()))
	flag = False
	for i in range(len(nums)):
		for j in range(len(nums)):
			if abs(i - j) > 1:
				if nums[i] == nums[j]:
					print('YES')
					flag = True
					break
		if flag:
			break
	if not flag:
		print('NO')


for _ in range(int(input())):
	input()
	a = map(int, input().split())
	memo = {}
	for (i, x) in enumerate(a, 2):
		if memo.setdefault(x, i) < i - 1:
			print('YES')
			break
	else:
		print('NO')


T = int(input())
for tc in range(T):
	N = int(input())
	a = list(map(int, input().split()))
	result = 'NO'
	pick = []
	exit = False
	for i in range(N - 1):
		for j in range(i + 2, N):
			if a[i] == a[j]:
				result = 'YES'
				exit = True
				break
		if exit:
			break
	print(result)


def sol(arr):
	for i in range(len(arr)):
		if arr[i] in arr[i + 2:]:
			print('YES')
			return
	print('NO')
	return
for t in range(int(input())):
	n = int(input())
	test = list(map(int, input().split()))
	sol(test)


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
import sys
from collections import defaultdict

def solve(n, A):
	if n == 1:
		return 1
	if n == 2:
		if len(set(A)) == 1:
			return 2
		return 1
	L = [[0] * 27 for _ in range(n + 1)]
	for (i, a) in enumerate(A):
		for j in range(27):
			L[i + 1][j] = L[i][j]
		L[i + 1][a] += 1
	ans = 0
	for l in range(n):
		for r in range(l, n):
			(Left, Right) = ([0] * 27, [0] * 27)
			mid = 0
			for i in range(27):
				Left[i] = L[l][i]
				mid = max(mid, L[r + 1][i] - L[l][i])
				Right[i] = L[n][i] - L[r + 1][i]
			tmp = 0
			for i in range(27):
				tmp = max(tmp, min(Left[i], Right[i]))
			ans = max(ans, mid + tmp * 2)
	return ans

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		A = tuple(map(int, input().split()))
		ans = solve(n, A)
		sys.stdout.write(str(ans) + '\n')
main()


a = int(input())
for _ in range(a):
	t1 = 0
	m = int(input())
	arry = input()
	arrym = arry.split()
	for z in range(m - 2):
		for v in range(z + 2, m):
			if arrym[z] == arrym[v]:
				t1 = 1
	if t1 == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	al = list(map(int, input().split()))
	for i in range(n):
		for j in range(i + 2, n):
			if al[i] == al[j]:
				print('YES')
				break
		else:
			continue
		break
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	dict = {}
	for (f, e) in enumerate(l):
		if e not in dict:
			dict[e] = [f]
		elif dict[e][-1] + 1 != f:
			dict[e].append(f)
	p = []
	i = 0
	for e in dict.values():
		i += 1
		if len(e) >= 2:
			print('YES')
			break
	if i == len(dict) and len(e) == 1:
		print('NO')


T = int(input())
for t in range(T):
	N = int(input())
	D = {}
	A = [int(x) for x in input().split()]
	for a in A:
		if a not in D:
			D[a] = 1
		else:
			D[a] += 1
	count = 0
	for d in D:
		if D[d] > 2:
			count = 1
			break
		elif D[d] == 2:
			if A[A.index(d) + 1] != d:
				count = 1
				break
	if count == 1:
		print('YES')
	else:
		print('NO')


import math
for _ in range(int(input())):
	n = int(input())
	arr = [int(s) for s in input().split()]
	ans = [[0 for i in range(26)] for j in range(n)]
	ansl = [[0 for i in range(26)] for j in range(n)]
	ans[0][arr[0] - 1] = 1
	ansl[n - 1][arr[n - 1] - 1] = 1
	for i in range(1, n):
		x = arr[i]
		for j in range(26):
			ans[i][j] = ans[i - 1][j]
		ans[i][x - 1] = ans[i - 1][x - 1] + 1
	for i in range(n - 2, -1, -1):
		x = arr[i]
		for j in range(26):
			ansl[i][j] = ansl[i + 1][j]
		ansl[i][x - 1] = ansl[i + 1][x - 1] + 1
	maxi = 0
	for i in range(n):
		for j in range(n - 1, i, -1):
			maxi2 = 0
			for k in range(26):
				if ans[i][k] == ansl[j][k]:
					if maxi2 < ans[i][k]:
						maxi2 = ans[i][k]
			sum = 0
			for l in range(26):
				x = ans[j - 1][l] - ans[i][l]
				if x > sum:
					sum = x
			maxi = max(maxi, 2 * maxi2 + sum)
	if maxi == 0:
		print(1)
	else:
		print(maxi)


import sys
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]
for _ in range(int(ri())):
	n = int(ri())
	a = Ri()
	count = list2d(27, len(a) + 1, 0)
	for i in range(len(a)):
		for j in range(1, 27):
			count[j][i + 1] = count[j][i]
		count[a[i]][i + 1] += 1
	ans = -1
	for i in range(len(a)):
		for j in range(i + 1, len(a)):
			if a[i] == a[j]:
				left = count[a[i]][i + 1]
				right = count[a[i]][len(a)] - count[a[i]][j]
				maxx = 0
				for k in range(1, 27):
					if k != a[i]:
						cnt = count[k][j + 1] - count[k][i]
						maxx = max(maxx, cnt)
				ans = max(ans, min(left, right) * 2 + maxx)
	for i in range(1, 27):
		ans = max(ans, count[i][n])
	print(ans)


import sys
import copy
input = sys.stdin.readline

def main():
	t = int(input())
	for _ in range(t):
		N = int(input())
		A = [int(x) for x in input().split()]
		ruiseki = [[0] * 201 for j in range(N)]
		vset = set()
		for (i, v) in enumerate(A):
			vset.add(v)
			ruiseki[i][v] += 1
			if i != 0:
				for j in range(1, 201):
					ruiseki[i][j] += ruiseki[i - 1][j]
		ans = 1
		for i in vset:
			l = 0
			r = N - 1
			tmp = 0
			while True:
				while l < N and A[l] != i:
					l += 1
				while r >= 0 and A[r] != i:
					r -= 1
				if l >= r:
					break
				tmp += 2
				ma = 0
				for j in vset:
					ma = max(ma, ruiseki[r - 1][j] - ruiseki[l][j])
				ans = max(ans, tmp + ma)
				l += 1
				r -= 1
		print(ans)
main()


for _ in range(int(input())):
	length = int(input())
	arr = [int(i) for i in input().split()]
	counts = {}
	for j in range(length):
		if arr[j] in counts:
			if j - counts[arr[j]] > 1:
				print('YES')
				break
		else:
			counts[arr[j]] = j
	else:
		print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	g = [-1] * (n + 1)
	for i in range(n):
		if g[a[i]] >= 0 and i - g[a[i]] > 1:
			print('YES')
			break
		if g[a[i]] < 0:
			g[a[i]] = i
	else:
		print('NO')


def palindrome(N, a):
	numbers = []
	for i in a:
		if i in numbers[:-1]:
			return 'YES'
		numbers.append(i)
	else:
		return 'NO'
T = int(input())
for i in range(T):
	N = int(input())
	a = list(map(int, input().split()))
	print(palindrome(N, a))


from collections import deque

def solve():
	n = int(input())
	li = [int(x) for x in input().split()]
	counts = [[0] * 27]
	pos = [[] for i in range(27)]
	for (i, a) in enumerate(li):
		counts.append(list(counts[-1]))
		counts[-1][a] += 1
		pos[a].append(i)
	mx = 0
	for (i, a) in enumerate(li):
		pre_occur_a = counts[i + 1][a]
		pos_suff_occur_a = pos[a][-pre_occur_a]
		if pos_suff_occur_a > i:
			for b in range(27):
				mx = max(mx, counts[pos_suff_occur_a][b] - counts[i + 1][b] + 2 * pre_occur_a)
		elif pos_suff_occur_a == i:
			mx = max(mx, 2 * pre_occur_a - 1)
	print(mx)
t = int(input())
for i in range(t):
	solve()


import sys
from collections import defaultdict

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
for _ in range(INT()):
	N = INT()
	A = LIST()
	D = defaultdict(int)
	for (i, a) in enumerate(A):
		if a in D:
			if i - D[a] >= 2:
				YES()
				break
		else:
			D[a] = i
	else:
		NO()


t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	store = dict()
	found = False
	for (idx, v) in enumerate(lst):
		if v not in store:
			store[v] = idx
		elif idx - store[v] > 1:
			found = True
			break
	if found:
		print('YES')
	else:
		print('NO')


t = int(input())
sol = []
while t:
	t -= 1
	n = int(input())
	a = [int(i) for i in input().split()]
	b = a[::-1]
	mat = [[0 for i in range(n + 1)] for j in range(n + 1)]
	for i in range(n):
		for j in range(n):
			if a[i] == b[j]:
				mat[i + 1][j + 1] = 1 + mat[i][j]
			else:
				mat[i + 1][j + 1] = max(mat[i + 1][j], mat[i][j + 1])
	if mat[n][n] > 2:
		sol.append('YES')
	else:
		sol.append('NO')
print(*sol, sep='\n')


import sys
from collections import *
input = lambda : sys.stdin.readline().rstrip()

def solve(n, A):
	C = [[0 for i in range(26)]]
	for i in range(n):
		C.append(C[-1].copy())
		C[-1][A[i] - 1] += 1
	res = 0
	for i in range(n):
		for j in range(i + 1, n):
			cx = 0
			for x in range(26):
				cntF = C[i + 1][x]
				cntS = C[-1][x] - C[j][x]
				if cntF == cntS:
					cx = max(cx, cntF + cntS)
			cy = 0
			for y in range(26):
				cy = max(cy, C[j][y] - C[i + 1][y])
			res = max(res, cx + cy)
	res = max(res, max(C[-1]))
	print(res)
t = int(input())
for test in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	solve(n, A)


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	ans = 'NO'
	d = {}
	for i in range(n):
		d[lst[i]] = d.get(lst[i], []) + [i]
	for k in d.keys():
		if d[k][-1] - d[k][0] >= 2:
			ans = 'YES'
			break
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	vals = list(map(int, input().split()))
	indics = {}
	found = False
	for (i, v) in enumerate(vals):
		pr_index = indics.get(v)
		if pr_index is not None and pr_index < i - 1:
			print('YES')
			found = True
			break
		if pr_index is None:
			indics[v] = i
	if found == False:
		print('NO')


import bisect
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	variety = list(set(arr))
	cnts = [[0] * (n + 1) for _ in range(201)]
	poss = [[] for _ in range(201)]
	for i in range(n):
		cnts[arr[i]][i] += 1
		poss[arr[i]].append(i)
		for j in range(201):
			cnts[j][i] += cnts[j][i - 1]
	ans = 0
	for i in range(201):
		tmp = cnts[i][n - 1]
		ans = max(ans, tmp)
	for a in variety:
		for i in range(n):
			l = poss[a][i]
			r = poss[a][-(i + 1)]
			if l >= r:
				break
			tmp = 0
			for j in range(201):
				if j == a:
					continue
				tmp = max(tmp, cnts[j][r] - cnts[j][l])
			tmp += 2 * (i + 1)
			ans = max(ans, tmp)
	print(ans)


for test in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	arr = [[0 for i in range(n + 1)] for j in range(201)]
	for i in range(n):
		arr[a[i]][i + 1] += 1
		for j in range(1, 201):
			arr[j][i + 1] += arr[j][i]
	ans = 1
	for i in range(1, 201):
		l = 0
		r = n - 1
		c = 0
		while l < r:
			while l < r and a[l] != i:
				l += 1
			while l < r and a[r] != i:
				r -= 1
			if l < r and a[l] == i and (a[r] == i):
				m = 0
				for j in range(1, 201):
					m = max(m, arr[j][r] - arr[j][l + 1])
				ans = max(c + m + 2, ans)
				c += 2
				l += 1
				r -= 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	ans = 'NO'
	a = list(map(int, input().split()))
	for i in range(len(a) - 2):
		if a[i] in a[i + 2:]:
			ans = 'YES'
			break
	print(ans)


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	l = list(map(int, sys.stdin.readline().split()))
	mapi = {}
	yes = False
	for i in range(n):
		num = l[i]
		if mapi.get(num):
			if abs(i + 1 - mapi[num]) > 1:
				yes = True
				break
		else:
			mapi[num] = i + 1
	if yes:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	temp = True
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				print('YES')
				temp = False
				break
		if not temp:
			break
	if temp:
		print('NO')


import bisect
t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	B = {}
	for i in range(n):
		try:
			B[A[i]].append(i)
		except:
			B[A[i]] = [i]
	fa = 0
	flag = 0
	for i in B.keys():
		flag = flag + 1
		lv = len(B[i])
		mv = lv
		for j in range(lv // 2):
			for k in B.keys():
				if k != i:
					c = 2 * (j + 1) + bisect.bisect(B[k], B[i][lv - j - 1]) - bisect.bisect(B[k], B[i][j])
					if mv < c:
						mv = c
		if fa < mv:
			fa = mv
	if flag == 1:
		print(n)
	else:
		print(max(fa, 1))


t = int(input())
for i in range(t):
	n = int(input())
	ls = list(map(int, input().split()))
	flag = 0
	j = n - 1
	d = {}
	for ii in range(n):
		if ls[ii] not in d:
			d[ls[ii]] = [ii]
		else:
			d[ls[ii]] = d[ls[ii]] + [ii]
	for ii in ls:
		if len(d[ii]) > 2:
			flag = 1
			break
		elif len(d[ii]) == 2:
			if d[ii][1] - d[ii][0] > 1:
				flag = 1
				break
	if flag == 1:
		print('YES')
	else:
		print('NO')


from collections import Counter
import sys
readline = sys.stdin.readline
for _ in range(int(input())):
	n = int(readline())
	a = list(map(int, readline().split()))
	cnt = Counter(a)
	ans = 0
	for x in range(1, 27):
		ans = max(ans, cnt[x])
		for y in range(1, 27):
			y_cnt = cnt[y]
			(l, r) = (-1, n)
			for i in range(1, 2000):
				(l, r) = (l + 1, r - 1)
				while l < r:
					if a[l] == y:
						y_cnt -= 1
					if a[l] == x:
						break
					l += 1
				while l < r:
					if a[r] == y:
						y_cnt -= 1
					if a[r] == x:
						break
					r -= 1
				if not l < r:
					break
				if ans < i * 2 + y_cnt:
					ans = i * 2 + y_cnt
	print(ans)


t = int(input())
while t:
	n = int(input())
	li = list(map(int, input().split()))
	for i in range(1, n - 1):
		if set(li[0:i]).intersection(set(li[i + 1:])):
			print('YES')
			break
	else:
		print('NO')
	t -= 1


def lps(str):
	n = len(str)
	L = [[0 for x in range(n)] for x in range(n)]
	for i in range(n):
		L[i][i] = 1
	for cl in range(2, n + 1):
		for i in range(n - cl + 1):
			j = i + cl - 1
			if str[i] == str[j] and cl == 2:
				L[i][j] = 2
			elif str[i] == str[j]:
				L[i][j] = L[i + 1][j - 1] + 2
			else:
				L[i][j] = max(L[i][j - 1], L[i + 1][j])
	return L[0][n - 1]
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	arr = list(map(int, input().strip().split(' ')))
	ans = lps(arr)
	if ans >= 3:
		print('YES')
	else:
		print('NO')


n = int(input())
for x in range(n):
	m = int(input())
	t = list(map(int, input().split()))
	d = {}
	b = False
	for y in range(0, m):
		if t[y] not in d.keys():
			d[t[y]] = [y]
	for y in range(m - 1, -1, -1):
		if len(d[t[y]]) == 1:
			d[t[y]].append(y)
	for y in d.values():
		if y[1] - y[0] >= 2:
			print('YES')
			b = True
			break
	if b == False:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(j) for j in input().split()]
	pre = [[0 for j in range(n + 1)] for k in range(26)]
	for j in range(1, n + 1):
		num = a[j - 1]
		for i in range(1, 27):
			pre[i - 1][j] = pre[i - 1][j - 1]
			if num == i:
				pre[i - 1][j] += 1
	ans = 0
	for j in range(26):
		ans = max(ans, pre[j][-1])
	for left in range(n):
		for right in range(left, n):
			middle = 0
			outer = 0
			for j in range(26):
				middle = max(middle, pre[j][right + 1] - pre[j][left])
				outer = max(outer, min(pre[j][left], pre[j][n] - pre[j][right + 1]) * 2)
			ans = max(ans, middle + outer)
	print(ans)


import sys
input = sys.stdin.readline

def calc(arr, a, b):
	n = len(arr)
	if arr.count(a) <= 1:
		return arr.count(b)
	maxlen = 0
	indices = [i for (i, x) in enumerate(arr) if x == a]
	for i in range(len(indices) // 2):
		idx1 = indices[i]
		idx2 = indices[-i - 1]
		temp = arr[idx1 + 1:idx2]
		maxlen = max(maxlen, (i + 1) * 2 + temp.count(b))
	return maxlen
t = int(input())
for j in range(t):
	n = int(input())
	arr = input().strip().split()
	poss = set(arr)
	maxlen = 0
	for i in poss:
		for j in poss:
			maxlen = max(maxlen, calc(arr, i, j))
	print(maxlen)


t = int(input())
for x in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	for i in range(0, n - 2):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				ans = 1
	if ans == 1:
		print('YES')
	else:
		print('NO')


def p(d):
	for v in d.values():
		if v[-1] - v[0] > 1:
			return 'YES'
	return 'NO'
for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	d = dict()
	for i in range(n):
		d[arr[i]] = []
	for i in range(n):
		d[arr[i]].append(i)
	print(p(d))


def findpalin(n, a):
	palin = False
	for i in range(n):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				palin = True
	return 'YES' if palin else 'NO'
t = int(input())
for z in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print(findpalin(n, a))


from sys import stdin, stdout
import math
from collections import defaultdict

def lps(str):
	n = len(str)
	L = [[0 for x in range(n)] for x in range(n)]
	for i in range(n):
		L[i][i] = 1
	for cl in range(2, n + 1):
		for i in range(n - cl + 1):
			j = i + cl - 1
			if str[i] == str[j] and cl == 2:
				L[i][j] = 2
			elif str[i] == str[j]:
				L[i][j] = L[i + 1][j - 1] + 2
			else:
				L[i][j] = max(L[i][j - 1], L[i + 1][j])
	return L[0][n - 1]
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if lps(a) < 3:
		print('NO')
	else:
		print('YES')


t = int(input())
for w in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	c = 0
	for i in range(n):
		if l[i] in l[i + 2:]:
			print('YES')
			c = 1
			break
	if c == 0:
		print('NO')


import sys
input = sys.stdin.readline
from bisect import bisect_left
Q = int(input())
Query = []
for _ in range(Q):
	N = int(input())
	A = list(map(int, input().split()))
	Query.append((N, A))
for (N, A) in Query:
	dic = {}
	for (i, a) in enumerate(A):
		if a in dic:
			dic[a].append(i)
		else:
			dic[a] = [i]
	Ns = list(dic.keys())
	ans = 0
	for (a, A1) in dic.items():
		ans = max(ans, len(A1))
		for (b, B1) in dic.items():
			if a == b:
				continue
			l1 = 0
			r1 = len(A1) - 1
			l2 = 0
			r2 = len(B1) - 1
			while l1 < r1:
				while l2 <= len(B1) - 1 and B1[l2] < A1[l1]:
					l2 += 1
				while r2 >= 0 and B1[r2] > A1[r1]:
					r2 -= 1
				t = (l1 + 1) * 2 + (r2 - l2 + 1)
				if t > ans:
					ans = t
				l1 += 1
				r1 -= 1
	print(ans)


import sys

def readInt():
	return int(rl())

def readList():
	return list(map(int, rl().strip().split()))

def readStr():
	return rl().strip()

def makeMatrix(a, b, value):
	z = [value] * a
	for i in range(a):
		z[i] = [value] * b
	return z
rl = input
tc = readInt()
while tc:
	tc -= 1
	n = readInt()
	a = readList()
	ok = False
	for i in range(n):
		for j in range(i + 2, n):
			if a[j] == a[i]:
				ok = True
		if ok:
			break
	if ok:
		print('YES')
	else:
		print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	num = list(map(int, input().split()))
	flag = 0
	for i in range(n // 2):
		if num[i] != num[n - i - 1]:
			flag = 1
			break
	if flag == 0:
		print('YES')
		continue
	elif flag == 1:
		flag2 = 0
		for i in range(n):
			for j in range(i + 2, n):
				if num[i] == num[j]:
					flag2 = 1
					break
			if flag2 == 1:
				break
		if flag2 == 1:
			print('YES')
		elif flag2 == 0:
			print('NO')


t = int(input())
for T in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	c = 0
	for i in range(n):
		val = l[i]
		for j in range(i + 2, n):
			if l[j] == val:
				c = 1
				break
		if c == 1:
			break
	if c == 1:
		print('YES')
	else:
		print('NO')


def q():
	b = eval(input())
	c = list(map(int, input().strip().split()))
	for i in range(len(c)):
		for j in range(i + 1, len(c)):
			if c[i] == c[j] and j - i > 1:
				print('YES')
				return 0
	print('NO')
	return 0
a = eval(input())
for i in range(a):
	q()


for t in range(int(input())):
	n = int(input())
	v = list(map(int, input().split()))
	l = []
	vis = []
	f = 0
	for i in range(n + 1):
		vis.append(0)
		l.append(0)
	for i in range(n):
		if vis[v[i]] == 1:
			if i - l[v[i]] > 1:
				f = 1
			continue
		vis[v[i]] = 1
		l[v[i]] = i
	if f == 0:
		print('NO')
	else:
		print('YES')


from sys import stdin, stdout
import traceback
import math
import bisect
from typing import List, Tuple
import itertools as it
from collections import Counter, defaultdict
import random
read = lambda : stdin.readline().strip()
write = stdout.write
writeln = lambda x: write(str(x) + '\n')

def reads(typ=int):
	return list(map(typ, read().split()))

def enumerate_sorted(iterable):
	return sorted(zip(it.count(), iterable), key=lambda x: x[1])

def is_3_palindrome(aa):
	aa = list(aa)
	if len(aa) == 0:
		return True
	counter = Counter(aa)
	if len(counter.keys()) > 2:
		return False
	a = aa[0]
	cnt = (counter[a] + 1) // 2
	if aa[:cnt] != [a] * cnt or aa[-cnt:] != [a] * cnt:
		return False
	return True

class Sol:

	def __init__(self, n, arr):
		self.n = len(arr)
		self.arr = arr

	@classmethod
	def input(cls):
		n = int(read())
		aa = reads()
		return cls(n, aa)

	def solve(self):
		res = 0
		counter = Counter(self.arr)
		indice = defaultdict(list)
		for (i, x) in enumerate(self.arr):
			indice[x].append(i)
		res = max((len(indice[x]) for x in indice))
		for a in indice:
			indice_a = indice[a]
			for x in range(1, counter[a] // 2 + 1):
				(left, right) = (indice_a[x - 1], indice_a[-x])
				for b in indice:
					indice_b = indice[b]
					if b == a:
						continue
					l1 = bisect.bisect_left(indice_b, left)
					r1 = bisect.bisect_right(indice_b, right)
					y = r1 - l1
					tmp = 2 * x + y
					if tmp > res:
						res = tmp
		return res

def mytest():
	import tqdm
	for _ in tqdm.tqdm(range(1000)):
		n = 10
		arr = [random.randint(1, 3) for _ in range(n)]
		print(arr)
		assert Sol(0, arr).solve() == solve_brute(arr)

def powerset(iterable):
	s = list(iterable)
	return it.chain.from_iterable((it.combinations(s, r) for r in range(len(s) + 1)))

def solve_brute(aa):
	res = 0
	for ar in powerset(aa):
		if is_3_palindrome(ar) and len(ar) > res:
			res = len(ar)
	return res

def main():
	try:
		t = int(read())
		for _ in range(t):
			res = Sol.input().solve()
			writeln(res)
	except Exception as e:
		print('Got exception:', repr(e))
		print(traceback.format_exc())
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(pref, sums, res) = ([[0] for _ in range(27)], [0] * 27, 0)
	for el in a:
		sums[el] += 1
		for j in range(1, 27):
			pref[j].append(sums[j])
	for l in range(n):
		for r in range(l + 1, n + 1):
			(pre1, pre2) = (0, 0)
			for j in range(1, 27):
				mid = pref[j][r] - pref[j][l]
				bor = 2 * min(pref[j][l], pref[j][n] - pref[j][r])
				pre1 = mid if mid > pre1 else pre1
				pre2 = bor if bor > pre2 else pre2
			res = pre1 + pre2 if pre1 + pre2 > res else res
	print(res)


from collections import Counter
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
from functools import reduce

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())
mod = int(1000000000.0) + 7

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
for _ in range(iinput()):
	n = iinput()
	a = rlinput()
	d = defaultdict(list)
	j = 0
	for i in a:
		d[i].append(j)
		j += 1
	flag = False
	for i in d:
		if len(d[i]) >= 2 and d[i][-1] - d[i][0] > 1:
			flag = True
			break
	print('YES' if flag else 'NO')


import sys
for _ in range(int(sys.stdin.readline().rstrip())):
	n = int(sys.stdin.readline().rstrip())
	num = list(map(int, sys.stdin.readline().rstrip().split()))
	re = False
	for i in range(len(num)):
		for j in range(i + 2, len(num)):
			if num[i] == num[j]:
				re = True
				break
		if re:
			break
	if re:
		print('YES')
	else:
		print('NO')


n = int(input())
for i in range(n):
	_ = input()
	ls = list(map(int, input().split()))
	check = False
	for i in range(len(ls) - 2):
		if ls[i] in ls[i + 2:]:
			check = True
			break
	if check:
		print('YES')
	else:
		print('NO')


for tin in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = list(set(a))
	b = []
	for _ in range(len(s)):
		c = []
		for i in range(len(a)):
			if a[i] == s[_]:
				c.append(i)
		b.append(c)
	k = 0
	for _ in range(len(b)):
		if len(b[_]) > 1 and abs(b[_][0] - b[_][-1]) >= 2:
			print('YES')
			k = 1
			break
	if k == 0:
		print('NO')


t = int(input())
for p in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	d = dict()
	for i in range(n):
		if arr[i] not in d:
			d[arr[i]] = 0
		d[arr[i]] += 1
	Max = 0
	answer = 0
	for i in d.keys():
		if d[i] > Max:
			Max = d[i]
		local = 0
		l = 0
		r = n - 1
		for j in range(d[i] // 2):
			q = dict()
			q[i] = 0
			while arr[l] != i and l < r:
				l += 1
			while arr[r] != i and l < r:
				r -= 1
			for o in range(l + 1, r):
				if arr[o] != i:
					if arr[o] not in q:
						q[arr[o]] = 0
					q[arr[o]] += 1
			local = max(local, max(q.values()) + (j + 1) * 2)
			l += 1
			r -= 1
		if local > answer:
			answer = local
	print(max(Max, answer))


def distribution(n):
	return (n - 1) // 2

def problem_A():
	print(distribution(int(input())))
s = 'abcdefghijklmnopqrstuvwxyz'

def cons_str(n, b):
	a = s[:b]
	ans = ''
	while n > b:
		ans += a
		n -= b
	ans += a[:n]
	return ans

def problem_B():
	(n, a, b) = list(map(int, input().split()))
	print(cons_str(n, b))

def comp_teams(a):
	if len(a) < 2:
		return 0
	s = len(set(a))
	if s == len(a):
		return 1
	a.sort()
	t = 0
	temp = 1
	for i in range(1, len(a)):
		if a[i] == a[i - 1]:
			temp += 1
		else:
			if t < temp:
				t = temp
			temp = 1
	if t < temp:
		t = temp
	if s <= t - 1:
		return s
	if s == t:
		return s - 1
	else:
		return t

def problem_C():
	l = int(input())
	a = list(map(int, input().split()))
	print(comp_teams(a))

def antisudoku(a):
	for i in range(9):
		for j in range(9):
			if a[i][j] == '2':
				a[i][j] = '1'
	return a

def problem_D():
	a = []
	for _ in range(9):
		a.append(list(input()))
	b = antisudoku(a)
	for i in range(9):
		print(''.join(b[i]))

def three_block_palindrome(s):
	a = []
	b = []
	c = []
	l = len(s)
	for _ in range(l):
		a = []
		for _ in range(l + 1):
			a.append([0] * 26)
	l += 1
	for i in range(1, l):
		k = s[i - 1] - 1
		for j in range(26):
			a[i][j] = a[i - 1][j]
		a[i][k] += 1
	maxlength = 0
	for i in range(l):
		for j in range(i, l):
			maxa = 0
			maxb = 0
			for k in range(26):
				if a[l - 1][k] - a[j][k] > a[i][k]:
					if maxa < a[i][k]:
						maxa = a[i][k]
				elif maxa < a[l - 1][k] - a[j][k]:
					maxa = a[l - 1][k] - a[j][k]
				if maxb < a[j][k] - a[i][k]:
					maxb = a[j][k] - a[i][k]
			if maxlength < 2 * maxa + maxb:
				maxlength = 2 * maxa + maxb
	return maxlength

def problem_E1():
	l = int(input())
	s = list(map(int, input().split()))
	print(three_block_palindrome(s))
cases = int(input())
for _ in range(cases):
	problem_E1()


from sys import stdin
input = stdin.buffer.readline
for _ in range(int(input())):
	n = int(input())
	(*a,) = map(int, input().split())
	d = {}
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = i
		elif d[a[i]] + 1 < i:
			print('YES')
			break
	else:
		print('NO')


from sys import stdin

def solve(n, a):
	d = {}
	for i in range(n):
		if a[i] in d:
			if d[a[i]] == 2:
				return 'YES'
			if a[i - 1] != a[i]:
				return 'YES'
			d[a[i]] += 1
		else:
			d[a[i]] = 1
	return 'NO'

def main():
	inp = stdin.readline
	for _ in range(int(inp())):
		n = int(inp())
		a = list(map(int, inp().split()))
		ans = solve(n, a)
		print(ans)
main()


t = int(input())
while t != 0:
	n = int(input())
	l = list(map(int, input().split()))
	flag = 1
	for i in range(n):
		for j in range(i + 2, n):
			if l[i] == l[j]:
				flag = 0
	if flag == 0:
		print('YES')
	else:
		print('NO')
	t -= 1


import sys
import math
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
n = inp()
for i in range(n):
	t = inp()
	q = inlt()
	rej = []
	check = False
	for ind in range(t):
		if not q[ind] in rej:
			for ind2 in range(ind + 2, t):
				if q[ind2] == q[ind]:
					check = True
					break
	if check:
		print('YES')
	else:
		print('NO')


def sm(c, l, r):
	return occur[c][r + 1] - occur[c][l]
t = int(input())
for z in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	brr = list(set(arr))
	pos = [[] for i in range(201)]
	occur = [[] for i in range(201)]
	for num in brr:
		occur[num].append(0)
		count = 0
		for i in range(n):
			if arr[i] == num:
				pos[num].append(i)
				count = count + 1
			occur[num].append(count)
	ans = 0
	for num in brr:
		ans = max(ans, occur[num][n])
		ln = len(pos[num])
		for k in range(ln // 2):
			l = pos[num][k] + 1
			r = pos[num][ln - k - 1] - 1
			cntin = 0
			for nm in brr:
				cntin = max(cntin, sm(nm, l, r))
			ans = max(ans, 2 * (k + 1) + cntin)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	seen = {}
	flag = False
	for i in range(n):
		if arr[i] in seen and i - seen[arr[i]] > 1:
			print('YES')
			flag = True
			break
		elif arr[i] not in seen:
			seen[arr[i]] = i
	if not flag:
		print('NO')


def r(a, b):
	n = len(a)
	s = len(a) // 2
	j = s - 1
	v = -1
	while j >= 0:
		x = a[j]
		y = a[n - 1 - j]
		c = 0
		for p in range(len(b)):
			if b[p] > x and b[p] < y:
				c += 1
		s = 2 * (j + 1) + c
		if s > v:
			v = s
		j += -1
	return v
t = int(input())
for i in range(t):
	n = int(input())
	b = list(map(int, input().split()))
	a = []
	c = dict()
	for j in range(1, 201):
		c[j] = 0
	for j in range(202):
		a.append([])
	for j in range(n):
		a[b[j]].append(j)
		c[b[j]] = 1
	m = -1
	d = []
	for j in range(1, 201):
		if c[j] == 1:
			d.append(j)
	for j in d:
		for p in d:
			if p == j:
				h = len(a[p])
			else:
				h = r(a[j], a[p])
			if h > m:
				m = h
	print(m)


numTests = int(input())
for _ in range(numTests):
	lenA = int(input())
	A = [int(i) for i in input().split()]
	if lenA < 3:
		print('NO')
		continue
	setA = set(A)
	if len(setA) == lenA:
		print('NO')
		continue
	dupes = {}
	found = 0
	for ii in range(lenA - 2):
		if A[ii] in A[ii + 2:]:
			print('YES')
			found = 1
			break
	if not found:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	array = list(map(int, input().split()))
	dick = {}
	for i in range(n):
		dick[array[i]] = dick.get(array[i], []) + [i]
	flag = False
	for i in dick:
		this = dick[i]
		if this[-1] - this[0] >= 2:
			flag = True
			break
	if flag:
		print('YES')
	else:
		print('NO')


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def get_str():
	return input().decode().strip()

def rint():
	return map(int, input().split())

def oint():
	return int(input())
t = oint()
for _ in range(t):
	n = oint()
	a = list(rint())
	p = [[] for __ in range(n + 1)]
	for i in range(n):
		p[a[i]].append(i)
		if len(p[a[i]]) >= 2 and p[a[i]][-1] - p[a[i]][0] >= 2:
			print('YES')
			break
	else:
		print('NO')


t = int(input())
for x in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	count = [0] * (n + 1)
	f = False
	for i in range(n):
		count[l[i]] += 1
		if count[l[i]] == 2:
			if l[i - 1] != l[i]:
				f = True
				break
		elif count[l[i]] > 2:
			f = True
			break
	if f:
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline
a = int(input())
for i in range(a):
	s = int(input())
	flag = 0
	z = list(map(int, input().split()))
	for i in range(len(z)):
		r = z.count(z[i])
		if r > 2:
			print('YES')
			flag = 1
			break
		elif r == 2:
			if i > 0 and i < len(z) - 1:
				if z[i + 1] != z[i] and z[i - 1] != z[i]:
					print('YES')
					flag = 1
					break
			elif i == 0:
				if z[1] != z[0]:
					print('YES')
					flag = 1
					break
			elif z[-2] != z[-1]:
				print('YES')
				flag = 1
				break
	if flag == 0:
		print('NO')


import sys
input = sys.stdin.readline

def getInt():
	return int(input())

def getVars():
	return map(int, input().split())

def getList():
	return list(map(int, input().split()))

def getStr():
	return input().strip()
t = getInt()
for _ in range(t):
	n = getInt()
	a = getList()
	d = {}
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = []
		d[a[i]].append(i)
	keys = list(d.keys())
	res = 0
	for x in keys:
		res = max(res, len(d[x]))
		for y in keys:
			if x == y:
				continue
			res1 = 0
			for i in range(len(d[x]) // 2):
				res1 += 2
				res11 = 0
				for py in d[y]:
					if py > d[x][i] and py < d[x][-(i + 1)]:
						res11 += 1
				res = max(res, res1 + res11)
	print(res)


ans = []
t = int(input())
for kek in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	flag = False
	for i in range(n):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				flag = True
	if flag:
		ans.append('Yes')
	else:
		ans.append('No')
print(*ans, sep='\n')


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(j) for j in input().split()[:n]]
	f = 0
	for i in range(n):
		a.append(i)
	x = []
	v = []
	for k in range(n):
		if a[k] not in x:
			x.append(a[k])
		else:
			l = k
			c = a.index(a[k])
			if l - c > 1:
				f += 1
				break
			else:
				pass
	if f > 0:
		if l - c > 1:
			print('YES')
	else:
		print('NO')


k = int(input())
for i in range(k):
	n = int(input())
	k = list(map(int, input().split()))
	kp = [k.count(i) for i in k]
	ch = [3 <= i for i in kp]
	ch1 = [i == 2 for i in kp]
	if any(ch):
		print('YES')
	elif any(ch1):
		temp = 1
		t = 0
		for i in ch1:
			if i:
				if t == n - 1:
					if k[t] == k[t - 1]:
						temp += 1
					else:
						temp = 0
				elif t == 0:
					if k[t] == k[t + 1]:
						temp += 1
					else:
						temp = 0
				elif k[t] == k[t + 1] or k[t] == k[t - 1]:
					temp += 1
				else:
					temp = 0
					break
			t += 1
		if temp == 0:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


import math
import collections
import functools
import sys
import os

def solve():
	_ = input()
	table = list(map(int, input().split()))
	dic = {}
	for i in range(len(table)):
		if table[i] in dic:
			if i - dic[table[i]] > 1:
				print('YES')
				return
		else:
			dic[table[i]] = i
	print('NO')

def main():
	T = 1
	T = int(input())
	while T:
		solve()
		T -= 1
main()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = dict()
	k = dict()
	flag = 0
	for i in range(n):
		if l[i] not in d:
			d[l[i]] = i
	for i in range(n):
		k[l[i]] = i
	for i in l:
		if k[i] - d[i] > 1:
			flag = 1
			break
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	t = a
	f = 0
	i = 0
	while i < n:
		x = a[i]
		if a.count(x) >= 2:
			p1 = a.index(x)
			a.reverse()
			p2 = a.index(x)
			p2 = n - 1 - p2
			a.reverse()
			if p2 - p1 > 1:
				print('YES')
				f = 1
				break
			else:
				i = p2 + 1
		else:
			i = i + 1
	if f == 0:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	c = 0
	for i in range(n):
		if l.count(l[i]) > 2:
			print('YES')
			c = 1
			break
		else:
			x = l[i + 1:]
			if x.count(l[i]) != 0:
				if x.index(l[i]) != 0:
					print('YES')
					c = 1
					break
	if c == 0:
		print('NO')


def formula(inp):
	for i in range(len(inp) - 2):
		for j in range(i + 2, len(inp)):
			if inp[i] == inp[j]:
				return 'YES'
	return 'NO'
t = int(input())
for i in range(t):
	n = int(input())
	inp = list(map(int, input().split()))
	print(formula(inp))


def myfun(inp):
	for i in range(len(inp) - 2):
		for j in range(i + 2, len(inp)):
			if a[i] == a[j]:
				return 'Yes'
	return 'No'
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print(myfun(a))


def count():
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				return 'YES'
	return 'NO'
t = int(input())
for i in range(t):
	print(count())


def max_pos(ar, a, b, counts):
	ans = 0
	i = 0
	j = len(ar) - 1
	alen = 0
	while i < j:
		if ar[i] == a:
			while i < j and ar[j] != a:
				j -= 1
			j -= 1
			if j >= i:
				alen += 2
			curr_ans = alen + counts[b][j] - counts[b][i]
			ans = max(ans, curr_ans)
		i += 1
	return ans

def solve(ar):
	counts = [[0] * len(ar) for i in range(202)]
	counts[ar[0]][0] = 1
	for (i, val) in enumerate(ar[1:], 1):
		for j in range(202):
			if j != val:
				counts[j][i] = counts[j][i - 1]
			else:
				counts[j][i] = counts[j][i - 1] + 1
	ans = 0
	for i in range(1, 201):
		for j in range(1, 201):
			if counts[i][-1] and counts[j][-1] and (counts[i][-1] + counts[j][-1] > ans):
				ans = max(ans, max_pos(ar, i, j, counts))
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	ar = list(map(int, input().strip().split()))
	if len(ar) == 1:
		print(1)
	else:
		print(solve(ar))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	result = ''
	for i in range(1, n - 1):
		if set(a[:i]) & set(a[i + 1:]):
			result = 'YES'
			break
	if result:
		print(result)
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	orig_arr = list(map(int, input().split()))
	hash_table = {}
	palin = False
	for j in range(n):
		if orig_arr[j] not in hash_table:
			hash_table[orig_arr[j]] = j
		elif j - hash_table[orig_arr[j]] > 1:
			palin = True
			break
		else:
			continue
	if not palin:
		print('NO')
	else:
		print('YES')


from itertools import groupby
for _ in range(int(input())):
	n = int(input())
	a = [int(_) for _ in input().split()]
	seen = set()
	l = [(k, len(list(v))) for (k, v) in groupby(a)]
	for (k, v) in l:
		if k in seen or v >= 3:
			print('YES')
			break
		seen.add(k)
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	if l == l[::-1]:
		print('YES')
		continue
	newl = l.copy()
	for i in range(n - 1):
		if l[i] == l[i + 1]:
			newl.remove(l[i])
	ans = False
	if len(newl) == len(set(newl)):
		ans = False
	else:
		ans = True
	for i in range(n - 2):
		if l[i] == l[i + 2]:
			ans = True
			break
	for i in range(n - 3):
		if l[i] == l[i + 3] and l[i + 1] == l[i + 2]:
			ans = True
			break
	if ans:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) - 1 for x in input().split()]
	left_counters = [[] for _ in range(len(arr))]
	right_counters = [[] for _ in range(len(arr))]
	counter = [0 for _ in range(26)]
	for idx in range(len(arr)):
		counter[arr[idx]] += 1
		left_counters[idx] = counter.copy()
	counter = [0 for _ in range(26)]
	for idx in range(len(arr) - 1, -1, -1):
		counter[arr[idx]] += 1
		right_counters[idx] = counter.copy()
	ans = 1
	for i in range(len(arr)):
		for j in range(i + 1, len(arr)):
			left = left_counters[i]
			right = right_counters[j]
			mid = list()
			mid_score = 0
			for idx in range(26):
				mid.append(left_counters[j - 1][idx] - left_counters[i][idx])
				mid_score = max(mid_score, mid[-1])
			for value in range(26):
				ans = max(ans, mid_score + min(left[value], right[value]) * 2)
	print(ans)


t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = [int(x) for x in input().split()]
	listoflist = [[] for i in range(5002)]
	for i in range(0, n):
		listoflist[arr[i]].append(i)
	flag = False
	for i in range(5002):
		if len(listoflist[i]) >= 3:
			flag = True
		elif len(listoflist[i]) >= 2:
			if listoflist[i][0] + 1 < listoflist[i][1]:
				flag = True
	print('YES' if flag else 'NO')


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	i1 = 0
	i2 = n
	flag = False
	for i in range(n - 2):
		e1 = a[i]
		for j in range(i + 2, n):
			if e1 == a[j]:
				print('YES')
				flag = True
				break
		if flag == True:
			break
	if flag == False:
		print('NO')
for i in range(int(input())):
	solve()


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	d = dict()
	l = int(input())
	a = list(map(int, input().split()))
	z = set([])
	ok = 0
	for i in range(1, l):
		if a[i] in z:
			print('Yes')
			ok = 1
			break
		if a[i - 1] != a[i]:
			z.add(a[i - 1])
	for i in range(l):
		if a[i] not in d:
			d[a[i]] = 1
		else:
			d[a[i]] += 1
	if ok == 0:
		for key in d:
			if d[key] > 2:
				print('Yes')
				ok = 1
				break
	if ok == 0:
		print('No')


t = int(input())
for _ in range(t):
	d = {}
	n = int(input())
	arr = list(map(int, input().split()))
	found = False
	for (i, a) in enumerate(arr):
		if a not in d:
			d[a] = i
		elif i - d[a] > 1:
			print('YES')
			break
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	flag = 0
	for i in range(n):
		if a[i] in d:
			if i - d[a[i]] > 1:
				flag = 1
				break
		else:
			d[a[i]] = i
	if flag == 1:
		print('YES')
	else:
		print('NO')


from collections import Counter, defaultdict
import sys
input = sys.stdin.buffer.readline
INF = 2 ** 62 - 1

def read_int():
	return int(input())

def read_int_n():
	return list(map(int, input().split()))

def slv(N, A):
	idx = defaultdict(list)
	s = [[0] * (N + 1) for _ in range(201)]
	for i in range(201):
		for (j, a) in enumerate(A):
			if a == i:
				idx[a].append(j)
				c = 1
			else:
				c = 0
			s[i][j + 1] = s[i][j] + c
	ans = 0
	ci = [0] * 201
	for i in A:
		ans = max(ans, len(idx[i]))
		k = ci[i]
		ci[i] += 1
		li = idx[i][k]
		ri = idx[i][-k - 1]
		if k >= len(idx[i]) // 2:
			continue
		for j in range(201):
			if i == j:
				continue
			ans = max(ans, 2 * (k + 1) + s[j][ri] - s[j][li])
	return ans

def main():
	T = read_int()
	for _ in range(T):
		N = read_int()
		A = read_int_n()
		print(slv(N, A))
main()


import os
import sys
from io import BytesIO, IOBase
import heapq as h
from types import GeneratorType
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import time
start_time = time.time()
import collections as col
import math, string
from functools import reduce

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())

def solve():
	N = getInt()
	A = getInts()
	A = [A[i] - 1 for i in range(N)]
	dp = [[0 for j in range(201)] for i in range(N + 1)]
	pos = [[] for j in range(201)]
	for i in range(N):
		for j in range(201):
			dp[i + 1][j] = dp[i][j]
		dp[i + 1][A[i]] += 1
		pos[A[i]].append(i)
	ans = 0
	for i in range(201):
		ans = max(ans, len(pos[i]))
		for j in range(len(pos[i]) // 2):
			l = pos[i][j] + 1
			r = pos[i][len(pos[i]) - j - 1] - 1
			for k in range(201):
				x = dp[r + 1][k] - dp[l][k]
				ans = max(ans, (j + 1) * 2 + x)
	return ans
for _ in range(getInt()):
	print(solve())


t = int(input())
for i in range(0, t):
	n = int(input())
	a = [int(x) for x in input().strip().split()]
	cnt = [0] * 5001
	chk = False
	for j in range(0, len(a)):
		cnt[a[j]] += 1
		if cnt[a[j]] >= 3:
			chk = True
			break
		elif cnt[a[j]] >= 2:
			if a[j] != a[j - 1]:
				chk = True
				break
	if chk:
		print('YES')
	elif not chk:
		print('NO')


t = int(input())
ans = []
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	sort = [0] * 5001
	for i in a:
		sort[i] += 1
	for i in range(5001):
		if sort[i] < 2:
			continue
		elif sort[i] > 2:
			ans.append('YES')
			break
		else:
			index = a.index(i)
			if index - 1 >= 0 and a[index - 1] == i:
				continue
			if index + 1 < n and a[index + 1] == i:
				continue
			ans.append('YES')
			break
	else:
		ans.append('NO')
print('\n'.join((i for i in ans)))


t = int(input())
l1 = []
for i in range(t):
	l = []
	n = int(input())
	s = list(map(int, input().split()))
	for j in range(n):
		l.append([s[j], j])
	l1.append(l)
for k in l1:
	k.sort()
	for m in range(1, len(k)):
		if m <= len(k) - 2:
			if k[m - 1][0] == k[m][0] == k[m + 1][0]:
				print('YES')
				break
			elif k[m - 1][0] == k[m][0] and k[m][1] - k[m - 1][1] > 1:
				print('YES')
				break
			elif m == len(k) - 1:
				print('NO')
		elif k[m - 1][0] == k[m][0] and k[m][1] - k[m - 1][1] > 1:
			print('YES')
			break
		elif m == len(k) - 1:
			print('NO')


import sys, os
from collections import Counter
import heapq
import math
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import os.path
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def listt():
	return [int(i) for i in input().split()]
from collections import defaultdict
t = 1
t = int(input())
for _ in range(t):
	n = int(input())
	l = listt()
	d = {}
	if n < 3:
		print('NO')
	else:
		ans = True
		for i in range(n):
			if l[i] in d:
				d[l[i]].append(i + 1)
			else:
				d[l[i]] = [i + 1]
		for i in d.values():
			if len(i) == 2:
				if i[1] - i[0] > 1:
					print('YES')
					ans = False
			elif len(i) > 2:
				print('YES')
				ans = False
			if ans == False:
				break
		if ans:
			print('NO')


import sys

def p(n, a):
	last = [[-1] * (n + 1) for _ in range(n)]
	last[0][a[0]] = 0
	for i in range(1, n):
		for j in range(1, n + 1):
			last[i][j] = last[i - 1][j]
		last[i][a[i]] = i
	dp = [[0] * n for _ in range(n)]
	dp[n - 1][n - 1] = 1
	for i in range(n - 2, -1, -1):
		dp[i][i] = 1
		for j in range(i + 1, n):
			dp[i][j] = dp[i + 1][j]
			k = last[j][a[i]]
			if k > i:
				dp[i][j] = max(dp[i][j], dp[i + 1][k - 1] + 2)
	return dp[0][n - 1]
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, sys.stdin.readline().split()))
	if p(n, a) >= 3:
		print('YES')
	else:
		print('NO')


t = int(input())
ans = []
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	sl = [[] for i in range(max(a))]
	for i in range(len(a)):
		if sl[a[i] - 1] != [] and i - sl[a[i] - 1][-1] >= 2 or len(sl[a[i] - 1]) == 2:
			ans.append('YES')
			break
		else:
			sl[a[i] - 1].append(i)
	if len(ans) != _ + 1:
		ans.append('NO')
for i in ans:
	print(i)


T = int(input())
for case in range(T):
	rows = int(input())
	state = [int(x) for x in input().split()]
	nums_ = {}
	flag = False
	for (i, s) in enumerate(state):
		if s in nums_:
			if i - nums_[s] >= 2:
				print('YES')
				flag = True
				break
		else:
			nums_[s] = i
	if not flag:
		print('NO')


import sys
input = sys.stdin.buffer.readline

def list_input():
	return list(map(int, input().split()))

def mult_input():
	return map(int, input().split())
for nt in range(int(input())):
	n = int(input())
	l = list_input()
	if n == 1:
		print(1)
		continue
	elif n == 2:
		if l[0] == l[1]:
			print(2)
		else:
			print(1)
		continue
	if len(set(l)) == n:
		print(1)
		continue
	counts = [[0 for i in range(n)] for j in range(200)]
	d = {}
	for i in range(1, 201):
		count = 0
		d[i] = {}
		for j in range(n):
			if l[j] == i:
				count += 1
				d[i][count] = j
			counts[i - 1][j] = count
	ans = 0
	for i in range(1, 201):
		for j in range(1, 201):
			start = 0
			end = n - 1
			for k in range(len(d[i]) // 2):
				first = d[i][k + 1]
				second = d[i][counts[i - 1][-1] - k]
				ans = max(ans, 2 * (k + 1) + counts[j - 1][second - 1] - counts[j - 1][first])
	print(ans)


TC = int(input())
for i in range(TC):
	count = 0
	size = int(input())
	arr = list((int(j) for j in input().strip().split()))[:size]
	for i in range(size):
		if arr[i] in arr[i + 2:]:
			index = arr[i + 2:].index(arr[i]) + i + 2
			if len(arr[i + 1:index]) >= 1:
				print('YES')
				count = 1
				break
	if count == 0:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	flag = 0
	i = 0
	while i < n - 1:
		if arr.count(arr[i]) > 2:
			print('YES')
			flag = 1
			break
		elif arr.count(arr[i]) == 2:
			if arr[i] == arr[i + 1]:
				i += 2
				continue
			else:
				print('YES')
				flag = 1
				break
		i += 1
	if flag == 0:
		print('NO')


t = int(input())
how_many_elements = []
strings = []
answers = []
for i in range(t):
	answers.append('NO')
	how_many_elements.append(int(input()))
	strings.append([int(x) for x in input().split()])
for i in range(t):
	if how_many_elements[i] >= 3:
		for x in range(how_many_elements[i]):
			current = strings[i][x]
			has_palindrome = False
			for j in range(how_many_elements[i]):
				if x != j and x != j - 1 and (x != j + 1):
					if strings[i][j] == current:
						answers[i] = 'YES'
						has_palindrome = True
						break
			if has_palindrome:
				break
for x in answers:
	print(x)


def index(l, i):
	for j in range(len(l)):
		if l[j] == i:
			return j
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	x = l[::-1]
	flag = 0
	for i in l:
		d[i] = d.get(i, 0) + 1
	for i in d:
		if d[i] >= 2:
			a = index(l, i)
			b = index(x, i)
			b = n - 1 - b
			if b - a >= 2:
				flag = 1
				break
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(0, t):
	n = int(input())
	l = list(map(int, input().split()))
	k = 1
	for i in range(0, len(l)):
		q = l[i]
		for j in range(i + 2, len(l)):
			if l[j] == q:
				print('YES')
				k = 0
				break
		if k == 0:
			break
	if k == 1:
		print('NO')


n = int(input())
y = []
for i in range(n):
	x = int(input())
	s = list(map(int, input().split()))
	ans = 'NO'
	for k in range(x - 2):
		if s[k + 2:].count(s[k]) >= 1:
			ans = 'YES'
			break
	y.append(ans)
print('\n'.join(y))


t = int(input())
for test in range(t):
	arr = []
	flag = False
	str = input()
	str = input().split()
	for s in str:
		arr.append(int(s))
	for i in range(len(arr) - 2):
		if arr.count(arr[i]) > 1:
			if arr[i + 2:len(arr)].count(arr[i]) != 0:
				flag = True
				break
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = dict()
	for i in range(n):
		if a[i] not in d.keys():
			d[a[i]] = i
		elif i - d[a[i]] > 1:
			print('YES')
			break
	else:
		print('NO')


import sys, bisect
from collections import defaultdict
input = sys.stdin.readline

def inp():
	return int(input().rstrip())

def inlt():
	return list(map(int, input().rstrip().split()))

def insr():
	s = input().rstrip()
	return s[:len(s) - 1]

def invr():
	return map(int, input().rstrip().split())
t = inp()
for _ in range(t):
	n = inp()
	nums = inlt()
	if n == 1:
		print(1)
	elif n == 2:
		if len(set(nums)) == 1:
			print(2)
		else:
			print(1)
	else:
		ans = 1
		for i in range(1, 27):
			left = 0
			right = n - 1
			cnt = 0
			while left < right:
				mid = defaultdict(int)
				if nums[left] == i:
					while left < right:
						if nums[right] == i:
							cnt += 2
							for j in range(left + 1, right):
								mid[nums[j]] += 1
							cur_max = 0
							for key in mid:
								if mid[key] > cur_max:
									cur_max = mid[key]
							ans = max(ans, cnt + cur_max)
							right -= 1
							break
						else:
							right -= 1
				left += 1
		print(ans)


import sys
import math
mod = 998244353

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def modInverse(a, m):
	m0 = m
	y = 0
	x = 1
	if m == 1:
		return 0
	while a > 1:
		q = a // m
		t = m
		m = a % m
		a = t
		t = y
		y = x - q * y
		x = t
	if x < 0:
		x = x + m0
	return x
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	maxi = 0
	index = {}
	s = list(set(a))
	n1 = len(s)
	for i in range(n):
		if index.get(a[i], -1) == -1:
			index[a[i]] = []
		index[a[i]].append(i)
	maxi = 0
	for i in range(n1):
		j1 = 0
		n2 = len(index[s[i]])
		j2 = n2 - 1
		cnt = 0
		while j1 < j2:
			dc = {}
			cnt += 2
			for itr in range(index[s[i]][j1] + 1, index[s[i]][j2]):
				dc[a[itr]] = dc.get(a[itr], 0) + 1
			maxi1 = 0
			for i1 in dc:
				maxi1 = max(maxi1, dc[i1])
			maxi = max(maxi, maxi1 + cnt)
			j1 += 1
			j2 -= 1
	for i in index:
		maxi = max(maxi, len(index[i]))
	print(maxi)


testnum = int(input())
res = []
for i in range(testnum):
	a = []
	a_size = int(input())
	temp = input().split()
	a += temp
	res.append('NO')
	for k in range(a_size):
		if k + 1 < len(a) - 1 - a[::-1].index(a[k]):
			res[i] = 'YES'
			break
for i in range(testnum):
	print(res[i])


t = int(input())
for i in range(t):
	n = int(input())
	ai = list(map(int, input().split()))
	ai2 = [ai[0]]
	num = 1
	maxn = 1
	for i in range(1, n):
		if ai[i] == ai[i - 1]:
			num += 1
		else:
			ai2 += [ai[i]]
			maxn = max(maxn, num)
			num = 1
	maxn = max(maxn, num)
	if len(set(ai2)) != len(ai2) or maxn > 2:
		print('YES')
	else:
		print('NO')


from sys import stdin
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	d = [[0 for _ in range(26)] for _ in range(n)]
	for i in range(n):
		x = a[i] - 1
		if i != 0:
			for j in range(26):
				d[i][j] = d[i - 1][j]
		d[i][x] += 1
	ans = 1
	for i in range(n):
		for j in range(i, n):
			if a[i] != a[j]:
				continue
			x = a[i] - 1
			for k in range(26):
				if k != x:
					ans = max(d[j][x] - d[i][x] + 1 + 2 * min(d[i][k], d[n - 1][k] - d[j][k]), ans)
	print(ans)


def solve(l):
	first_index = [-1] * 5010
	last_index = [-1] * 5010
	for i in range(len(l)):
		e = l[i]
		if first_index[e] == -1:
			first_index[e] = i
		last_index[e] = i
	for i in range(len(first_index)):
		if last_index[i] - first_index[i] >= 2:
			return 'YES'
	return 'NO'
n = int(input())
for i in range(n):
	input()
	l = list(map(int, input().split()))
	print(solve(l))


for _ in range(int(input())):
	n = int(input())
	d = {}
	for (i, e) in enumerate(input().split()):
		if e in d:
			if i - d[e] > 1:
				print('YES')
				break
		else:
			d[e] = i
	else:
		print('NO')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	counters = [[] for _ in range(len(arr))]
	counter = [0 for _ in range(201)]
	mp = dict()
	for idx in range(len(arr)):
		counter[arr[idx]] += 1
		counters[idx] = counter.copy()
		mp[arr[idx], counter[arr[idx]]] = idx
	ans = 1
	for idx_v in range(len(arr)):
		v = arr[idx_v]
		occ_v = counters[idx_v][v]
		total_occs_v = counters[-1][v]
		symmetric_idx_v = mp[v, total_occs_v - occ_v + 1]
		if idx_v >= symmetric_idx_v:
			continue
		mx = max([second - first for (first, second) in zip(counters[idx_v], counters[symmetric_idx_v - 1])]) + occ_v * 2
		ans = max(ans, mx)
	print(ans)


t = int(input())
for case in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	f = False
	for i in range(0, n):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				f = True
				break
	if f:
		print('YES')
	else:
		print('NO')


from collections import defaultdict
from math import gcd
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	d1 = defaultdict()
	for i in range(n):
		d1[arr[i]] = i
	d2 = defaultdict()
	for i in range(n - 1, -1, -1):
		d2[arr[i]] = i
	flag = False
	for key in d1:
		if d1[key] - d2[key] > 1:
			flag = True
			break
	if flag:
		print('YES')
	else:
		print('NO')


from collections import defaultdict, Counter
t = int(input())
for _ in range(t):
	dc = defaultdict(int)
	n = int(input())
	a = list(map(int, input().split()))
	c = Counter(a)
	if c.most_common()[0][1] >= 3:
		print('YES')
		continue
	for i in range(n):
		if dc[a[i]] and i - dc[a[i]] >= 1:
			print('YES')
			break
		else:
			dc[a[i]] = i + 1
	else:
		print('NO')


import sys

def data():
	return sys.stdin.readline().strip()

def mdata():
	return map(int, data().split())
for t in range(int(data())):
	n = int(data())
	A = list(mdata())
	d = {}
	flag = False
	for i in range(n):
		if d.get(A[i]) == None:
			d[A[i]] = i
		elif i - d[A[i]] > 1:
			flag = True
			break
	if flag == True:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	q = int(input())
	r = 0
	e = [-1] * 5002
	w = list(map(int, input().split()))
	for i in range(q):
		if e[w[i]] == -1:
			e[w[i]] = i
		elif i - e[w[i]] > 1:
			print('YES')
			r = 1
			break
	if r == 0:
		print('NO')


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	occ = [[0 for i in range(n)] for i in range(n)]
	for i in range(n):
		r = {}
		m = 0
		for j in range(i, n):
			r[a[j]] = r.get(a[j], 0) + 1
			m = max(m, r[a[j]])
			occ[i][j] = m
	f = defaultdict(list)
	l = {}
	ans = 1
	for i in range(n):
		f[a[i]].append(i)
		l[a[i]] = l.get(a[i], 0) + 1
		for j in range(l[a[i]] // 2):
			left = f[a[i]][j]
			right = f[a[i]][l[a[i]] - 1 - j]
			ans = max(2 * (j + 1) + (occ[left + 1][right - 1] if left + 1 <= right - 1 else 0), ans)
	print(ans)


nCases = int(input())
for _ in range(nCases):
	nNum = int(input())
	nums = [int(x) for x in input().split()]
	palin = False
	for i in range(nNum):
		for j in range(i + 2, nNum, 1):
			if nums[i] == nums[j]:
				palin = True
				break
		if palin:
			break
	if palin:
		print('YES')
	else:
		print('NO')


T = int(input())
for test in range(T):
	n = int(input())
	l = list(map(int, input().split()))
	queue = []
	c = False
	for i in range(n):
		if l[i] in queue[:-1]:
			c = True
			break
		else:
			queue.append(l[i])
	if c:
		print('Yes')
	else:
		print('No')


from sys import stdin

def get_int_input():
	return int(stdin.readline())

def get_str_input():
	return input()

def multiple_input():
	return map(int, stdin.readline().split())

def list_input():
	return list(map(int, stdin.readline().split()))
t = get_int_input()
while t:
	t -= 1
	n = get_int_input()
	a = list_input()
	ans = 'NO'
	for e in set(a):
		if a.count(e) > 1:
			x = a.index(e)
			try:
				if a[x + 2:].index(e) != -1:
					ans = 'YES'
					break
			except:
				pass
	print(ans)


import sys
import math
import heapq
import collections

def inputnum():
	return int(input())

def inputnums():
	return map(int, input().split())

def inputlist():
	return list(map(int, input().split()))

def inputstring():
	return [x for x in input()]

def inputmatrixchar(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d

def inputmatrixint(rows):
	arr2d = []
	for _ in range(rows):
		arr2d.append([int(i) for i in input().split()])
	return arr2d
t = int(input())
for q in range(t):
	n = inputnum()
	a = inputlist()
	done = False
	for i in range(n - 2):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				done = True
				print('YES')
				break
		if done:
			break
	if not done:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	found = False
	for i in range(n):
		try:
			w = a[i + 2:].index(a[i])
			print('YES')
			found = True
			break
		except ValueError:
			continue
	if not found:
		print('NO')


t = int(input())
for T in range(t):
	n = int(input())
	x = input()
	a = []
	a = x.split(' ')
	for i in range(len(a)):
		a[i] = int(a[i])
	visited = []
	for i in range(n + 1):
		visited.append([])
		visited[i].append(0)
		visited[i].append(0)
	flag = False
	for i in range(n):
		if visited[a[i]][0] == 0:
			visited[a[i]][0] = 1
			visited[a[i]][1] = i
		elif i - visited[a[i]][1] > 1:
			flag = True
			break
	if flag:
		print('YES')
	else:
		print('NO')


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d1 = [0 for i in range(201)]
	locs = [[] for i in range(201)]
	values = set()
	counts = []
	for i in range(n):
		d1[a[i]] += 1
		counts.append(d1.copy())
		locs[a[i]].append(i)
		values.add(a[i])
	max_len = 1
	for wing in values:
		current_locs = locs[wing]
		for i in range(len(current_locs) // 2):
			left = current_locs[i]
			right = current_locs[-i - 1] - 1
			max_middle = 0
			for key in values:
				middle_len = counts[right][key] - counts[left][key]
				if middle_len > max_middle:
					max_middle = middle_len
			if (i + 1) * 2 + max_middle > max_len:
				max_len = (i + 1) * 2 + max_middle
	print(max_len)


for _ in range(int(input())):
	n = int(input())
	s = [int(i) for i in input().split()]
	x = True
	for i in range(n - 2):
		if s[i] in s[i + 2:n]:
			print('YES')
			x = False
			break
	if x:
		print('NO')


from sys import stdin
input = lambda : stdin.readline().strip()
lin = lambda : list(map(int, input().split()))
iin = lambda : int(input())
main = lambda : map(int, input().split())
from math import ceil, sqrt, factorial, log
from collections import deque
from bisect import bisect_left

def gcd(a, b):
	(a, b) = (max(a, b), min(a, b))
	while a % b != 0:
		(a, b) = (b, a % b)
	return b
mod = 998244353

def solve():
	n = iin()
	l = lin()
	dp = [[0 for i in range(202)] for i in range(n + 2)]
	z = [[] for i in range(202)]
	for i in range(n):
		z[l[i]].append(i)
		for j in range(201):
			dp[i][j] = dp[i - 1][j]
		dp[i][l[i]] += 1
	ans = 0
	for i in range(1, 201):
		t = len(z[i])
		ans = max(ans, t)
		for j in range(ceil(t // 2)):
			x = z[i][j]
			y = z[i][t - j - 1]
			m = 0
			for k in range(1, 201):
				if i != k:
					m = max(m, dp[y][k] - dp[x][k])
			ans = max(ans, 2 * (j + 1) + m)
	print(ans)
qwe = 1
qwe = int(input())
for _ in range(qwe):
	solve()


import math
tc = int(input())
for _ in range(tc):
	n = int(input())
	s = [int(x) for x in input().split()]
	z = len(set(s))
	a = 'NO'
	for i in range(n - 2):
		for k in range(i + 2, n):
			if s[i] == s[k]:
				a = 'YES'
				break
		if a == 'YES':
			break
	print(a)


def _():
	x = int(input())
	z = set()
	y = list(map(int, input().split()))
	for i in range(1, x):
		if y[i] in z:
			print('YES')
			return 0
		z.add(y[i - 1])
	print('NO')
	return 0
for __ in range(int(input())):
	_()


t = int(input())
for sai in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	flag = 0
	for i in range(n):
		ele = a[i]
		for hareen in range(i + 2, n):
			if ele == a[hareen]:
				flag = 1
				break
		if flag == 1:
			break
	if flag == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	f = False
	for k in range(n):
		for j in range(k + 2, n):
			if l[j] == l[k]:
				f = True
				break
	if f:
		print('YES')
	else:
		print('NO')


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	for i in range(0, n - 2):
		if s == 1:
			break
		for j in range(i + 2, n):
			if a[i] == a[j]:
				print('YES')
				s = 1
				break
	if s == 0:
		print('NO')


n = int(input())
for k in range(n):
	s = int(input())
	arr = list(map(int, input().split()))
	i = 0
	j = len(arr) - 1
	c = 0
	while i < len(arr) - 2:
		j = len(arr) - 1
		while j >= i + 2:
			if arr[j] == arr[i]:
				c = 1
				break
			j = j - 1
		i = i + 1
	print('NO') if c == 0 else print('YES')


def test(a):
	n = len(a)
	for i in range(n):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				return True
	return False
t = int(input())
for dum in range(t):
	n = int(input())
	a = input().split()
	a = list(map(int, a))
	if test(a):
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in a:
		if i in d.keys():
			d[i] += 1
		else:
			d[i] = 1
	for j in d.keys():
		if d[j] > 2:
			print('YES')
			break
		elif d[j] == 2:
			z = a.index(j)
			if z == 0:
				if a[z] != a[z + 1]:
					print('YES')
					break
			elif z == len(a):
				if a[z] != a[z - 1]:
					print('YES')
					break
			elif a[z] != a[z + 1] and a[z] != a[z - 1]:
				print('YES')
				break
	else:
		print('NO')


t = int(input())
for t_ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = [[-1] * 3 for i in range(5005)]
	ok = 0
	for i in range(len(a)):
		x = a[i]
		k = 0
		while k < 3:
			if cnt[x][k] == -1:
				cnt[x][k] = i
				break
			k += 1
		if k == 1:
			if cnt[x][0] != cnt[x][1] - 1:
				ok = 1
				break
		if k == 2:
			ok = 1
			break
	if ok:
		print('YES')
	else:
		print('NO')


t = int(input())
for case_num in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	first = dict()
	ok = False
	for i in range(n):
		if a[i] in first:
			if i - first[a[i]] >= 2:
				ok = True
				break
		else:
			first[a[i]] = i
	print('YES' if ok else 'NO')


import os, sys
from collections import defaultdict, Counter, deque
from io import BytesIO, IOBase
from math import gcd, inf

def main():
	for _ in range(int(input())):
		n = int(input())
		a = [int(x) for x in input().split()]
		f = 1
		v = defaultdict(lambda : -1)
		z = defaultdict(lambda : 0)
		for i in range(n):
			if v[a[i]] == -1:
				v[a[i]] = i + 1
			z[a[i]] += 1
		for i in set(a):
			if z[i] >= 3:
				print('YES')
				f = 0
				break
			elif z[i] == 2:
				if a[v[i]] != i:
					print('YES')
					f = 0
					break
		if f:
			print('NO')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input(''))):
	n = int(input(''))
	a = list(map(int, input().split()))
	cnt = [0] * (n + 1)
	ind = [-1] * (n + 1)
	hoise = 0
	for i in range(n):
		cnt[a[i]] += 1
		if cnt[a[i]] == 2:
			if ind[a[i]] != i - 1:
				hoise = 1
				break
		if cnt[a[i]] > 2:
			hoise = 1
			break
		ind[a[i]] = i
	if hoise:
		print('YES')
	else:
		print('NO')


def T():
	x = int(input())
	s = input().split()
	for i in range(x):
		for j in range(i + 2, x):
			if s[i] == s[j]:
				print('YES')
				return
	print('NO')
[T() for _ in range(int(input()))]


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	p = dict()
	mx = 0
	for i in range(n):
		if a[i] not in p.keys():
			p[a[i]] = list()
		p[a[i]].append(i)
		mx = max(mx, len(p[a[i]]))
	for x in range(1, n // 2 + 1):
		for e in p.keys():
			if len(p[e]) < 2 * x:
				continue
			l = p[e][x - 1]
			r = p[e][len(p[e]) - x]
			for e2 in p.keys():
				if e2 == e:
					continue
				count = 0
				for i in p[e2]:
					if i >= r:
						break
					if i > l:
						count += 1
				mx = max(mx, count + 2 * x)
	print(mx)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	freq = [0 for i in range(n + 1)]
	liste = list(map(int, input().split()))
	found = False
	for i in range(n):
		if found:
			break
		for j in range(i + 2, n):
			if found:
				break
			if liste[i] == liste[j]:
				print('YES')
				found = True
	if not found:
		print('NO')


t = int(input())
for z in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	flag = 0
	for i in range(len(l)):
		if l[i] not in d:
			d[l[i]] = 1
		elif l[i] in d:
			if i - 1 >= 0 and l[i - 1] == l[i] and (d[l[i]] == 1):
				d[l[i]] = -1
			elif d[l[i]] == -1:
				print('YES')
				flag = 1
				break
			else:
				d[l[i]] += 1
				if d[l[i]] == 2:
					print('YES')
					flag = 1
					break
	if flag == 0:
		print('NO')


import sys
input = sys.stdin.buffer.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	current_sums = [0] * 27
	pref_lenghts = [[0] for _ in range(27)]
	result = 0
	for elem in a:
		current_sums[elem] += 1
		for j in range(1, 27):
			pref_lenghts[j].append(current_sums[j])
	for l in range(n):
		for r in range(l + 1, n + 1):
			(pre1, pre2) = (0, 0)
			for j in range(1, 27):
				if pref_lenghts[j][r] - pref_lenghts[j][l] > pre1:
					pre1 = pref_lenghts[j][r] - pref_lenghts[j][l]
				if 2 * min(pref_lenghts[j][l], pref_lenghts[j][n] - pref_lenghts[j][r]) > pre2:
					pre2 = 2 * min(pref_lenghts[j][l], pref_lenghts[j][n] - pref_lenghts[j][r])
			if pre1 + pre2 > result:
				result = pre1 + pre2
	print(result)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if a[i] in d:
			d[a[i]].append(i)
		else:
			d[a[i]] = [i]
	flag = 0
	for i in d:
		if len(d[i]) > 2:
			flag = 1
			break
		elif len(d[i]) == 2:
			if d[i][0] + 1 != d[i][1]:
				flag = 1
				break
	if flag:
		print('YES')
	else:
		print('NO')


def to_list(s):
	return list(map(lambda x: int(x), s.split(' ')))

def solve(a):
	dict_ = {}
	set_a = set(a)
	for item in set_a:
		dict_[item] = 0
	for i in range(len(a)):
		dict_[a[i]] += 1
	for i in range(len(a) - 1):
		if dict_[a[i]] > 2:
			return 'YES'
		elif (a[i + 1] != a[i]) & (dict_[a[i]] > 1):
			return 'YES'
		dict_[a[i]] -= 1
	return 'NO'
answers = []
t = int(input())
for i in range(t):
	n = int(input())
	a = to_list(input())
	answers.append(solve(a))
print('\n'.join(answers))


t = int(input())
for inh in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	b = set()
	ans = False
	b.add(a[0])
	b.add(a[1])
	dic = {}
	dic[a[0]] = 1
	if a[0] == a[1]:
		dic[a[0]] += 1
	else:
		dic[a[1]] = 1
	for i in range(2, n):
		if a[i] in b:
			dic[a[i]] += 1
			if a[i] != a[i - 1] or dic[a[i]] > 2:
				ans = True
				break
		else:
			b.add(a[i])
			dic[a[i]] = 1
	if ans:
		print('YES')
	else:
		print('NO')


for w in range(int(input())):
	l = int(input())
	d = input()
	if l == 3:
		if d == d[::-1]:
			print('YES')
		else:
			print('NO')
	else:
		cond = False
		carrier = [x for x in d.split(' ')]
		e = []
		for x in range(l):
			if carrier[x] not in e:
				e.append(carrier[x])
			elif x >= carrier.index(carrier[x]) + 2:
				cond = True
				break
		if cond:
			print('YES')
		else:
			print('NO')


import sys
from collections import Counter
inp = sys.stdin.readline
ans = ''
for _ in range(int(inp())):
	n = int(inp())
	arr = list(map(int, inp().split()))
	count = Counter(arr)
	if arr == arr[::-1] or count.most_common(1)[0][1] >= 3:
		ans += 'YES\n'
		continue
	flag = 0
	sett = dict()
	for (i, elem) in enumerate(arr):
		if elem in sett:
			if sett[elem] < i - 1:
				ans += 'YES\n'
				flag = 1
				break
		else:
			sett[elem] = i
	if not flag:
		ans += 'NO\n'
print(ans)


data = input().rstrip().split()
t = int(data[0])

def pal(arr):
	if len(arr) < 3:
		return 'NO'
	positions = {}
	for (i, el) in enumerate(arr):
		if el in positions:
			positions[el].append(i)
		else:
			positions[el] = [i]
	for (el, idxs) in positions.items():
		if max(idxs) - min(idxs) > 1:
			return 'YES'
	return 'NO'
for _ in range(t):
	n = input().rstrip().split()
	n = int(n[0])
	data = input().rstrip().split()
	data = [int(d) for d in data]
	print(pal(data))


t = int(input())
for x in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = {}
	for x in range(n):
		if a[x] in b:
			if b[a[x]][-1] != x - 1:
				b[a[x]].append(x)
		else:
			b[a[x]] = [x]
	t = False
	for x in b:
		if len(b[x]) >= 2:
			t = True
	if t:
		print('YES')
	else:
		print('NO')


t = int(input())
for iii in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	flag = False
	for i in range(n - 2):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				print('YES')
				flag = True
				break
		if flag:
			break
	if not flag:
		print('NO')


from collections import defaultdict
t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	d1 = defaultdict(list)
	for i in range(0, len(a)):
		d1[a[i]].append(i)
	for i in d1:
		a2 = d1[i]
		if max(a2) - min(a2) >= 2:
			print('YES')
			break
	else:
		print('NO')
	t -= 1


testCases = int(input())
for _ in range(testCases):
	n = int(input())
	a = list(map(int, input().split()))
	for index in range(n - 2):
		if a[index] in a[index + 2:]:
			print('YES')
			break
		if index == n - 3:
			print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = [0] * n
	flag = True
	for j in range(n):
		l[a[j] - 1] += 1
		if j == 0:
			pass
		elif l[a[j] - 1] == 2 and a[j] != a[j - 1]:
			print('YES')
			flag = False
			break
		elif l[a[j] - 1] == 3:
			print('YES')
			flag = False
			break
	if flag:
		print('NO')


from collections import defaultdict
for _ in range(int(input())):
	dic = defaultdict(list)
	n = int(input())
	a = list(map(int, input().split()))
	flag = 0
	done = 0
	for i in range(n):
		dic[a[i]].append(i)
	for (key, val) in dic.items():
		if len(val) > 1:
			if val[0] + 1 != val[-1]:
				print('YES')
				break
	else:
		print('NO')


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
t = int(input())
for tests in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	COUNT = [[0] * 27 for i in range(n)]
	for i in range(n):
		for j in range(1, 27):
			COUNT[i][j] = COUNT[i - 1][j]
		COUNT[i][A[i]] += 1
	ANS = 1
	for i in range(1, 27):
		bind = 0
		eind = n - 1
		score = 0
		while bind < eind:
			while bind < n and A[bind] != i:
				bind += 1
			while eind >= 0 and A[eind] != i:
				eind -= 1
			if bind < eind:
				score += 2
				C = [0] * 27
				for j in range(27):
					C[j] = COUNT[eind - 1][j] - COUNT[bind][j]
				ANS = max(ANS, score + max(C))
				bind += 1
				eind -= 1
	print(ANS)


t = int(input())
for i in range(t):
	n = int(input())
	s = input().split()
	f = False
	maldade = []
	set = {}
	for c in range(len(s)):
		if s[c] not in set and s.count(s[c]) >= 3:
			print('YES')
			break
		if s[c] not in maldade:
			maldade.append(s[c])
		elif s[c] in maldade and s[c - 1] != s[c]:
			print('YES')
			break
	else:
		print('NO')


from sys import stdin, stdout
t = int(stdin.readline())
for tt in range(t):
	n = int(stdin.readline())
	l = [*map(int, stdin.readline().split())]
	d = {}
	if n <= 2:
		print('NO')
	else:
		for i in range(0, n):
			if l[i] in d:
				a = d[l[i]]
				a[0] += 1
				a.append(i)
				d[l[i]] = a
			else:
				d[l[i]] = [1, i]
		for i in l:
			if i in d and d[i][0] >= 2 and (d[i][1] - d[i][len(d[i]) - 1] != -1):
				print('YES')
				break
		else:
			print('NO')


import collections
for _ in range(int(input())):
	n = int(input())
	s = map(int, input().split())
	s = list(s)
	d = collections.defaultdict(list)
	yes = 'NO'
	for (i, x) in enumerate(s):
		d[x].append(i)
		if len(d[x]) > 2:
			yes = 'YES'
			break
		if len(d[x]) == 2:
			if d[x][0] + 1 != d[x][1]:
				yes = 'YES'
				break
	print(yes)


t = int(input().strip())
from collections import defaultdict
import bisect
for _ in range(t):
	n = int(input().strip())
	nums = [int(i) for i in input().strip().split()]
	the_dict = defaultdict(list)
	for (index, i) in enumerate(nums):
		the_dict[i].append(index)
	has = False
	for i in range(n):
		if the_dict[nums[i]][-1] > i + 1:
			has = True
			break
	if has:
		print('YES')
	else:
		print('NO')


t = int(input())
for query in range(t):
	n = int(input())
	a = input().split()
	b = 0
	for x in range(n):
		for y in range(x + 2, n):
			if a[x] == a[y]:
				b = 1
	if b:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	a = [int(s) for s in input().split()]
	d = {}
	for j in range(n):
		d.setdefault(a[j], set())
		d[a[j]].add(j)
	for j in d.values():
		l = len(j)
		if l >= 3:
			print('YES')
			break
		elif l == 2:
			x = list(j)
			if abs(x[0] - x[1]) > 1:
				print('YES')
				break
	else:
		print('NO')


from sys import stdin, gettrace
if not gettrace():

	def input():
		return next(stdin)[:-1]

def main():

	def solve():
		n = int(input())
		aa = (int(a) for a in input().split())
		am = {}
		for (i, a) in enumerate(aa):
			if a not in am:
				am[a] = i
			elif am[a] < i - 1:
				print('YES')
				return
		print('NO')
		return
	q = int(input())
	for _ in range(q):
		solve()
main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(_) for _ in input().split()]
	used = [a[0]]
	flag = False
	for i in range(1, n):
		if a[i] in used:
			if not a[i] == a[i - 1]:
				flag = True
				break
			elif i > 1:
				if a[i] == a[i - 1] == a[i - 2]:
					flag = True
					break
		else:
			used.append(a[i])
	if flag:
		print('YES')
	else:
		print('NO')


def check(array):
	R = len(array) - 1
	for i in range(len(array)):
		if R < i:
			return True
		elif i == R:
			if array[i] == array[R]:
				return True
			return False
		if array[i] == array[R]:
			R -= 1
		else:
			return False

def palindrome_problem():
	cases = int(input())
	length = []
	arrays = []
	for i in range(cases):
		length.append(int(input()))
		arrays.append(list(map(int, input().split())))
	for i in range(len(length)):
		if length[i] == 3:
			if check(arrays[i]):
				print('YES')
			else:
				print('NO')
		elif length[i] < 3:
			print('NO')
		else:
			dic = {}
			found = False
			for j in range(len(arrays[i])):
				if arrays[i][j] in dic:
					if int(j - dic[arrays[i][j]]) >= 2:
						print('YES')
						found = True
						break
					else:
						arrays[i][j] = j
				else:
					dic[arrays[i][j]] = j
			if not found:
				print('NO')
palindrome_problem()


from collections import defaultdict
import heapq

def main():
	t = int(input())
	while t:
		t -= 1
		n = int(input())
		a = list(map(int, input().split()))
		cnt = [[0 for _ in range(n + 1)] for _ in range(26)]
		for i in range(n):
			for j in range(26):
				cnt[j][i + 1] = cnt[j][i]
			cnt[a[i] - 1][i + 1] += 1
		ans = 0
		for i in range(26):
			ans = max(ans, cnt[i][n - 1])
		for l in range(n):
			for r in range(l, n):
				(maxout, maxin) = (0, 0)
				for i in range(26):
					maxout = max(maxout, min(cnt[i][l], cnt[i][n] - cnt[i][r + 1]) * 2)
					maxin = max(maxin, cnt[i][r + 1] - cnt[i][l])
				ans = max(ans, maxout + maxin)
		print(ans)
main()


t = int(input())
for i in range(t):
	N = int(input())
	nums = input().split()
	isodd_0 = False
	ret = True
	cnt = {}
	for i in range(len(nums)):
		nums[i] = int(nums[i])
		if i > 0:
			if i > 1 and nums[i] == nums[i - 1] and (nums[i - 1] == nums[i - 2]):
				if nums[i] in cnt:
					cnt[nums[i]] += 1
				else:
					cnt[nums[i]] = 1
			if nums[i] != nums[i - 1]:
				if nums[i] in cnt:
					cnt[nums[i]] += 1
				else:
					cnt[nums[i]] = 1
		else:
			cnt[nums[i]] = 1
	check = False
	for val in cnt.values():
		if val >= 2:
			check = True
			break
	if check:
		print('YES')
	else:
		print('NO')


def r(t=int):
	return list(map(t, input().split()))

def ri(t=int):
	return t(input())

def cd(it):
	ret_val = dict()
	for v in it:
		ret_val[v] = ret_val.get(v, 0) + 1
	return ret_val
for _ in range(ri()):
	n = ri()
	a = r()
	started = dict()
	p = a[0]
	for ai in a:
		try:
			started[ai] += 1
			if started[ai] == 3 or ai != p:
				print('YES')
				break
		except:
			started[ai] = 1
		p = ai
	else:
		print('NO')


import sys
import os
from io import BytesIO, IOBase

def cel(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def prr(a, sep=' '):
	print(sep.join(map(str, a)))

def dd():
	return defaultdict(int)

def ddl():
	return defaultdict(list)

def ddd():
	return defaultdict(defaultdict(int))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import defaultdict
for _ in range(ii()):
	n = ii()
	a = [0] + li()
	cnt = [[0 for i in range(201)] for j in range(n + 1)]
	ans = 1
	for i in range(1, n + 1):
		for j in range(1, 201):
			cnt[i][j] = cnt[i - 1][j]
		cnt[i][a[i]] += 1
	for i in range(1, 201):
		(la, ra) = (1, n)
		score = 0
		while la < ra:
			while la <= n and a[la] != i:
				la += 1
			while ra >= 1 and a[ra] != i:
				ra -= 1
			if la < ra:
				score += 2
			if score == 0:
				break
			for j in range(1, 201):
				ans = max(ans, score + cnt[ra - 1][j] - cnt[la][j])
			la += 1
			ra -= 1
	print(ans)


from math import *
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	k = len(a)
	f = 0
	while k >= 3:
		if a[0] in a[2:k]:
			f = 1
			break
		else:
			a.remove(a[0])
			k = k - 1
	if f == 0:
		print('NO')
	else:
		print('YES')


T = int(input())
for t in range(T):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	arr2 = list(reversed(arr))
	count = 0
	state = 'NO'
	for i in range(len(arr) - 2):
		if arr[i] in arr[i + 2:]:
			state = 'YES'
			break
	print(state)


from collections import Counter, defaultdict
from sys import stdin
M = 10000001
T = int(input())
for _ in range(T):
	(N,) = map(int, stdin.readline().split())
	arr = list(map(int, stdin.readline().split()))
	x = defaultdict(lambda : -1)
	zz = False
	for i in range(N):
		curr = x[arr[i]]
		if curr == -1:
			x[arr[i]] = i
		elif i - curr > 1:
			zz = True
	if zz:
		print('YES')
	else:
		print('NO')


t = int(input())
for te in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ase = list(set(a))
	f = 0
	for i in range(len(ase)):
		if a.count(ase[i]) >= 3:
			f = 1
			print('YES')
			break
	if f == 0:
		for i in range(len(ase)):
			if a.count(ase[i]) == 2:
				ind = a.index(ase[i])
				ind2 = a[ind + 1:].index(ase[i]) + (ind + 1)
				if ind + 1 != ind2:
					print('YES')
					f = 1
					break
	if f == 0:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	m = [-1 for i in range(n + 1)]
	f = 0
	for i in range(n):
		if m[a[i]] == -1:
			m[a[i]] = i
		elif m[a[i]] != i - 1:
			f = 1
	if f:
		print('YES')
	else:
		print('NO')


def subPalindrome(a):
	a2 = list()
	a2.append(a.pop())
	first = a.pop()
	while a:
		next = a.pop()
		if next in a2:
			return True
		a2.append(first)
		first = next
	return False
for _ in range(int(input())):
	n = int(input())
	a = input().split(' ')
	print('YES' if subPalindrome(a) else 'NO')


t = int(input())
while t != 0:
	t -= 1
	n = int(input())
	a = input().split()
	ok = False
	m = {}
	for i in range(n):
		if a[i] not in m:
			m[a[i]] = 1
		elif a[i] == a[i - 1] and m[a[i]] == 1:
			m[a[i]] += 1
		else:
			ok = True
	if ok:
		print('YES')
	else:
		print('NO')


import sys, os, io
for _ in range(int(input())):
	n = int(input())
	a = [int(i) - 1 for i in input().split()]
	cnt = [[0] * n for i in range(26)]
	ind = [[] for i in range(26)]
	for i in range(n):
		cnt[a[i]][i] += 1
		ind[a[i]].append(i)
	for i in range(26):
		for j in range(1, n):
			cnt[i][j] += cnt[i][j - 1]
	ans = 0
	for i in range(26):
		l = 0
		r = len(ind[i]) - 1
		tans = 0
		ttans = 0
		while l < r:
			ttans += 2
			for j in range(26):
				ttans += cnt[j][ind[i][r] - 1] - cnt[j][ind[i][l]]
				tans = max(tans, ttans)
				ttans -= cnt[j][ind[i][r] - 1] - cnt[j][ind[i][l]]
			l += 1
			r -= 1
			ans = max(ans, tans)
	for i in range(26):
		ans = max(ans, cnt[i][-1])
	print(ans)


def solve(arr):
	seen = dict()
	for (index, val) in enumerate(arr):
		if val not in seen:
			seen[val] = index
			continue
		if index - seen[val] > 1:
			return True
	return False
t = int(input())
for test in range(t):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	if solve(arr):
		print('YES')
	else:
		print('NO')


import bisect
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	variety = list(set(arr))
	ans = 0
	for a in variety:
		for b in variety:
			if a == b:
				ans = max(ans, arr.count(a))
			else:
				acum1 = [0] * n
				acum2 = [0] * n
				for i in range(n):
					if arr[i] == a:
						acum1[i] += 1
					elif arr[i] == b:
						acum2[i] += 1
					acum1[i] += acum1[i - 1]
					acum2[i] += acum2[i - 1]
				for i in range(1, acum1[-1] // 2 + 1):
					l = bisect.bisect_left(acum1, i)
					r = bisect.bisect_left(acum1, acum1[-1] - i + 1)
					tmp = i * 2 + acum2[r] - acum2[l]
					ans = max(ans, tmp)
	print(ans)


t = int(input())
numsSeen = []
for i in range(t):
	works = False
	n = int(input())
	del numsSeen[:]
	nums = list(map(int, input().split(' ')))
	for i in range(n):
		if nums.count(nums[i]) > 2:
			print('YES')
			works = True
			break
		elif nums.count(nums[i]) == 2 and nums[i] not in numsSeen:
			if (i != 0 and nums[i - 1] != nums[i] or i == 0) and (i != n - 1 and nums[i + 1] != nums[i] or i == n - 1):
				print('YES')
				works = True
				break
		numsSeen.append(nums[i])
	if not works:
		print('NO')


def solve(n, a):
	d = {}
	for i in range(n):
		if a[i] in d:
			d[a[i]].append(i)
		else:
			d[a[i]] = [i]
	for i in d:
		if len(d[i]) >= 3:
			return 'YES'
		if len(d[i]) == 2:
			if d[i][1] - d[i][0] > 1:
				return 'YES'
	return 'NO'

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = [int(i) for i in input().split()]
		print(solve(n, a))
main()


a = int(input())
ans = ''
for i in range(a):
	b = int(input())
	c = input().split()
	dict = {}
	chckr = 0
	for j in range(len(c)):
		if c[j] in dict and abs(int(j) - int(dict[c[j]])) > 1:
			print('YES')
			chckr = 1
			break
		elif c[j] not in dict:
			dict[c[j]] = j
	if chckr == 0:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = set()
	l = None
	for x in a:
		if x in s and len(s) >= 1:
			print('YES')
			break
		if l:
			s.add(l)
		l = x
	else:
		print('NO')


import sys
input = sys.stdin.readline
q = int(input())
for _ in range(q):
	N = int(input())
	A = list(map(int, input().split()))
	if N < 3:
		print('NO')
	else:
		ans = False
		for i in range(N - 2):
			x = A[i]
			for j in range(i + 2, N):
				if A[j] == x:
					ans = True
		if ans:
			print('YES')
		else:
			print('NO')


import sys
from math import ceil, sqrt
read = sys.stdin.buffer.readline
mi = lambda : map(int, read().split())
li = lambda : list(mi())
cin = lambda : int(read())

def isp(n):
	i = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += 1
	return True
primes = []
for _ in range(cin()):
	n = cin()
	a = li()
	(i, j) = (0, n - 1)
	flag = False
	while i < n - 1:
		if a[i] == a[j] and j - i > 1:
			flag = True
			break
		j -= 1
		if j <= i:
			i += 1
			j = n - 1
	if flag:
		print('yes')
	else:
		print('no')


import sys
import copy
input = sys.stdin.readline

def main():
	t = int(input())
	for _ in range(t):
		N = int(input())
		A = [int(x) for x in input().split()]
		pre = []
		suf = []
		for (i, v) in enumerate(A):
			if i == 0:
				tmp = [0] * 27
				tmp[v] += 1
				pre.append(tmp)
			else:
				tmp = copy.deepcopy(pre[i - 1])
				tmp[v] += 1
				pre.append(tmp)
		for (i, v) in enumerate(A[::-1]):
			if i == 0:
				tmp = [0] * 27
				tmp[v] += 1
				suf.append(tmp)
			else:
				tmp = copy.deepcopy(suf[i - 1])
				tmp[v] += 1
				suf.append(tmp)
		ans = 0
		for i in range(N):
			for j in range(N):
				if N - 1 == i + j:
					break
				tmp = 0
				for k in range(1, 27):
					tmp = max(tmp, min(pre[i][k], suf[j][k]) * 2)
				ma = 0
				for k in range(1, 27):
					ma = max(ma, pre[N - j - 2][k] - pre[i][k])
				ans = max(ans, tmp + ma)
		print(max(1, ans))
main()


def ispal(l):
	d = {}
	for i in range(len(l)):
		d[l[i]] = d.get(l[i], i)
		if d[l[i]] + 1 < i:
			return 'YES'
	return 'NO'
t = int(input())
for i in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	print(ispal(l))


testcase = int(input())
for i in range(testcase):
	flag = 0
	num = int(input())
	array = list((int(num2) for num2 in input().strip().split()))[:num]
	for i in range(num):
		if array[i] in array[i + 2:]:
			ind = array[i + 2:].index(array[i]) + i + 2
			if len(array[i + 1:ind]) >= 1:
				print('YES')
				flag = 1
				break
	if flag == 0:
		print('NO')


def has_pal(n, arr):
	for i in range(n):
		if arr[i] in arr[i + 2:]:
			return 1
	return -1
t = int(input())
for i in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	if has_pal(n, arr) == 1:
		print('YES')
	else:
		print('NO')


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = 0
	d = defaultdict(list)
	for i in range(n):
		d[a[i]].append(i)
	for i in d:
		if d[i][-1] - d[i][0] >= 2:
			c = 1
			break
	if c == 1:
		print('YES')
	else:
		print('NO')


numTestCases = int(input())
caseLengths = []
cases = []
for i in range(numTestCases):
	caseLengths.append(int(input()))
	cases.append(list(map(int, input().split())))
for case in cases:
	isThere = False
	for i in range(len(case) - 2):
		if case[i] in case[i + 2:len(case)]:
			print('YES')
			isThere = True
			break
	if not isThere:
		print('NO')


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		A = list(map(int, input().split()))
		C = [[0] * (n + 1) for _ in range(200)]
		P = [[] for _ in range(200)]
		for i in range(n):
			for j in range(200):
				if j == A[i] - 1:
					C[j][i + 1] = C[j][i] + 1
					P[j].append(i)
				else:
					C[j][i + 1] = C[j][i]
		ans = 0
		for i in range(200):
			if len(P[i]) >= ans:
				ans = len(P[i])
			if len(P[i]) == 0 or len(P[i]) == 1:
				continue
			for j in range(len(P[i]) // 2 + 1):
				l = P[i][j]
				r = P[i][-(j + 1)]
				if r <= l:
					continue
				for k in range(200):
					temp = 2 * (j + 1) + C[k][r] - C[k][l + 1]
					if temp >= ans:
						ans = temp
		print(ans)
main()


from collections import Counter, defaultdict, deque
from heapq import heapify, heappush, heappop, heappushpop
from sys import stdin
int_inp = lambda : int(stdin.readline().strip())
lst_inp = lambda : [int(i) for i in stdin.readline().strip().split(' ')]
str_inp = lambda : stdin.readline().strip()

def solve(a, n):
	l = Counter()
	l[a[0]] += 1
	r = Counter(a)
	r[a[0]] -= 1
	r[a[1]] -= 1
	for i in range(1, n - 1):
		for k in l:
			if l[k] * r[k] > 0:
				return 'YES'
		l[a[i]] += 1
		r[a[i + 1]] -= 1
	return 'NO'
for _ in range(int_inp()):
	n = int_inp()
	a = lst_inp()
	print(solve(a, n))


def max_tbp(xs, a, b):
	m = 1
	occs = [i for (i, x) in enumerate(xs) if x == a]
	for i in range(len(occs) // 2):
		st = occs[i]
		en = occs[len(occs) - 1 - i]
		cnt = 0
		for x in xs[st + 1:en]:
			if x == b:
				cnt += 1
		m = max(m, (i + 1) * 2 + cnt)
	return m
t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	m = 0
	sA = set(A)
	for a in sA:
		for b in sA:
			m = max(m, max_tbp(A, a, b))
	print(m)


for _ in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	flag = 0
	d = {}
	count = 0
	for i in range(len(l)):
		if l.count(l[i]) >= 3:
			flag = 1
			break
		elif l[i] not in d:
			d[l[i]] = count
			count += 1
		elif l[i] != list(d.keys())[-1]:
			flag = 1
			break
	if flag == 1:
		print('YES')
	else:
		print('NO')


N = int(input())
for t in range(N):
	n = int(input())
	A = list(map(int, input().split()))
	d = dict()
	for a in A:
		if a not in d.keys():
			d[a] = 0
		d[a] += 1
	for (key, value) in d.items():
		if value > 2:
			print('YES')
			break
		if value == 1:
			continue
		first = -1
		second = -2
		first = A.index(key)
		second = A.index(key, first + 1)
		if second - first > 1:
			print('YES')
			break
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	D = {}
	V = 'NO'
	L = list(map(int, input().split()))
	for i in range(n):
		if L[i] not in D:
			D[L[i]] = [i]
		else:
			D[L[i]].append(i)
	for i in range(n):
		if L[i] in D:
			if D[L[i]][-1] - D[L[i]][0] >= 2:
				V = 'YES'
				break
	print(V)


testcases = int(input())
for i in range(testcases):
	n = int(input())
	arr = list(map(int, input().split()))
	Exists = False
	for i in range(n):
		for j in range(i + 2, n):
			if arr[i] == arr[j]:
				Exists = True
				print('YES')
				break
		if Exists:
			break
	if Exists == False:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	x = [*map(int, input().split())]
	saved = dict()
	fail = True
	for (n, i) in enumerate(x):
		if saved.get(i, -1) != -1:
			if n - saved[i] > 1:
				print('YES')
				fail = False
				break
		else:
			saved[i] = n
	if fail:
		print('NO')


for o in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	f = 1
	for i in range(n - 2):
		if a[i] in a[i + 2:]:
			print('YES')
			f = 0
			break
	if f == 1:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	check = set()
	nah = False
	for j in range(n):
		check.add(a[j])
		for k in range(j + 1, n):
			if a[k] in check and k - j > 1:
				nah = True
	if nah:
		print('YES')
	else:
		print('NO')


test_case = int(input())
while test_case:
	n = int(input())
	list = input().split(' ')
	bool = False
	for i in range(0, len(list)):
		for j in range(i + 2, len(list)):
			if list[i] == list[j]:
				bool = True
				break
		if bool:
			break
	if bool:
		print('YES')
	else:
		print('NO')
	test_case -= 1


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = [0] * (n + 1)
	f = 0
	for i in range(n):
		if i != 0:
			if l1[l[i]] > 0 and l1[l[i]] < 2:
				if l[i - 1] != l[i]:
					f = 1
					break
			elif l1[l[i]] >= 2:
				f = 1
				break
		l1[l[i]] += 1
	if f == 1:
		print('YES')
	else:
		print('NO')


for q11 in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = {}
	for q in range(n):
		if a[q] in s and s[a[q]] < q - 1:
			print('YES')
			break
		if a[q] not in s:
			s[a[q]] = q
	else:
		print('NO')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = set(a)
	m = 1
	dp = [[0] * 201 for i in range(n)]
	for i in range(n):
		for j in range(1, 201):
			dp[i][j] = dp[i - 1][j]
		dp[i][a[i]] += 1
	for i in s:
		(start, end, count) = (0, n - 1, 0)
		while start < end:
			while start < end and a[start] != i:
				start += 1
			while start < end and a[end] != i:
				end -= 1
			if start < end:
				count += 2
				for j in range(1, 201):
					m = max(m, count + dp[end - 1][j] - dp[start][j])
				start += 1
				end -= 1
	print(m)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = [[] for i in range(5002)]
	ch = 0
	for j in range(n):
		for h in b[a[j]]:
			if abs(h - j) > 1:
				ch = 1
				break
		b[a[j]].append(j)
	if ch == 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a[::-1]
	f = 0
	for i in range(n):
		c = a.index(a[i])
		d = n - 1 - b.index(a[i])
		if abs(c - d) >= 2:
			f += 1
	if f >= 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	pos = [-1] * 5555
	ans = 'NO'
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n):
		if pos[a[i]] < 0:
			pos[a[i]] = i
		elif i - pos[a[i]] > 1:
			ans = 'YES'
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	t1 = n - 1
	i = 0
	flag = 0
	if n < 3:
		print('NO')
	else:
		for i in range(n):
			if a[i + 2:].count(a[i]) >= 1:
				flag = 1
	if flag == 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	flag = 0
	for i in range(n):
		if flag == 1:
			break
		for j in range(i + 1, n):
			if a[i] == a[j] and j - i > 1:
				print('YES')
				flag = 1
				break
	if flag == 0:
		print('NO')


n = int(input())
while n != 0:
	t = False
	nu = input()
	txt = input()
	list1 = list(map(int, txt.split(' ')))
	for i in range(len(list1)):
		if list1[i] in list1[i + 2:len(list1)]:
			print('YES')
			t = True
			break
	if t == False:
		print('NO')
	n = n - 1


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	flag = 0
	freq = {}
	freq[arr[0]] = 1
	for i in range(1, n):
		if arr[i - 1] != arr[i] or (i >= 2 and arr[i - 2] == arr[i]):
			if arr[i] in freq.keys():
				flag = 1
				break
			else:
				freq[arr[i]] = 1
	if flag:
		print('YES')
	else:
		print('NO')


def main():
	ch = True
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(len(a)):
		try:
			b = a[i + 1:]
			b.reverse()
			if not b.index(a[i]) in (len(b) - 1, -1):
				print('YES')
				ch = False
				break
		except ValueError:
			pass
	if ch:
		print('NO')
for _ in range(int(input())):
	main()


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	nums = [0] * (n + 1)
	pal = 0
	prec = 0
	for j in range(n):
		nums[a[j]] += 1
		if nums[a[j]] == 2 and prec != a[j]:
			pal = 1
		if nums[a[j]] == 3:
			pal = 1
		prec = a[j]
	if pal == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	a = b = c = 0
	for i in range(n):
		for j in range(i + 1):
			a = arr[i]
		for k in range(i + 2, n):
			if arr[k] == a:
				c += 1
				break
	if c > 0:
		print('YES')
	else:
		print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	a = list(input().split())
	k = ' '.join(a)
	b = set(a)
	for i in b:
		if a.count(i) > 2:
			n = 0
		elif a.count(i) == 2:
			if i + ' ' + i not in k:
				n = 0
	if n == 0:
		print('YES')
	else:
		print('NO')


def checker(n, lst):
	flag = False
	for i in range(n):
		for j in range(i + 2, n):
			if lst[i] == lst[j]:
				flag = True
	return 'YES' if flag else 'NO'
for _ in range(int(input())):
	m = int(input())
	a = [int(i) for i in input().split()]
	print(checker(m, a))


a = int(input())
c = []
for i in range(a):
	b = int(input())
	d = input().split()
	for j in range(b):
		d[j] = int(d[j])
	c.append(d)
for i in c:
	s = list(set(i))
	if len(s) == len(i):
		print('NO')
	else:
		dif = len(i) - len(s)
		count = 0
		for j in range(len(i) - 1):
			if i[j] == i[j + 1]:
				if j < len(i) - 2 and i[j + 1] == i[j + 2]:
					print('YES')
					count = 99999999999
					break
				else:
					count += 1
		if count < 99999999:
			if count >= dif:
				print('NO')
			else:
				print('YES')


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = Counter(a)
	x = [(i, j) for i in s for j in s if i != j]
	maxi = 0
	for (i, j) in x:
		size = 0
		l = 0
		r = n - 1
		while True:
			while a[l] != i:
				l += 1
			while a[r] != i:
				r -= 1
			if l >= r:
				break
			size += 1
			maxi = max(maxi, size * 2 + a[l:r].count(j))
			l += 1
			r -= 1
	print(max(maxi, max(s.values())))


t = int(input())
for you in range(t):
	n = int(input())
	lpos = [[] for i in range(200)]
	lfi = [[0 for i in range(200)] for j in range(n)]
	l = input().split()
	li = [int(i) for i in l]
	for j in range(200):
		count = 0
		for i in range(n):
			if li[i] == j + 1:
				count += 1
			lfi[i][j] = count
	for i in range(n):
		lpos[li[i] - 1].append(i)
	maxa = 0
	for i in range(200):
		for numofel in range(1, len(lpos[i]) + 1):
			start = lpos[i][numofel - 1] + 1
			end = lpos[i][len(lpos[i]) - numofel] - 1
			if start <= end:
				for k in range(200):
					if k == i:
						continue
					if start == 0:
						if lfi[end][k] + 2 * numofel > maxa:
							maxa = 2 * numofel + lfi[end][k]
					elif lfi[end][k] + 2 * numofel - lfi[start - 1][k] > maxa:
						maxa = 2 * numofel + lfi[end][k] - lfi[start - 1][k]
			else:
				break
		if maxa < len(lpos[i]):
			maxa = len(lpos[i])
	print(maxa)


def solution():
	from sys import stdout, stdin
	(_input, _print) = (stdin.readline, stdout.write)
	(_int, _range, _min, _max) = (int, range, min, max)
	for _ in _range(_int(_input())):
		n = _int(_input())
		arr = [_int(i) for i in _input().split()]
		exp = 201
		end = [0] * exp
		for i in arr:
			end[i] += 1
		ans = _max(end)
		l = [0] * exp
		for i in _range(n - 1):
			l[arr[i]] += 1
			r = [0] * exp
			for j in _range(n - 1, i, -1):
				r[arr[j]] += 1
				d = _min(l[arr[j]], r[arr[j]])
				for k in _range(exp):
					if end[k] - (r[k] + l[k]) + d * 2 > ans:
						ans = end[k] - (r[k] + l[k]) + d * 2
		print(ans)
solution()


t = int(input())
for z in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	L = [[0 for x in range(n)] for x in range(n)]
	for i in range(n):
		L[i][i] = 1
	for cl in range(2, n + 1):
		for i in range(n - cl + 1):
			j = i + cl - 1
			if l[i] == l[j] and cl == 2:
				L[i][j] = 2
			elif l[i] == l[j]:
				L[i][j] = L[i + 1][j - 1] + 2
			else:
				L[i][j] = max(L[i][j - 1], L[i + 1][j])
	if L[0][n - 1] >= 3:
		print('YES')
	else:
		print('NO')


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	dict = Counter(a)
	ans = False
	for i in range(n - 1):
		if dict[a[i]] > 2:
			ans = True
			break
		elif dict[a[i]] == 2:
			if a[i + 1] != a[i]:
				ans = True
				break
			else:
				dict[a[i]] -= 1
	if ans == True:
		print('YES')
	else:
		print('NO')


for ii in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	if len(a) >= 3:
		temp = 0
		idx = 0
		idx2 = idx + 2
		for i in range(0, n):
			idx2 = i + 2
			idx = i
			while idx2 < len(a):
				if a[idx] == a[idx2]:
					temp = 1
					break
				idx2 += 1
			if temp == 1:
				break
		if temp == 1:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = input()
	a = map(int, input().split())
	d = defaultdict(list)
	for (i, e) in enumerate(a):
		d[e].append(i)
	found = any((v[-1] - v[0] > 1 for (k, v) in d.items()))
	print('YES' if found else 'NO')


from math import ceil, sqrt, gcd, log, floor
from collections import deque

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def li():
	return list(mi())

def main():
	for i in range(ii()):
		n = ii()
		a = li()
		f = 0
		for i in range(n):
			x = a[i]
			for j in range(i + 2, n):
				if a[j] == x:
					f = 1
					break
			if f:
				break
		if f == 0:
			print('NO')
		else:
			print('YES')
main()


import sys
input = sys.stdin.buffer.readline

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		A = list(map(int, input().split()))
		C = [[0] * (n + 1) for _ in range(200)]
		P = [[] for _ in range(200)]
		for i in range(n):
			for j in range(200):
				if j == A[i] - 1:
					C[j][i + 1] = C[j][i] + 1
					P[j].append(i)
				else:
					C[j][i + 1] = C[j][i]
		ans = 0
		for i in range(200):
			if len(P[i]) >= ans:
				ans = len(P[i])
			if len(P[i]) == 0 or len(P[i]) == 1:
				continue
			for j in range(len(P[i]) // 2 + 1):
				l = P[i][j]
				r = P[i][-(j + 1)]
				if r <= l:
					continue
				for k in range(200):
					temp = 2 * (j + 1) + C[k][r] - C[k][l + 1]
					if temp >= ans:
						ans = temp
		print(ans)
main()


t = int(input())

def main(n):
	l = list(map(int, input().split()))
	for i in range(len(l) - 2):
		a = False
		for j in range(i + 2, len(l)):
			if l[j] == l[i]:
				return True
	return False
for i in range(t):
	n = int(input())
	ans = main(n)
	if ans:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	arr = [int(k) for k in stdin.readline().split()]
	dic = {}
	counter = 0
	for i in range(n):
		if arr[i] not in dic:
			dic[arr[i]] = [i]
		else:
			dic[arr[i]].append(i)
	for (i, j) in dic.items():
		if len(j) >= 2:
			for k in range(len(j)):
				if j[k] - j[0] >= 2:
					counter = 1
					break
	if counter == 1:
		stdout.write('YES')
		stdout.write('\n')
	else:
		stdout.write('NO')
		stdout.write('\n')


T = int(input())
for t in range(T):
	n = int(input())
	a = input().split()
	z = False
	for i in range(0, len(a)):
		for j in range(i + 2, len(a)):
			if a[i] == a[j]:
				z = True
				break
				break
	if z:
		print('YES')
	else:
		print('NO')


def binsearchfunc(element, start, end, ar):
	while start <= end:
		middle = start + (end - start) // 2
		if ar[middle] == element:
			return True
		elif ar[middle] > element:
			end = middle - 1
		else:
			start = middle + 1
	return False

def binsearch(element, start, end, ar):
	newList = ar[start:end + 1]
	newList.sort()
	if binsearchfunc(element, 0, len(newList) - 1, newList) == True:
		return True
	else:
		return False

def palindrome(n, ar):
	for (index, i) in enumerate(ar):
		if index == n - 2:
			print('NO')
			return
		if binsearch(i, index + 2, n - 1, ar) is True:
			print('YES')
			return

def main():
	t = int(input())
	while t != 0:
		size = int(input())
		lst = list(map(int, input().strip().split()))
		palindrome(size, lst)
		t -= 1
main()


t = int(input())
for rep in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	f = 0
	d = {}
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = i
		elif i >= d[a[i]] + 2:
			f = 1
			print('YES')
			break
	if f == 0:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	inds = dict()
	for i in range(n):
		inds.setdefault(arr[i], [])
		inds[arr[i]].append(i)
	check = False
	for (_, ind) in inds.items():
		if max(ind) - min(ind) >= 2:
			check = True
			break
	print('YES' if check else 'NO')


from __future__ import division, print_function

def get_ints():
	return map(int, input().strip().split())

def get_array():
	return list(map(int, input().strip().split()))
from math import pow, gcd
from random import randint

def main():
	for _ in range(int(input())):
		n = int(input())
		arr = get_array()
		if arr == arr[::-1]:
			print('YES')
			continue
		flag = False
		for i in range(n):
			if arr[i] in arr[i + 2:]:
				flag = True
		if flag:
			print('YES')
		else:
			print('NO')
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def run():
	n = int(input())
	a = list(map(int, input().split()))
	cnt = [0] * 5010
	for i in range(n - 2):
		cnt[a[i]] += 1
	ptr = n - 2
	while ptr > 0:
		for i in range(ptr + 1, n):
			if cnt[a[i]] > 0:
				print('YES')
				return
		cnt[a[ptr - 1]] -= 1
		ptr -= 1
	print('NO')
tt = int(input())
while tt > 0:
	tt -= 1
	run()


for u in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	f = 0
	for i in range(n - 2):
		for j in range(i + 2, n):
			if l[i] == l[j]:
				f = 1
				break
		if f == 1:
			break
	if f == 0:
		print('NO')
	else:
		print('YES')


import sys
from collections import defaultdict
input = sys.stdin.readline
for ti in range(int(input().strip())):
	n = int(input().strip())
	a = [int(x) for x in input().strip().split()]
	d = defaultdict(list)
	for i in range(len(a)):
		d[a[i]].append(i)
	for di in d:
		if len(d[di]) >= 2:
			if d[di][-1] - d[di][0] > 1:
				print('YES')
				break
	else:
		print('NO')


import sys
inp = sys.stdin.readline
for i in range(int(inp())):
	n = int(inp())
	a = list(map(int, inp().split()))
	t = 1
	for j in a:
		r = a.count(j)
		if r == 2:
			x = a.index(j)
			if x + 1 != a.index(j, 1 + x):
				print('YES')
				t = 0
				break
		elif r > 2:
			print('YES')
			t = 0
			break
	if t:
		print('NO')


def possible(l, n):
	d = {}
	for i in range(n):
		val = d.get(l[i], None)
		if val is not None:
			if i - val >= 2:
				return 'Yes'
		else:
			d[l[i]] = i
	return 'No'
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	print(possible(l, n))


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = dict()
	for (j, k) in zip(l, range(len(l))):
		if j not in d:
			d[j] = []
		d[j].append(k)
	for j in d:
		flag = False
		if len(d[j]) >= 3:
			print('YES')
			flag = True
			break
		elif len(d[j]) == 2:
			flag = True
			x = d[j]
			if len(x) != 1:
				temp = [x[y] - x[y - 1] for y in range(1, len(x))]
				if 1 in temp:
					flag = False
				if flag:
					print('YES')
					flag = True
					break
	if not flag:
		print('NO')


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		arr = [int(x) for x in input().split()]
		n = len(arr)
		f = False
		for i in range(n):
			for j in range(i + 2, n):
				if arr[i] == arr[j]:
					f = True
		if f:
			print('YES')
		else:
			print('NO')
main()


n = int(input())
for x in range(n):
	t = int(input())
	l = list(map(int, input().split()))
	xy = set()
	bools = 'NO'
	mins = -1
	for tp in range(t):
		if l[tp] not in xy:
			xy.add(l[tp])
		elif mins != l[tp]:
			bools = 'YES'
			break
		elif mins == l[tp] and mins == l[tp - 2]:
			bools = 'YES'
			break
		mins = l[tp]
	print(bools)


t = int(input())
for i in range(t):
	n = int(input())
	ans = 'NO'
	nums = list(map(int, input().split()))
	setnum = list(dict.fromkeys(nums))
	for j in setnum:
		ind = nums.index(j)
		count = nums.count(j)
		if count > 2 or (count == 2 and nums[ind] != nums[ind + 1]):
			ans = 'YES'
			break
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	li = list(map(int, input().split()))
	flag = 1
	for i in range(n):
		if li[i + 2:].count(li[i]) > 0:
			print('YES')
			flag = 0
			break
	if flag == 1:
		print('NO')


t = int(input())
for t1 in range(0, t):
	n = int(input())
	l = list(map(int, input().split()))
	s = [0] * n
	flag = 0
	d = dict()
	for i in range(0, n):
		if l[i] not in d:
			d[l[i]] = [i]
		else:
			d[l[i]].append(i)
	for i in d:
		if len(d[i]) > 2:
			flag = 1
			break
		elif len(d[i]) == 2:
			if d[i][0] + 1 != d[i][1]:
				flag = 1
				break
	if flag == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	result = 'NO'
	i = 0
	while i < n:
		if a.count(a[i]) == 1:
			i += 1
		elif a.count(a[i]) == 2:
			if a[i] == a[i + 1]:
				i += 2
			else:
				result = 'YES'
				break
		else:
			result = 'YES'
			break
	print(result)


def qq(n, a):
	for i in range(n - 1):
		k = a[i]
		t = a[i + 1]
		a[i] = 0
		a[i + 1] = 0
		if k in a:
			return 'YES'
		a[i + 1] = t
	return 'NO'
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print(qq(n, a))


import sys
sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline

def solve():
	t = int(rl())
	for _ in range(t):
		n = int(rl())
		a = list(map(int, rl().split()))
		flg = False
		for i in range(n - 2):
			if a[i] in a[i + 2:]:
				flg = True
				break
		print('YES' if flg else 'NO')
solve()


for __ in range(int(input())):
	n = int(input())
	arr = [int(s) for s in input().split()]
	flag = 0
	for i in range(n - 2):
		if arr[i] in arr[i + 2:]:
			flag = 1
			break
	if flag:
		print('YES')
	else:
		print('NO')


col = int(input())
for _ in range(col):
	n = int(input())
	flag = False
	L = list(map(int, input().split()))
	x = 3
	for i in range(n):
		if L[i] in L[i + 2:]:
			print('YES')
			break
	else:
		print('NO')


import sys
read = lambda : list(map(int, sys.stdin.readline().strip().split()))
for _ in range(int(input())):
	n = int(input())
	a = read()
	mark = [0] * (n + 1)
	for num in a:
		mark[num] += 1
	flag = False
	for i in range(n - 2):
		if mark[a[i]] == 2 and a[i] != a[i + 1]:
			flag = True
			break
		elif mark[a[i]] > 2:
			flag = True
			break
		mark[a[i]] -= 1
	if flag:
		print('YES')
	else:
		print('NO')


tc = int(input())
while tc:
	n = int(input())
	l = list(map(int, input().split()))
	for i in range(len(l)):
		if l.count(l[i]) >= 3:
			print('YES')
			break
		elif l.count(l[i]) == 2:
			m = l[i]
			l[i] = 0
			y = l.index(m)
			if i + 1 != y:
				print('YES')
				break
	else:
		print('NO')
	tc -= 1


from sys import stdin

def inp():
	return stdin.buffer.readline().rstrip().decode('utf8')

def itg():
	return int(stdin.buffer.readline())

def mpint():
	return map(int, stdin.buffer.readline().split())

def merge(a1, a2):
	if not a1:
		return a2
	if not a2:
		return a1
	i1 = i2 = 0
	m = []
	while True:
		if a1[i1] < a2[i2]:
			m.append(a1[i1])
			i1 += 1
			if i1 == len(a1):
				m.extend(a2[i2:])
				return m
		else:
			m.append(a2[i2])
			i2 += 1
			if i2 == len(a2):
				m.extend(a1[i1:])
				return m
for case in range(itg()):
	n = itg()
	arr = tuple(mpint())
	lst = [[] for _ in range(201)]
	for i in range(n):
		lst[arr[i]].append((i, arr[i]))
	ans = max(range(1, 201), key=lambda x: len(lst[x]))
	ans = len(lst[ans])
	for a in range(1, 201):
		for b in range(1, 201):
			if a == b or len(lst[a]) + len(lst[b]) <= ans or len(lst[a]) == 0 or (len(lst[b]) == 0):
				continue
			m = merge(lst[a], lst[b])
			m1 = 0
			m2 = len(m) - 1
			for i in range(1, len(lst[a]) // 2 + 1):
				while m[m1][1] != a:
					m1 += 1
				while m[m2][1] != a:
					m2 -= 1
				a_and_b = m2 - m1 - 1
				a_inside = len(lst[a]) - i * 2
				b_count = a_and_b - a_inside
				ans = max(ans, b_count + i * 2)
				m1 += 1
				m2 -= 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	debug = True
	arr = list(map(int, input().split()))
	d = {}
	found = False
	for i in range(n):
		if arr[i] not in d:
			d[arr[i]] = i
		elif i - d[arr[i]] > 1:
			found = True
			break
	print('YES' if found else 'NO')


from collections import defaultdict
tc = int(input())
for i in range(tc):
	n = int(input())
	arr = list(map(int, input().split()))
	count = defaultdict(list)
	for i in range(0, len(arr)):
		count[arr[i]].append(i)
	ans = 0
	lis = sorted(count.items(), key=lambda x: x[1], reverse=True)
	for ele in lis:
		(u, v) = (ele[0], ele[1])
		if len(v) >= 2:
			if abs(v[-1] - v[0]) >= 2:
				ans = 1
				break
	if ans == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	a = list(map(int, input().split()))
	if n == 3:
		if a[0] == a[2]:
			print('YES')
		else:
			print('NO')
		continue
	k = 0
	for i in range(n):
		start = i
		for j in range(n - 1, i + 1, -1):
			if a[j] == a[start]:
				k = 1
				break
		if k == 1:
			break
	if k == 0:
		print('NO')
	else:
		print('YES')


from collections import Counter
ans = ''
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ra = a[::-1]
	count = Counter(a)
	many = [k for (k, v) in count.items() if v > 1]
	if not many:
		ans += 'NO\n'
		continue
	threemany = [k for (k, v) in count.items() if v > 2]
	if threemany:
		ans += 'YES\n'
		continue
	two = [k for k in many if count[k] == 2]
	flag = False
	for k in two:
		if a.index(k) + ra.index(k) != n - 2:
			flag = True
			break
	ans += 'YES\n' if flag else 'NO\n'
print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = set([a[0]])
	for i in range(2, n):
		if a[i] in s:
			print('YES')
			break
		else:
			s.add(a[i - 1])
	else:
		print('NO')


t = int(input())

def three_blocks_palindrome(arr, outer):
	if len(arr) == 0:
		return 0
	max_count = max([arr.count(i) for i in set(arr)])
	if arr.count(outer) >= 2:
		lower = arr.index(outer)
		upper = len(arr) - 1 - arr[::-1].index(outer)
		return max(max_count, three_blocks_palindrome(arr[lower + 1:upper], outer) + 2)
	else:
		return max_count
for _ in range(t):
	length = int(input())
	array = [int(i) for i in input().split()]
	best = 0
	for k in set(array):
		best = max(best, three_blocks_palindrome(array, k))
	print(best)


def finder(x, y):
	if x not in y:
		return False
	else:
		al = y[::-1]
		index = len(y) - 1 - al.index(x)
		if index != 0:
			return index
		else:
			return False
t = int(input())
while t:
	t -= 1
	n = int(input())
	l = [int(x) for x in input().split(' ')]
	if l == l[::-1]:
		print('YES')
	else:
		al = list()
		for k in range(0, len(l) - 2):
			al = l[k + 1:]
			if finder(l[k], al):
				print('YES')
				break
		else:
			print('NO')


import sys
input = sys.stdin.buffer.readline

def getCnts(j, l, r):
	if l > r:
		return 0
	if l == 0:
		return prefCnt[r][j]
	else:
		return prefCnt[r][j] - prefCnt[l - 1][j]
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	prefCnt = [[0 for _a in range(27)] for _b in range(n)]
	for j in range(1, 27):
		cnts = 0
		for i in range(n):
			if a[i] == j:
				cnts += 1
			prefCnt[i][j] = cnts
	ans = 0
	for outer in range(1, 27):
		outerLen = 0
		l = -1
		r = n
		while l < r:
			for inner in range(1, 27):
				innerLen = getCnts(inner, l + 1, r - 1)
				ans = max(ans, outerLen * 2 + innerLen)
			l += 1
			while l < n and a[l] != outer:
				l += 1
			r -= 1
			while r > -1 and a[r] != outer:
				r -= 1
			outerLen += 1
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(s) for s in input().split()]
	for i in range(0, n):
		cnt = a.count(a[i])
		if cnt >= 3:
			print('YES')
			break
		elif i != 0 and i != n - 1 and (cnt == 2) and (a[i] != a[i + 1]) and (a[i] != a[i - 1]):
			print('YES')
			break
		elif i == 0 and cnt == 2 and (a[i] != a[i + 1]):
			print('YES')
			break
		elif i == n - 1 and cnt == 2 and (a[i] != a[i - 1]):
			print('YES')
			break
	else:
		print('NO')


t = int(input())
for _ in range(t):
	a = int(input())
	nums = list(map(lambda x: int(x), input().split()))
	dp = set()
	dp.add(nums[0])
	yes = False
	for x in range(1, a):
		if nums[x] in dp:
			if nums[x - 1] != nums[x] or (x > 1 and nums[x - 2] == nums[x]):
				print('YES')
				yes = True
				break
		else:
			dp.add(nums[x])
	if yes == False:
		print('NO')


for _ in range(int(input())):
	arr = [list(map(int, input().split())) for i in 'aa'][1]
	print('YNEOS'[not any((arr[i] in arr[i + 2:] for i in range(len(arr))))::2])


t = int(input())
while t:
	t -= 1
	n = int(input())
	v = list(map(int, input().strip().split()))[:n]
	ans = 1
	w = [0] * 26
	w2 = [0] * 26
	for i in range(n):
		v[i] -= 1
		w2[v[i]] += 1
		ans = max(ans, w2[v[i]])
	i = 0
	while i < n:
		j = i + 1
		w[v[i]] += 1
		w4 = [0] * 26
		while j < n:
			w4[v[j]] += 1
			ans = max(ans, 2 * min(w[v[i]], w2[v[i]] - w4[v[i]] - w[v[i]]) + w4[v[j]])
			j += 1
		i += 1
	print(ans)


import sys
import math
for _ in range(int(input())):
	x = int(input())
	l = list(map(int, input().split()))
	al = [0] * 5001
	flag = 0
	for i in l:
		al[i] += 1
	for i in range(len(al)):
		if al[i] > 2:
			flag = 1
			break
		elif al[i] == 2:
			x = []
			for j in range(len(l)):
				if l[j] == i:
					x.append(j)
			if x[1] - x[0] > 1:
				flag = 1
				break
	if flag == 1:
		print('YES')
	else:
		print('NO')


t = int(input().strip())
for _ in range(t):
	n = int(input().strip())
	arr = list(map(int, input().strip().split()))
	visited = set([arr[0]])
	for i in range(2, n):
		if arr[i] in visited:
			print('YES')
			break
		visited.add(arr[i - 1])
	else:
		print('NO')


I = input
pr = print

def solve():
	l = int(I())
	ar = list(map(int, I().split()))
	for i in range(l - 2):
		try:
			if ar.index(ar[i], i + 2) - i > 1:
				pr('YES')
				return
		except:
			pass
	pr('NO')
for _ in range(int(I())):
	solve()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n < 3:
		print('NO')
	else:
		freq = {}
		m = 1
		for x in a:
			if x in freq:
				freq[x] += 1
			else:
				freq[x] = 1
			if m < freq[x]:
				m = freq[x]
		if m == 1:
			print('NO')
		elif m > 2:
			print('YES')
		else:
			x = 0
			while x < n:
				if freq[a[x]] == 2:
					if a[x] != a[x + 1]:
						print('YES')
						break
					else:
						x += 1
				x += 1
			else:
				print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	x = {}
	for i in range(len(a)):
		if a[i] in x:
			x[a[i]].append(i)
		else:
			x[a[i]] = [i]
	l = []
	z = 0
	for (i, j) in x.items():
		if len(j) >= 3:
			z = 1
			break
		elif len(j) == 2:
			l.append(j)
	if z == 0:
		if l == []:
			print('NO')
		else:
			for (i, j) in l:
				if abs(j - i) != 1:
					print('YES')
					break
			else:
				print('NO')
	else:
		print('YES')


n = int(input())
for i in range(n):
	m = int(input())
	X = input().split()
	B = False
	for k in range(m):
		p = X[k]
		for j in range(k + 2, m):
			if X[j] == p:
				B = True
				print('YES')
				break
		if B:
			break
	else:
		print('NO')


import sys
import string
input = sys.stdin.buffer.readline
t = int(input())
maxD = 26
for tt in range(t):
	n = int(input())
	pos = [[] for i in range(maxD)]
	a = [int(el) - 1 for el in input().split()]
	s = [[0 for i in range(n + 1)] for d in range(maxD)]
	for (i, el) in enumerate(a):
		pos[el].append(i)
		for d in range(maxD):
			s[d][i] = s[d][i - 1]
			if el == d:
				s[d][i] += 1
	m = 0
	for i in range(maxD):
		for k in range(len(pos[i]) // 2):
			l = pos[i][k]
			r = pos[i][-k - 1]
			for j in range(maxD):
				m = max(m, 2 * (k + 1) + max(0, s[j][r - 1] - s[j][l]))
	print(max(m, 1))


def one():
	return int(input())

def more():
	return map(int, input().split())
for _ in range(one()):
	n = one()
	l = list(more())
	index = {}
	d = {}
	for (ind, i) in enumerate(l):
		if not i in index:
			index[i] = ind
		else:
			d[i] = ind - index[i]
	ans = False
	for i in d:
		if d[i] > 1:
			print('YES')
			ans = True
			break
	if not ans:
		print('NO')


def tetris(x):
	s = x[0] % 2
	for i in range(1, len(x)):
		if x[i] % 2 != s:
			return 'NO'
	return 'YES'

def palindrome(x):
	u = [0] * len(x)
	u[x[0] - 1] = 1
	j = False
	for i in range(1, len(x)):
		if x[i] != x[i - 1] or j:
			u[x[i] - 1] += 1
			if u[x[i] - 1] == 2:
				return 'YES'
			j = False
		else:
			j = True
	return 'NO'
cases = int(input())
for _ in range(cases):
	l = int(input())
	x = list(map(int, input().split()))
	print(palindrome(x))


import math
import sys
from collections import defaultdict, Counter, deque, OrderedDict
import bisect
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))

def list2d(a, b, c):
	return [[c] * b for i in range(a)]
from itertools import accumulate, groupby
import sys
import heapq
for _ in range(int(input())):
	N = int(input())
	A = alele()
	Ans = max([len(list(hh)) for (h, hh) in groupby(A)])
	G = defaultdict(list)
	temp = [0] * 201
	dp = []
	dp.append(temp.copy())
	for i in range(N):
		temp[A[i]] += 1
		G[A[i]].append(i)
		dp.append(temp.copy())
	mark = {}
	for i in range(N):
		maxi = dp[i + 1][A[i]]
		a = [A[i]]
		for k in a:
			if mark.get((k, maxi), -1) == -1:
				mark[k, maxi] = 1
				j = G[k][len(G[k]) - maxi]
				if j <= i:
					break
				maxi2 = 0
				for l in range(1, 201):
					if dp[j][l] - dp[i + 1][l] > maxi2:
						maxi2 = dp[j][l] - dp[i + 1][l]
				if maxi2 == 0:
					break
				Ans2 = maxi * 2 + maxi2
				Ans = max(Ans, Ans2)
	print(Ans)


t = int(input())
i = 0
while i < t:
	n = int(input())
	arr = list(map(int, input().split()))
	has = {}
	ans = 'NO'
	for x in range(n):
		if arr[x] not in has.keys():
			has[arr[x]] = x
		elif x - has[arr[x]] + 1 >= 3:
			ans = 'YES'
			break
	print(ans)
	i = i + 1


t = int(input())
for _ in range(t):
	n = int(input())
	A = [int(x) for x in input().split()]
	Dict = {}
	for i in range(n):
		try:
			Dict[A[i]].append(i)
		except:
			Dict[A[i]] = [i]
	flag = False
	for i in Dict.keys():
		if len(Dict[i]) >= 2 and Dict[i][-1] - Dict[i][0] >= 2:
			flag = True
			break
	if flag:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	dic = {}
	ans = 'NO'
	for i in range(n):
		if a[i] not in dic:
			dic[a[i]] = i
		elif i - dic[a[i]] + 1 >= 3:
			ans = 'YES'
			break
	print(ans)


t = int(input())
for _ in range(t):
	n = input()
	arr = list(map(int, input().split()))
	flag = 'NO'
	for i in range(len(arr)):
		if arr[i] in arr[i + 2:]:
			flag = 'YES'
	print(flag)


from math import *
from sys import *
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	d = {}
	f = 0
	for i in range(n):
		if a[i] in d:
			if i - d[a[i]] > 1:
				f = 1
				break
		else:
			d[a[i]] = i
	if f == 1:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
import string
T = int(stdin.readline())
for i in range(T):
	n = int(stdin.readline())
	a = list(stdin.readline().split())
	if len(a) == 1:
		stdout.write('1' + '\n')
		continue
	mx = 0
	for ch in range(1, 27):
		others = [0] * 26
		poses = []
		cur = 0
		for i in range(len(a)):
			if a[i] == str(ch):
				cur += 1
				poses.append(i)
		mx = max(len(poses), mx)
		if len(poses) < 2:
			continue
		localMx = 0
		start = poses[len(poses) // 2 - 1]
		end = poses[(len(poses) + 1) // 2 + 1 - 1]
		for x in a[start + 1:end]:
			others[int(x) - 1] += 1
			localMx = max(others[int(x) - 1], localMx)
			mx = max(mx, localMx + len(poses) - len(poses) % 2)
		for i in range(1, len(poses) // 2):
			newStart = poses[len(poses) // 2 - i - 1]
			newEnd = poses[(len(poses) + 1) // 2 + 1 + i - 1]
			for x in a[newStart + 1:start]:
				others[int(x) - 1] += 1
				localMx = max(others[int(x) - 1], localMx)
			for x in a[end + 1:newEnd]:
				others[int(x) - 1] += 1
				localMx = max(others[int(x) - 1], localMx)
			mx = max(mx, localMx + len(poses) - len(poses) % 2 - i * 2)
			start = newStart
			end = newEnd
	stdout.write(str(mx) + '\n')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	raj = dict()
	for i in range(n):
		if raj.get(arr[i]) == None:
			raj[arr[i]] = [i + 1]
		else:
			raj[arr[i]].append(i + 1)
	flg = 0
	for i in raj:
		if flg == 1:
			break
		chk = raj.get(i)
		for j in range(len(chk)):
			if chk[-1] - chk[0] > 1 or len(chk) > 2:
				flg = 1
				break
	note = []
	for i in raj:
		chk = raj.get(i)
		if len(chk) > 1:
			for j in range(1, len(chk)):
				if chk[j] - chk[j - 1] == 1:
					note.append((chk[j - 1], chk[j]))
	for i in raj:
		chk = raj.get(i)
		if len(chk) > 1:
			left = chk[0]
			right = chk[-1]
			for j in note:
				l = j[0]
				r = j[1]
				if left < l and right > r:
					flg = 1
					break
		if flg == 1:
			break
	if flg:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(n, ans) = (int(input()), 'NO')
	lis = list(map(int, input().split()))
	for (i, e) in enumerate(lis):
		if e in lis[i + 2:]:
			ans = 'YES'
			break
	print(ans)


T = int(input())
for t in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	repeats = [[0] * N for _ in range(26)]
	indices = [[] for _ in range(26)]
	for i in range(0, N):
		if i > 0:
			for j in range(26):
				repeats[j][i] = repeats[j][i - 1]
		repeats[A[i] - 1][i] += 1
		indices[A[i] - 1].append(i)
	result = 1
	for i1 in range(len(A)):
		c = repeats[A[i1] - 1][i1]
		i2 = indices[A[i1] - 1][-c]
		if i2 > i1:
			mr = max((repeats[j0][i2 - 1] - repeats[j0][i1] for j0 in range(26)))
			result = max(result, 2 * c + mr)
	print(result)


t = int(input())
while t:
	t = t - 1
	n = int(input())
	flag = 0
	l = list(map(int, input().split()))
	for i in range(0, n):
		if l.count(l[i]) > 1:
			j = n - 1
			while j > i:
				if l[j] == l[i] and j - i >= 2:
					print('YES')
					flag = 1
					break
				j = j - 1
		if flag == 1:
			break
	if flag == 0:
		print('NO')


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	st = 'NO'
	arr = list(map(int, input().split()))
	d = defaultdict(list)
	for i in range(len(arr)):
		d[arr[i]].append(i)
	for j in d.values():
		if len(j) >= 3:
			st = 'YES'
			break
		if len(j) == 2 and j[0] != j[1] - 1:
			st = 'YES'
			break
	print(st)


import sys
RI = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
ts = int(ri())
for _ in range(ts):
	n = int(ri())
	a = RI()
	flag = False
	dic = {}
	for i in range(len(a)):
		if a[i] not in dic:
			dic[a[i]] = i
		elif i - dic[a[i]] >= 2:
			flag = True
			break
	if flag:
		print('YES')
	else:
		print('NO')


import sys
import os
import time
import collections
from collections import Counter, deque
import itertools
import math
import timeit
import random
import string

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	divisors = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				divisors.append(i)
			else:
				divisors.extend([i, n // i])
	return divisors

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def flin(d, x, default=-1):
	left = right = -1
	for i in range(len(d)):
		if d[i] == x:
			if left == -1:
				left = i
			right = i
	if left == -1:
		return (default, default)
	else:
		return (left, right)

def ceil(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def prr(a, sep=' '):
	print(sep.join(map(str, a)))

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)
input = sys.stdin.readline
res = []
for _ in range(ii()):
	n = ii()
	d = li()
	ans = 'NO'
	p = ddl()
	for i in range(n):
		p[d[i]].append(i)
	ans = 'NO'
	for k in p.values():
		if len(k) > 1 and k[-1] - k[0] > 1:
			ans = 'YES'
			break
	res.append(ans)
prr(res, '\n')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = set()
	s.add(l[0])
	out = False
	pivot = l[1]
	for i in l[2:]:
		if i in s:
			print('YES')
			out = True
			break
		else:
			s.add(pivot)
			pivot = i
	if out == False:
		print('NO')


result = []
t = int(input())
for _ in range(0, t):
	n = int(input())
	line = input().split()
	pal = {}
	for index in range(0, len(line)):
		if int(line[index]) in pal.keys():
			pal[int(line[index])].append(index)
		else:
			pal[int(line[index])] = []
			pal[int(line[index])].append(index)
	found = 0
	for i in pal.keys():
		if len(pal[i]) > 1:
			if pal[i][-1] - pal[i][0] > 1:
				result.append('YES')
				found = 1
				break
	if found == 0:
		result.append('NO')
for i in result:
	print(i)


from sys import stdin

def solve(tc):
	n = int(stdin.readline().strip())
	seq = list(map(int, stdin.readline().split()))
	nums = [[] for i in range(200)]
	for i in range(n):
		nums[seq[i] - 1].append(i)
	cum = [[0 for i in range(200)] for j in range(n + 1)]
	for i in range(n):
		cum[i + 1][seq[i] - 1] += 1
		for j in range(200):
			cum[i + 1][j] += cum[i][j]
	ans = 1
	for x in range(200):
		for y in range(200):
			l = 0
			r = len(nums[x]) - 1
			xsize = 2
			while l < r:
				ysize = cum[nums[x][r]][y] - cum[nums[x][l] + 1][y]
				ans = max(ans, xsize + ysize)
				l += 1
				r -= 1
				xsize += 2
	print(ans)
LOCAL_TEST = not __debug__
if LOCAL_TEST:
	infile = __file__.split('.')[0] + '-test.in'
	stdin = open(infile, 'r')
tcs = int(stdin.readline().strip())
tc = 1
while tc <= tcs:
	solve(tc)
	tc += 1


a = int(input())
for i in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	e = 0
	for j in range(b - 2):
		if e == 1:
			break
		d = c[j]
		for k in range(j + 2, b):
			if c[k] == d:
				print('YES')
				e = 1
				break
	if e == 0:
		print('NO')


from sys import stdin
try:
	for _ in range(int(stdin.readline())):
		n = int(stdin.readline())
		arr = list(map(int, stdin.readline().rstrip().split()))
		flag = 0
		for i in range(n):
			if arr[i] in arr[i + 2:]:
				flag = 1
				break
		if flag:
			print('YES')
		else:
			print('NO')
except:
	pass


t = int(input())
for i in range(t):
	n = int(input())
	a = input().split()
	for j in range(n):
		if a[j] in a[j + 2:]:
			ans = 'YES'
			break
	else:
		ans = 'NO'
	print(ans)


n = int(input())
for _ in range(n):
	m = int(input())
	s = set()
	mat = list(map(int, input().split()))
	for i in range(1, m):
		if mat[i] in s:
			print('YES')
			break
		s.add(mat[i - 1])
	else:
		print('NO')


from collections import defaultdict, Counter
for _ in range(int(input())):
	N = int(input())
	X = list(map(int, input().split()))
	Check = False
	MyCounter = Counter(X)
	for i in MyCounter.values():
		if i >= 3:
			Check = True
			break
	if Check:
		print('YES')
		continue
	Temp = [X[0]]
	for i in X:
		if Temp[-1] != i:
			Temp.append(i)
	MyDict = defaultdict(int)
	for i in Temp:
		MyDict[i] += 1
		if MyDict[i] == 3:
			Check = True
			break
		if MyDict[i] == 2 and len(MyDict) >= 2:
			Check = True
			break
	print('YES' if Check else 'NO')


for _ in range(int(input())):
	n = int(input())
	s = [*map(int, input().split())]
	flag = False
	for i in s:
		if n - 1 - s[::-1].index(i) > s.index(i) + 1:
			flag = True
			break
	print('YES' if flag else 'NO')


import collections
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	table = collections.defaultdict(list)
	table2 = collections.defaultdict(list)
	for x in a:
		table[x].append([len(table[j]) for j in range(1, 27)])
	for x in a[::-1]:
		table2[x].append([len(table2[j]) for j in range(1, 27)])
	ans = 0
	for x in table:
		ans = max(ans, len(table[x]))
	for x in table:
		for y in range(26):
			if x == y + 1:
				continue
			(l, r) = (0, 0)
			L = len(table[x])
			while l + r <= L - 1:
				if table[x][l][y] == table2[x][r][y]:
					ans = max(ans, table[x][l][y] * 2 + L - l - r)
					l += 1
				elif table[x][l][y] > table2[x][r][y]:
					ans = max(ans, table2[x][r][y] * 2 + L - l - r)
					r += 1
				else:
					ans = max(ans, table[x][l][y] * 2 + L - l - r)
					l += 1
	print(ans)


import sys
input = sys.stdin.buffer.readline
MX = 200

def solve(a):
	pref_count = [[0] * (MX + 1)]
	for e in a:
		pref_count.append(pref_count[-1][:])
		pref_count[-1][e] += 1
	indexes = [[] for _ in range(MX + 1)]
	for i in range(len(a)):
		indexes[a[i]].append(i)
	best = max((pref_count[-1][x] for x in range(1, MX + 1)))
	for x in range(1, MX + 1):
		for i in range(len(indexes[x]) // 2):
			l = indexes[x][i]
			r = indexes[x][len(indexes[x]) - 1 - i]
			for y in range(1, MX + 1):
				if x != y:
					best = max(best, (i + 1) * 2 + pref_count[r + 1][y] - pref_count[l][y])
	return best
tc = int(input())
res = []
for _ in range(tc):
	n = int(input())
	a = list(map(int, input().split()))
	res.append(solve(a))
print('\n'.join(map(str, res)))


cases = int(input().strip())
for case in range(cases):
	n = int(input().strip())
	numbers = [int(x) for x in input().strip().split(' ')]
	has_palindrome = False
	numbers_idx = {}
	for (idx, number) in enumerate(numbers):
		if number not in numbers_idx:
			numbers_idx[number] = idx
		elif idx - numbers_idx[number] > 1:
			has_palindrome = True
			break
	if has_palindrome:
		print('YES')
	else:
		print('NO')


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	d = {i: [] for i in range(1, 26 + 1)}
	ac = [0 for i in range(26 + 1)]
	ach = []
	for (i, ai) in enumerate(a):
		d[ai].append(i)
		ac[ai] += 1
		ach.append(ac.copy())
	maxs = max(ac)
	for i in range(1, 26 + 1):
		j = 0
		pos = d[i]
		k = len(pos) - 1
		while j < k:
			x = ach[pos[j]]
			y = ach[pos[k]]
			for l in range(len(x)):
				y[l] -= x[l]
			y[i] -= 1
			maxs = max(maxs, max(y) + (j + 1) * 2)
			j += 1
			k -= 1
	print(maxs)

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()


t = int(input())
for i in range(t):
	n = int(input())
	myl = []
	myl = list(map(int, input().strip().split()))[:n]
	qq = 0
	d = dict()
	ind = 0
	found = False
	fdict = dict()
	ldict = dict()
	for i in myl:
		if fdict.get(i) is None:
			fdict[i] = ind
			ldict[i] = ind
		else:
			ldict[i] = ind
		ind += 1
	found = False
	for i in myl:
		if ldict[i] - fdict[i] >= 2:
			found = True
			break
	if found:
		print('YES')
	else:
		print('NO')


import sys
from array import array
import typing as Tp

def input():
	return sys.stdin.buffer.readline().decode('utf-8')

def main():
	t = int(input())
	ans = [0] * t
	acc = [array('i', [0]) * (2 * 10 ** 5 + 1) for _ in range(200)]
	for ti in range(t):
		n = int(input())
		a = (-1,) + tuple(map(lambda x: int(x) - 1, input().split())) + (-1,)
		for (i, v) in enumerate(a[1:-1], start=1):
			for j in range(200):
				acc[j][i] = acc[j][i - 1]
			acc[v][i] += 1
		keys = [i for i in range(200) if acc[i][n]]
		maxv = max((acc[i][n] for i in keys))
		for x in keys:
			(l, r, outside) = (0, n + 1, 1)
			while l < r:
				while l < r and a[l] != x:
					l += 1
				while l < r and a[r] != x:
					r -= 1
				if l == r:
					break
				inside = 0
				for k in keys:
					inside = max(inside, acc[k][r - 1] - acc[k][l])
				maxv = max(maxv, outside * 2 + inside)
				(l, r, outside) = (l + 1, r - 1, outside + 1)
		ans[ti] = maxv
	sys.stdout.buffer.write(('\n'.join(map(str, ans)) + '\n').encode('utf-8'))
main()


def pp(l, n):
	if n == 3:
		if l[0] == l[2]:
			return 'YES'
		else:
			return 'NO'
	arr = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		arr[i][i] = 1
	for le in range(2, n + 1):
		for i in range(n - le + 1):
			j = i + le - 1
			if l[i] != l[j]:
				arr[i][j] = max(arr[i + 1][j], arr[i][j - 1])
			elif le == 2 and l[i] == l[j]:
				arr[i][j] = 2
			else:
				arr[i][j] = arr[i + 1][j - 1] + 2
	for i in range(n):
		for j in range(i, n):
			if arr[i][j] > 2:
				return 'YES'
	return 'NO'
ts = int(input())
for t in range(ts):
	n = int(input())
	l = list(map(int, input().split()))
	print(pp(l, n))


from collections import Counter
t = int(input())
for case in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	m = -1
	seen = set()
	for start in range(n):
		target = a[start]
		if target in seen:
			continue
		seen.add(target)
		i = start
		j = n - 1
		x = 0
		while i < j:
			while i < j and a[i] != target:
				i += 1
			while i < j and a[j] != target:
				j -= 1
			if i < j and a[i] == target and (a[j] == target):
				c = Counter(a[i + 1:j]).most_common(1)
				x += 1
				if c:
					y = c[0][1]
				else:
					y = 0
				m = max(m, 2 * x + y)
			i += 1
			j -= 1
	x = 0
	c = Counter(a).most_common(1)
	if c:
		y = c[0][1]
	else:
		y = 0
	m = max(m, 2 * x + y)
	print(m)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if arr[i] in d:
			d[arr[i]].append(i)
		else:
			d[arr[i]] = [i]
	f = 0
	for i in d:
		x = len(d[i])
		cur = d[i][0]
		for j in range(1, x):
			if abs(cur - d[i][j]) >= 2:
				f = 1
				break
		if f == 1:
			break
	if f == 1:
		print('YES')
	else:
		print('NO')


def main():
	from collections import Counter, defaultdict
	Q = int(input())
	for query in range(Q):
		_ = int(input())
		A = [int(i) for i in input().split()]
		c = Counter(A)
		flag = False
		dic = defaultdict(list)
		for (i, a) in enumerate(A):
			if c[a] < 2:
				continue
			if c[a] > 2:
				flag = True
				break
			dic[a].append(i)
			if len(dic[a]) > 1:
				if abs(dic[a][0] - dic[a][1]) > 1:
					flag = True
			if flag:
				break
		if flag:
			print('YES')
		else:
			print('NO')
main()


from collections import defaultdict
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	N = int(input())
	L = list(map(int, input().split()))
	L.insert(0, 0)
	M = 0
	Hash = defaultdict(lambda : 0)
	for i in L:
		Hash[i] += 1
	for i in range(27):
		M = max(M, Hash[i])
	for i in range(1, 27):
		if Hash[i] != 0:
			for j in range(1, 27):
				if Hash[j] != 0 and i != j and (M < Hash[i] + Hash[j]):
					prefix = [0] * (N + 2)
					prefix[0] = 0
					for k in range(1, N + 1):
						if L[k] == j:
							prefix[k] = prefix[k - 1] + 1
						else:
							prefix[k] = prefix[k - 1]
					f = 1
					b = N
					countF = 0
					countB = 0
					while f < b + 1:
						M = max(M, 2 * min(countF, countB) + prefix[b] - prefix[f - 1])
						FLAG1 = 0
						FLAG = 0
						if L[f] == i:
							FLAG = 1
							countF += 1
							f += 1
						if L[b] == i:
							FLAG1 = 1
							countB += 1
							b -= 1
						elif FLAG == 1 or FLAG1 == 1:
							continue
						elif countF > countB:
							b -= 1
						elif countB > countF:
							f += 1
						else:
							b -= 1
							f += 1
	print(M)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	d = {}
	flag = False
	for (idx, i) in enumerate(arr):
		if d.get(i) == None:
			d[i] = idx
		elif idx - d[i] >= 2:
			flag = True
			break
	print('YES' if flag else 'NO')


def inp_n():
	return int(input())

def inp_list():
	return list(map(int, input().split()))

def inp_mul_num():
	return map(int, input().split())

def is_odd(n):
	if n & 1:
		return True
	return False

def is_prime(n):
	i = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += 1
	return True
from collections import defaultdict
import math
for _ in range(inp_n()):
	n = inp_n()
	l = inp_list()
	f = 0
	for i in range(n - 2):
		for j in range(i + 2, n):
			if l[i] == l[j]:
				f = 1
	print('YES' if f == 1 else 'NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	candidate = {}
	for i in range(len(arr)):
		if arr[i] in candidate:
			candidate[arr[i]].append(i)
		else:
			candidate[arr[i]] = [i]
	Found = False
	for i in candidate:
		if Found:
			break
		if len(candidate[i]) > 1 and candidate[i][-1] - candidate[i][0] > 1:
			Found = True
			break
	if Found:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
from collections import Counter

def ai():
	return list(map(int, input().split()))

def ei():
	return map(int, input().split())

def ip():
	return int(stdin.readline())

def op(ans):
	return stdout.write(str(ans) + '\n')

def sv():
	n = ip()
	li = ai()
	flag = 0
	for i in range(n - 2):
		for j in range(n - 1, i + 1, -1):
			if li[i] == li[j]:
				print('YES')
				flag = 1
				break
		if flag:
			break
	if not flag:
		print('NO')
TC = ip()
for tc in range(TC):
	sv()


import math
import sys
import collections
input = sys.stdin.buffer.readline

def solveAll():
	nbCases = int(input())
	for _ in range(nbCases):
		case = readCase()
		print(solve(case))

def readCase():
	input()
	return [int(x) for x in input().split()]

def solve(nums):
	indices = [[] for _ in range(201)]
	for (i, num) in enumerate(nums):
		indices[num] += [i]
	bucket = buildBucket(nums)
	ans = -math.inf
	for num in range(1, 201):
		ans = max(ans, computeThreeBlock(nums, indices[num], bucket.copy()))
	return ans

def buildBucket(nums):
	if len(nums) <= 200:
		return collections.Counter(nums)
	else:
		bucket = [0] * 201
		for num in nums:
			bucket[num] += 1
		return bucket

def computeThreeBlock(nums, indices, bucket):
	le = 0
	ri = len(nums) - 1
	indicesFrom = 0
	indicesTo = len(indices) - 1
	ans = -math.inf
	while indicesFrom <= indicesTo:
		while le <= indices[indicesFrom]:
			bucket[nums[le]] -= 1
			le += 1
		while ri >= indices[indicesTo]:
			bucket[nums[ri]] -= 1
			ri -= 1
		if indicesFrom < indicesTo:
			ans = max(ans, getMaxValue(bucket, len(nums)) + 2 * (indicesFrom + 1))
		else:
			ans = max(ans, getMaxValue(bucket, len(nums)) - 1 + 2 * (indicesFrom + 1))
		indicesFrom += 1
		indicesTo -= 1
	return ans

def getMaxValue(bucket, n):
	if n > 200:
		return max(bucket)
	else:
		return max(0, max(bucket.values()))
solveAll()


t = int(input())
while t:
	n = int(input())
	a = [int(x) for x in input().split()]
	l = [0] + [-1] * n
	found = False
	for i in range(0, n):
		if l[a[i]] != -1 and l[a[i]] != i - 1:
			print('YES')
			found = True
			break
		if l[a[i]] == -1:
			l[a[i]] = i
	if not found:
		print('NO')
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	dic = {}
	for i in range(n):
		try:
			if dic[a[i]]:
				dic[a[i]].append(i)
		except:
			dic[a[i]] = [i]
	flag = ''
	for num in dic:
		if flag == 'bingo':
			break
		temp = dic[num]
		if len(temp) >= 2:
			first = temp[0]
			last = temp[-1]
			if abs(first - last) >= 2:
				for x in range(first + 1, last):
					if a[i] != temp:
						flag = 'bingo'
						break
	if flag == 'bingo':
		print('YES')
	else:
		print('NO')


from sys import stdin
for _ in range(int(stdin.readline())):
	n = int(stdin.readline().strip('\n'))
	arr = [int(x) for x in stdin.readline().split()]
	flag = False
	(backward, forward) = ({}, {})
	for i in arr:
		if i in backward:
			backward[i] += 1
		else:
			backward[i] = 1
	for i in arr:
		if flag:
			break
		backward[i] -= 1
		for j in forward:
			if backward[j] > 0:
				flag = True
				break
		if i in forward:
			forward[i] += 1
		else:
			forward[i] = 1
	print('YES' if flag else 'NO')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	pos = [-1] * (n + 1)
	f = False
	for i in range(n):
		if pos[arr[i]] == -1:
			pos[arr[i]] = i
		elif pos[arr[i]] <= i - 2:
			f = True
			break
	print('YES' if f else 'NO')


for i in range(int(input())):
	a = int(input())
	f = 0
	lst = list(map(int, input().split()))
	for (i, j) in enumerate(lst):
		if j in lst[i + 2:]:
			print('YES')
			f = 1
			break
	if f == 0:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	checked = []
	flag = 0
	for i in range(1, len(l)):
		if l[i] in checked:
			flag = 1
			break
		checked.append(l[i - 1])
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())

def palindrome(aa):
	if len(aa) < 3:
		return False
	return aa == list(reversed(aa))
for ts in range(t):
	l = int(input())
	a = [i for i in input().split()]
	f = {}
	flag = True
	for ii in range(l):
		i = a[ii]
		f[i] = f.get(i, [0, []])
		f[i][0] += 1
		f[i][1].append(ii)
		if f[i][0] == 3 or (f[i][0] == 2 and f[i][1][1] - f[i][1][0] > 1):
			flag = False
			print('YES')
			break
	if flag:
		print('NO')


tc = int(input())
for _ in range(tc):
	num = int(input())
	arr = [int(x) for x in input().split()]
	ans = 'NO'
	for i in range(num):
		for k in range(i + 2, num):
			if arr[i] == arr[k]:
				ans = 'YES'
				break
	print(ans)


numbers = 26
for _ in range(int(input())):
	n = int(input())
	seq = [int(x) - 1 for x in input().split()]
	pre = [(n + 1) * [0] for i in range(numbers)]
	for (j, val) in enumerate(seq, 1):
		for i in range(numbers):
			if val == i:
				pre[i][j] = pre[i][j - 1] + 1
			else:
				pre[i][j] = pre[i][j - 1]
	ans = 1
	for i in range(numbers):
		max_val = pre[i][-1] // 2
		for x in range(1, 1 + max_val):
			bmin = pre[i].index(x)
			bmax = n + 1 - pre[i][::-1].index(pre[i][-1] - x)
			ymax = 0
			if bmax - bmin > 1:
				for j in range(numbers):
					ymax = max(ymax, pre[j][bmax - 1] - pre[j][bmin])
			ans = max(ans, ymax + 2 * x)
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	d = {}
	for (i, v) in enumerate(a):
		if v in d:
			d[v].append(i)
		else:
			d[v] = [i]
	for v in d:
		if len(d[v]) > 1:
			low = min(d[v])
			high = max(d[v])
			if high - low >= 2:
				print('YES')
				break
	else:
		print('NO')


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	possible = False
	find = {}
	i = 0
	while i < n:
		if a[i] not in find:
			find[a[i]] = 1
		else:
			if a[i] != a[i - 1]:
				possible = True
				break
			find[a[i]] += 1
		i += 1
	for val in find:
		if find[val] > 2:
			possible = True
			break
	if possible:
		print('YES')
	else:
		print('NO')
	t -= 1


from collections import defaultdict, Counter
for _ in range(int(input())):
	n = int(input())
	b = list(map(int, input().split()))
	d = defaultdict(list)
	for (idx, i) in enumerate(b):
		d[i].append(idx)
	freq = Counter(b)
	r = False
	for i in freq:
		if not r:
			if freq[i] >= 2:
				for j in range(1, len(d[i])):
					if d[i][j] - d[i][0] >= 2:
						r = True
						break
		else:
			break
	if r:
		print('YES')
	else:
		print('NO')


from math import *
inf = 10 ** 10
t = int(input())
for zz in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	ind = {}
	for i in range(len(a)):
		if a[i] not in ind:
			ind[a[i]] = [i]
		else:
			ind[a[i]].append(i)
	ha = False
	for k in ind.keys():
		if len(ind[k]) >= 3:
			ha = True
			break
		elif len(ind[k]) == 2:
			for i in range(1, len(ind[k])):
				if ind[k][i] - ind[k][i - 1] > 1:
					ha = True
					break
		if ha:
			break
	if ha:
		print('YES')
	else:
		print('NO')


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		arr = list(map(int, input().strip().split()))
		ma = {}
		for (i, ele) in enumerate(arr):
			if ele not in ma:
				ma[ele] = []
			ma[ele].append(i)
		flag = False
		for key in ma:
			if len(ma[key]) > 1:
				if ma[key][-1] - ma[key][0] > 1:
					flag = True
					break
		if flag:
			print('YES')
		else:
			print('NO')
main()


t = int(input())
for i in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	flag = False
	for i in range(n - 2):
		for j in range(i + 2, n):
			if l[j] == l[i]:
				flag = True
				break
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(j) for j in input().split()]
	p = 0
	for j in range(n):
		if a[j] in a[j + 2:]:
			p = 1
			print('YES')
			break
	if p == 0:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	ok = 1
	maxx = 0
	a = [int(x) for x in input().split()]
	for i in range(0, n - 1):
		for j in range(i + 1, n):
			if a[j] == a[i]:
				if j - i > maxx:
					maxx = j - i
	if maxx >= 2:
		print('YES')
	else:
		print('NO')


t = int(input())
for test in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	rec = {}
	for i in range(n):
		if a[i] in rec:
			rec[a[i]] += 1
		else:
			rec[a[i]] = 1
	flag = 0
	for i in range(n - 1):
		if rec[a[i]] > 2 or (rec[a[i]] == 2 and a[i + 1] != a[i]):
			flag = 1
			break
		rec[a[i]] -= 1
	print('YES' if flag else 'NO')


def main():
	n = int(input())
	lst = list(map(int, input().split()))
	dct = dict()
	prev = -1
	for i in lst:
		if not i in dct:
			dct[i] = 1
		elif prev == i and dct[i] == 1:
			dct[i] += 1
		else:
			print('YES')
			return
		prev = i
	print('NO')
t = int(input())
for i in range(t):
	main()


from sys import stdin, stdout
(_input, _print) = (stdin.readline, stdout.write)
(_range, _int, _list, _len, _str) = (range, int, list, len, str)
from collections import defaultdict

def solution():
	for _ in _range(_int(_input())):
		n = _int(_input())
		arr = [int(i) for i in input().split()]
		count = defaultdict(int)
		count[arr[0]] += 1
		for (ind, i) in enumerate(arr[1:]):
			count[i] += 1
			if count[i] >= 2 and arr[ind] != i:
				print('YES')
				break
			if count[i] == 3:
				print('YES')
				break
		else:
			print('NO')
solution()


n = int(input())
for i in range(n):
	m = int(input())
	l = list(map(int, input().split()))
	count = 0
	for j in range(m - 1):
		for k in range(j + 2, m):
			if l[j] == l[k]:
				count = 1
				break
	if count == 1:
		print('YES')
	else:
		print('NO')


def function(N, array):
	dummy = set()
	for i in range(2, N):
		dummy.add(array[i - 2])
		if array[i] in dummy:
			return 'YES'
	return 'NO'
for _ in range(int(input())):
	N = int(input())
	array = [int(x) for x in input().split()]
	print(function(N, array))


def solve(arr, n, ans):
	dp = [[0 for i in range(27)] for j in range(n)]
	for i in range(n):
		dp[i][arr[i]] += 1
		if i > 0:
			for j in range(27):
				dp[i][j] += dp[i - 1][j]
	max_val = 0
	for i in range(n):
		left = dp[i][:]
		for j in range(i + 1, n):
			total = 0
			counts = dp[j - 1][:]
			for k in range(27):
				counts[k] -= dp[i][k]
			total += max(counts)
			right = dp[-1][:]
			for k in range(27):
				right[k] -= dp[j - 1][k]
			max_curr = 0
			for k in range(27):
				max_curr = max(max_curr, 2 * min(left[k], right[k]))
			total += max_curr
			max_val = max(max_val, total)
		max_val = max(max_val, max(left))
	ans.append(max_val)

def main():
	t = int(input())
	ans = []
	for i in range(t):
		n = int(input())
		arr = list(map(int, input().split()))
		solve(arr, n, ans)
	for i in ans:
		print(i)
main()


from sys import stdin
input = stdin.readline
import bisect
import math
for xoxo in range(1):
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		b = [[] for i in range(27)]
		c = [0] * 26
		for i in range(n):
			b[a[i]].append(i)
		ans = 1
		for i in range(1, 27):
			for j in range(len(b[i]) // 2):
				c = [0] * 26
				for k in range(b[i][j] + 1, b[i][-1 * (j + 1)]):
					c[a[k] - 1] += 1
				ans = max(max(c) + (j + 1) * 2, ans)
		print(ans)


from math import ceil, gcd, floor, log
from collections import deque, defaultdict
from heapq import heappush as hpush, heappop as hpop, heapify
from functools import lru_cache
import sys

def inpi():
	return int(input())

def inpa():
	return list(map(int, input().split()))

def inp():
	s = input()
	return list(s)

def inpv():
	return map(int, input().split())
l = 'abcdefghijklmnopqrstuvwxyz'
testcase = int(input())
for test in range(testcase):
	n = inpi()
	a = inpa()
	dp = []
	for (i, j) in enumerate(a):
		if i == 0:
			d = defaultdict(int)
			d[j] += 1
			dp.append(d)
		else:
			d = dp[-1].copy()
			d[j] += 1
			dp.append(d)
	s = set(a)
	m = max(dp[-1].values())
	for x in s:
		for y in s:
			if x != y:
				i = 0
				j = n - 2
				while i <= j:
					c1 = dp[i][x]
					c2 = dp[-1][x] - dp[j][x]
					if c1 == c2:
						k1 = dp[j][y] - dp[i][y]
						m = max(c1 + c2 + k1, m)
					m = max(c1 + c2, m)
					if c1 > c2:
						j -= 1
					elif c2 > c1:
						i += 1
					else:
						i += 1
						j -= 1
	print(m)


t = int(input())
for i in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	flg = 0
	for i in range(n):
		for j in range(i + 2, n):
			if ar[i] == ar[j]:
				flg = 1
				break
	if flg == 1:
		print('YES')
	else:
		print('NO')


import sys
t = int(sys.stdin.readline())
for i in range(t):
	seen = [0] * 5100
	n = int(sys.stdin.readline())
	array = list(map(int, sys.stdin.readline().split()))
	palindrome = False
	for i in range(n):
		seen[array[i]] += 1
		if seen[array[i]] >= 3 or (seen[array[i]] == 2 and array[i - 1] != array[i]):
			palindrome = True
			break
	if palindrome:
		print('YES')
	else:
		print('NO')


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	num_list = []
	for (i, j) in enumerate(a):
		num_list.append((j, i))
	num_list.sort()
	a.sort()
	for i in range(1, n - 1):
		if a[i - 1] == a[i] == a[i + 1]:
			print('YES')
			break
	else:
		for i in range(len(num_list) - 1):
			if num_list[i][0] == num_list[i + 1][0] and num_list[i + 1][1] - num_list[i][1] > 1:
				print('YES')
				break
		else:
			print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	o = 0
	l = list(map(int, input().split()))
	for i in range(0, len(l)):
		if l[i] in l[i + 2:len(l)]:
			o = 1
			break
	print('YES') if o == 1 else print('NO')


k = int(input())
for w in range(k):
	n = input()
	numbers = [int(i) for i in input().split()]
	data = [0 for i in range(5001)]
	data[numbers[0]] += 1
	for i in range(1, len(numbers)):
		if data[numbers[i]] >= 1 and numbers[i - 1] != numbers[i]:
			data[numbers[i]] += 2
		else:
			data[numbers[i]] += 1
	flag = False
	for i in data:
		if i >= 3:
			flag = True
	if flag:
		print('YES')
	else:
		print('NO')


import math
t = int(input())
for z in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	f = False
	for i in range(n):
		for j in range(i + 2, n):
			if l[i] == l[j]:
				f = True
	if not f:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	al = list(map(int, input().split()))
	ok = False
	for i in range(n):
		for j in range(i + 2, n):
			if al[i] == al[j]:
				ok = True
				break
	if ok == True:
		print('YES')
	else:
		print('NO')


import sys
from functools import lru_cache
from heapq import merge, heapify, heappop, heappush
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
from collections import defaultdict as dd, deque, Counter as c
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var))

def outln(var):
	sys.stdout.write(str(var) + '\n')

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for _ in range(int(data())):
	n = int(data())
	arr = l()
	dp = dd(list)
	for i in range(n):
		dp[arr[i]].append(i)
	answer = 0
	for i in dp.values():
		answer = max(answer, len(i))
	for i in dp.keys():
		res = len(dp[i])
		for j in dp.keys():
			if i != j:
				if len(dp[i]) > 1:
					(left, right, side) = (0, len(dp[i]) - 1, 1)
					while left < right:
						cnt = 0
						for k in dp[j]:
							if dp[i][right] > k > dp[i][left]:
								cnt += 1
						res = max(res, 2 * side + cnt)
						side += 1
						left += 1
						right -= 1
		answer = max(answer, res)
	outln(answer)


v = int(input())
for tc in range(v):
	n = int(input())
	l = list(map(int, input().split()))
	import collections as c
	ff = c.defaultdict(lambda : [])
	for i in range(n):
		ff[l[i]].append(i)
	flag = 0
	for i in ff.keys():
		if len(ff[i]) > 1:
			if ff[i][-1] - ff[i][0] > 1:
				flag = 1
				break
	print('YES') if flag else print('NO')


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 'NO'
	for i in range(n):
		for j in range(i + 1, n):
			if arr[i] == arr[j] and j - i > 1:
				ans = 'YES'
				break
	print(ans)


t = int(input())
for k in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	sim_els = {}
	fl = 0
	for i in range(len(a)):
		if a[i] in sim_els:
			sim_els[a[i]] = sim_els[a[i]] + [i]
		else:
			sim_els[a[i]] = [i]
	for (ke, v) in sim_els.items():
		if len(v) > 2:
			fl = 1
		elif len(v) > 1:
			if abs(v[0] - v[1]) > 1:
				fl = 1
	if fl == 0:
		print('NO')
	else:
		print('YES')


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
MOD = 1000000007
MOD2 = 998244353
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
let = 'abcdefghijklmnopqrstuvwxyz'
for _ in range(ii()):
	n = ii()
	l = il()
	cntr = dict()
	mxcnt = 0
	dp = [[0 for i in range(201)] for j in range(n + 1)]
	for i in range(n - 1, -1, -1):
		for j in range(1, 201):
			dp[i][j] = dp[i + 1][j] + (l[i] == j)
		mxcnt = max(dp[i][l[i]], mxcnt)
		x = str(dp[i][l[i]]) + ',' + str(l[i])
		cntr[x] = i
	cntr2 = dict()
	for i in range(n):
		cntr2[l[i]] = cntr2.get(l[i], 0) + 1
		x = str(cntr2[l[i]]) + ',' + str(l[i])
		if x in cntr and cntr[x] > i:
			for j in range(1, 201):
				mxcnt = max(cntr2[l[i]] * 2 + (dp[i + 1][j] - dp[cntr[x]][j]), mxcnt)
	print(mxcnt)


from collections import defaultdict

def process(data):
	m = defaultdict(list)
	for (i, d) in enumerate(data):
		m[d].append(i)
	for k in m:
		lst = m[k]
		if len(lst) > 1 and lst[0] + 2 <= lst[-1]:
			return 'YES'
	return 'NO'
N = int(input())
for n in range(N):
	L = int(input())
	data = [int(a) for a in input().split()]
	print(process(data))


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	s = input().split()
	res = 'NO'
	for i in range(n - 1):
		for j in range(i + 2, n):
			if s[i] == s[j]:
				res = 'YES'
				break
	print(res)


def solve(n, d):
	indexer = []
	for i in range(5001):
		indexer.append(-1)
	for i in range(n):
		if indexer[d[i]] == -1:
			indexer[d[i]] = i
		else:
			before = indexer[d[i]]
			if before != i - 1:
				return 'YES'
	return 'NO'

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		d = input()
		d = [int(i) for i in d.split()]
		ans = solve(n, d)
		print(ans)
main()


import math
q = int(input())
for _ in range(q):
	n = int(input())
	l = list(map(int, input().split()))
	a = [0] * 5010
	for i in range(n):
		a[l[i]] += 1
	if max(a) >= 3:
		print('YES')
	else:
		flag = 0
		for i in range(n - 1):
			if a[l[i]] == 2:
				if l[i] != l[i + 1]:
					print('YES')
					flag = -1
					break
				else:
					a[l[i]] -= 1
		if flag == 0:
			print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	find = False
	for i in range(0, n - 2):
		if a[i] in a[i + 2:]:
			print('YES')
			find = True
			break
	if not find:
		print('NO')


import sys
input = sys.stdin.buffer.readline

def list_input():
	return list(map(int, input().split()))

def mult_input():
	return map(int, input().split())
for nt in range(int(input())):
	n = int(input())
	a = list_input()
	for i in range(n):
		a[i] -= 1
	dp = [[0 for i in range(200)] for i in range(n)]
	for i in range(n):
		for j in range(200):
			dp[i][j] = dp[i - 1][j]
		dp[i][a[i]] += 1
	m = 1
	for i in range(200):
		(s, e, c) = (0, n - 1, 0)
		while s < e:
			while s < e and a[s] != i:
				s += 1
			while s < e and a[e] != i:
				e -= 1
			if s < e:
				c += 2
				for j in range(200):
					m = max(m, c + dp[e - 1][j] - dp[s][j])
				s += 1
				e -= 1
	print(m)


import sys

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().strip().split()))
		d = dict()
		didbreak = False
		for i in range(n):
			if a[i] not in d:
				d[a[i]] = [1, i]
			else:
				if i - d[a[i]][1] > 1:
					didbreak = True
					break
				d[a[i]][0] += 1
		if didbreak:
			print('YES')
		else:
			print('NO')
main()


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	ls = [[] for i in range(n + 1)]
	for i in range(n):
		ls[a[i]].append(i)
	flag = False
	for i in range(n + 1):
		if len(ls[i]) > 2:
			flag = True
			break
		elif len(ls[i]) == 2:
			if ls[i][0] + 1 != ls[i][1]:
				flag = True
				break
	if flag:
		print('YES')
	else:
		print('NO')
	t -= 1


n = int(input())
nums = dict()
while n > 0:
	n -= 1
	fl = False
	m = int(input())
	a = [int(x) for x in input().split()]
	if m < 3:
		print('NO')
		continue
	for i in range(0, m):
		for j in range(i + 1, m):
			if a[j] == a[i] and i != j - 1:
				fl = True
	if fl:
		print('Yes')
	else:
		print('No')


t = t = int(input())
from collections import Counter
while t > 0:
	n = input()
	arr = [int(x) for x in input().split()]
	hashmap = {}
	for i in range(len(arr)):
		if arr[i] in hashmap:
			hashmap[arr[i]].append(i)
		else:
			hashmap[arr[i]] = [i]
	flag = False
	for val in hashmap.values():
		if len(val) != 1:
			if val[-1] - val[0] >= 2:
				print('YES')
				flag = True
				break
	if not flag:
		print('NO')
	t -= 1


for j in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = False
	arr1 = {}
	for i in range(n):
		if arr[i] in arr1:
			if i - arr1[arr[i]] > 1:
				ans = True
		else:
			arr1[arr[i]] = i
	if ans:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	for i in range(n):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				ans = 'YES'
				break
		if ans == 'YES':
			break
		else:
			ans = 'NO'
	print(ans)


import sys
import math
import copy
from collections import Counter
debug = 1
readln = sys.stdin.readline

def readint():
	return int(readln().strip())

def readints():
	return map(int, readln().split())

def readstr():
	return readln().strip()

def readstrs():
	return readln().split()

def dbg(*args):
	if debug:
		print(' '.join(map(str, args)))

def subr(i, j, cter):
	if j < i:
		return 0
	elif j == i:
		return 1
	else:
		c = cter[j] - (cter[i - 1] if i > 0 else Counter())
		return c.most_common(1)[0][1]

def solve(n, A):
	maxa = 0
	pos = [[] for _ in range(201)]
	cter = []
	for (i, a) in enumerate(A):
		pos[a].append(i)
		maxa = max(maxa, a)
		if i > 0:
			cter.append(cter[i - 1].copy())
		else:
			cter.append(Counter())
		cter[i][a] += 1
	keys = [a for a in range(len(pos)) if pos[a]]
	ans = 0
	for x in keys:
		posx = pos[x]
		lenx = len(posx)
		for i in range(len(posx) // 2):
			ans = max(ans, (i + 1) * 2 + subr(posx[i] + 1, posx[lenx - i - 1] - 1, cter))
	return max(ans, subr(0, n - 1, cter))
t = readint()
for _ in range(t):
	n = readint()
	A = readints()
	print(solve(n, A))


def func(a, k):
	maxi = 0
	for i in range(201):
		if i != a:
			maxi = max(maxi, prefix[charocc[a][-k - 1]][i] - prefix[charocc[a][k]][i])
		else:
			maxi = max(maxi, prefix[charocc[a][-k - 1]][i] - prefix[charocc[a][k]][i] - 1)
	return maxi
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	charocc = [[] for i in range(201)]
	prefix = [[0] * 201 for i in range(n)]
	for i in range(n):
		charocc[arr[i]] += [i]
	for i in range(n):
		prefix[i][arr[i]] += 1
	for i in range(1, n):
		for j in range(201):
			prefix[i][j] += prefix[i - 1][j]
	maxim = 0
	for a in range(1, 201):
		for k in range(len(charocc[a]) // 2):
			maxim = max(maxim, 2 * (k + 1) + func(a, k))
	if maxim == 0:
		print(1)
	else:
		print(maxim)


def lps(s):
	n = len(s)
	dp = [[0 for x in range(n)] for x in range(n)]
	for i in range(n):
		dp[i][i] = 1
	for el in range(2, n + 1):
		for i in range(n - el + 1):
			j = i + el - 1
			if s[i] == s[j] and el == 2:
				dp[i][j] = 2
			elif s[i] == s[j]:
				dp[i][j] = dp[i + 1][j - 1] + 2
			else:
				dp[i][j] = max(dp[i][j - 1], dp[i + 1][j])
	return dp[0][n - 1]
import math
t = int(input())
for _ in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	l = [0] * n
	dp = []
	for i in range(n):
		dp.append(l)
	for i in range(n):
		dp[i][i] = 1
	for el in range(2, n + 1):
		for i in range(n - el + 1):
			j = i + el - 1
			if s[i] == s[j] and el == 2:
				dp[i][j] = 2
			elif s[i] == s[j]:
				dp[i][j] = dp[i + 1][j - 1] + 2
			else:
				dp[i][j] = max(dp[i][j - 1], dp[i + 1][j])
	if dp[0][n - 1] < 3:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = len(a)
	ps = False
	for i in range(l - 2):
		for j in range(i + 2, l):
			if a[i] == a[j]:
				ps = True
				break
		if ps:
			break
	if ps:
		print('YES')
	else:
		print('NO')


import sys

def lower_bound(arr, num):
	ans = 10000000000.0
	(low, high) = (0, len(arr) - 1)
	ind = -1
	while low <= high:
		mid = (low + high) // 2
		if arr[mid] >= num:
			ind = mid
			ans = min(ans, arr[mid])
			high = mid - 1
		else:
			low = mid + 1
	return ind
	return ans

def upper_bound(arr, num):
	ans = -1
	ind = -1
	(low, high) = (0, len(arr) - 1)
	while low <= high:
		mid = (low + high) // 2
		if arr[mid] <= num:
			ans = max(ans, arr[mid])
			ind = mid
			low = mid + 1
		else:
			high = mid - 1
	return ind
	return ans
from collections import defaultdict
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	dic = defaultdict(list)
	arr = list(map(int, sys.stdin.readline().split()))
	for i in range(n):
		dic[arr[i]].append(i)
	ans = 0
	for i in dic:
		ans = max(ans, len(dic[i]))
	for i in dic:
		m = len(dic[i])
		for j in range(m // 2):
			(left, right) = (dic[i][j], dic[i][m - j - 1])
			for k in dic:
				if k != i:
					low = lower_bound(dic[k], left)
					high = upper_bound(dic[k], right)
					if low <= high and low != -1 and (high != -1):
						ans = max(ans, high - low + 1 + (j + 1) * 2)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	seq = [int(x) for x in input().split()]
	ans = 'NO'
	for (i, val) in enumerate(seq[:-2]):
		for other_val in seq[i + 2:]:
			if val == other_val:
				ans = 'YES'
				break
	print(ans)


import sys
get_string = lambda : sys.stdin.readline().strip()
get_int_list = lambda : list(map(int, sys.stdin.readline().strip().split()))
get_int = lambda : int(sys.stdin.readline())
for _ in range(get_int()):
	n = get_int()
	lst = get_int_list()
	if len(lst) <= 2:
		print('NO')
	else:
		flag = False
		for i in lst:
			LI = lst.index(i)
			RI = n - lst[::-1].index(i) - 1
			if RI >= LI + 2:
				flag = True
				break
		if flag:
			print('YES')
		else:
			print('NO')


from operator import itemgetter
import sys
input = sys.stdin.readline
MAX_A = 200
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ruiseki = [[0] * MAX_A for i in range(n + 1)]
	for i in range(n):
		for j in range(MAX_A):
			ruiseki[i + 1][j] = ruiseki[i][j]
			if a[i] - 1 == j:
				ruiseki[i + 1][j] += 1
	ans = 0
	for num in set(a):
		l = 0
		r = n - 1
		cnt = 0
		while True:
			while True:
				if a[l] != num:
					l += 1
				else:
					break
			while True:
				if a[r] != num:
					r -= 1
				else:
					break
			if l == r:
				ans = max(ans, cnt + 1)
				break
			elif l > r:
				break
			cnt += 1
			tmp_max = 0
			for j in range(MAX_A):
				tmp_max = max(tmp_max, ruiseki[r][j] - ruiseki[l + 1][j])
			ans = max(ans, cnt * 2 + tmp_max)
			l += 1
			r -= 1
	print(ans)


a = int(input())
for i in range(a):
	flag = 0
	b = int(input())
	c = input().split(' ')
	for j in range(b):
		if c[j] in c[j + 2:]:
			flag += 1
	if flag >= 1:
		print('YES')
	else:
		print('NO')


def g():
	n = int(input())
	l = [int(x) for x in input().split()]
	for i in range(n - 1):
		for j in range(i + 2, n):
			if l[i] == l[j]:
				return 'YES'
	return 'NO'
test_case = int(input())
for ___ in range(test_case):
	print(g())


t = int(input())
for i in range(t):
	n = int(input())
	status = 0
	a = list(map(int, input().split()))
	for j in range(n):
		for k in range(j + 1, n):
			if a[k] == a[j] and k - j != 1:
				status = 1
				break
	if status == 1:
		print('YES')
	else:
		print('NO')


import io
import os
from collections import Counter, defaultdict
DEBUG = False

def solve(N, A):
	indices = defaultdict(list)
	cumCounts = [[0 for i in range(200)]]
	for (i, x) in enumerate(A):
		cumCounts.append(list(cumCounts[-1]))
		cumCounts[-1][x] += 1
		indices[x].append(i)
	best = max(Counter(A).values())
	for (i, a) in enumerate(A):
		aLen = cumCounts[i][a] + 1
		j = indices[a][-aLen]
		if DEBUG:
			assert A[:i + 1].count(a) == aLen
			assert A[j:].count(a) == aLen
		if i < j:
			for b in range(200):
				if a != b:
					bLen = cumCounts[j][b] - cumCounts[i + 1][b]
					if DEBUG:
						assert A[i + 1:j].count(b) == bLen
					best = max(best, 2 * aLen + bLen)
	return best
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
T = int(input())
for t in range(T):
	(N,) = [int(x) for x in input().split()]
	A = [int(x) - 1 for x in input().split()]
	ans = solve(N, A)
	print(ans)


import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
from functools import lru_cache
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
if os.path.exists('out.txt'):
	sys.stdout = open('out.txt', 'w')

def input():
	return sys.stdin.readline()

def mapi(arg=0):
	return map(int if arg == 0 else str, input().split())
for _ in range(int(input())):
	n = int(input())
	a = list(mapi())
	freq = defaultdict(list)
	cur = [0] * 200
	B = [list(cur)]
	for i in range(n):
		cur[a[i]] += 1
		B.append(list(cur))
		freq[a[i]].append(i)
	ans = max((len(i) for i in freq.values()))
	for i in range(200):
		tmp = freq[i]
		for j in range(len(tmp) // 2):
			D1 = B[tmp[j] + 1]
			D2 = B[tmp[~j]]
			for k in range(200):
				ans = max(ans, 2 * (j + 1) + D2[k] - D1[k])
	print(ans)


from collections import defaultdict
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	d = defaultdict(lambda : list())
	for i in range(n):
		d[a[i]].append(i)
	q = 0
	for v in d.values():
		k = len(v)
		if v[k - 1] - v[0] >= 2:
			q = 1
			break
	if q == 1:
		print('YES')
	else:
		print('NO')


from collections import defaultdict, deque
from itertools import permutations, product, combinations
from functools import lru_cache
from time import time
import sys
from operator import itemgetter
start = time()

def show_time():
	print(time() - start, file=sys.stderr)
try:
	f = open('test.in')

	def ints():
		return map(int, f.readline().strip().split())

	def one_int():
		return int(f.readline())

	def one_string():
		return f.readline().strip()

	def strings():
		return f.readline().strip().split()
except FileNotFoundError:

	def ints():
		return map(int, input().strip().split())

	def one_int():
		return int(input())

	def one_string():
		return input().strip()

	def strings():
		return input().strip().split()

def debug(s):
	print(s, file=sys.stderr)
sys.stderr.write('\n')
from collections import Counter

def ans(numbers, rev_numbers, a, c):
	r = max(c.values())
	for i in range(0, 1 + c[a] // 2):
		(ia, rev_ia) = set_indices(a, i, numbers, rev_numbers)
		if (ia, rev_ia) == (-1, -1):
			continue
		if rev_ia == 0:
			sub_list = numbers[ia:]
		else:
			sub_list = numbers[ia:-rev_ia]
		if sub_list:
			sub_counter = Counter(sub_list)
			r = max(r, 2 * i + max(sub_counter.values()))
		else:
			r = max(r, 2 * i)
	return r

@lru_cache(maxsize=None)
def set_indices(a, i, numbers, rev_numbers):
	if i == 0:
		return (0, 0)
	(ia, rev_ia) = set_indices(a, i - 1, numbers, rev_numbers)
	try:
		ia += numbers[ia:].index(a)
		ia += 1
		rev_ia += rev_numbers[rev_ia:].index(a)
		rev_ia += 1
	except ValueError:
		(ia, rev_ia) = (-1, -1)
	if ia + rev_ia > len(numbers):
		(ia, rev_ia) = (-1, -1)
	return (ia, rev_ia)
t = one_int()
for _ in range(t):
	n = one_int()
	numbers = tuple(list(ints()))
	rev_numbers = numbers[::-1]
	c = Counter(numbers)
	r = max(c.values())
	for a in set(numbers):
		r = max(r, ans(numbers, rev_numbers, a, c))
	print(r)


for t in range(int(input())):
	coluna = int(input())
	lista = list(map(int, input().split()))
	dic = {}
	string = 'NO'
	for k in range(len(lista)):
		dic.setdefault(lista[k], [])
		dic[lista[k]].append(k)
	for x in dic:
		if len(dic[x]) >= 3:
			string = 'YES'
			break
	for x in dic:
		if len(dic[x]) == 2 and dic[x][0] != dic[x][1] - 1:
			string = 'YES'
			break
	print(string)


t = int(input())
for i in range(t):
	n = int(input())
	ok = False
	arr = [int(ele) for ele in input().split()]
	for i in range(n):
		for j in range(i + 2, n):
			if arr[i] == arr[j]:
				ok = True
	if ok:
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline

def pal(n, x):
	if n < 3:
		return False
	for i in range(n - 2):
		y = x[i]
		if y in x[i + 2:]:
			return True
	return False
for _ in range(int(input())):
	n = int(input())
	x = [*map(int, input().split())]
	print('YES' if pal(n, x) else 'NO')


from sys import stdin, stdout
import math
import heapq
t = 1

def aint():
	return int(input().strip())

def lint():
	return list(map(int, input().split()))

def fint():
	return list(map(int, stdin.readline().split()))

def main():
	n = aint()
	l = lint()
	ans = 'NO'
	for i in range(n):
		for j in range(i + 2, n):
			if l[i] == l[j]:
				ans = 'YES'
				break
	print(ans)
t = int(input())
for i in range(t):
	main()


t = int(input())
for q in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	sl = [[] for i in range(201)]
	ss = [[0] for i in range(201)]
	for i in range(n):
		sl[l[i]].append(i)
		for j in range(201):
			ss[j].append(ss[j][-1])
		ss[l[i]][-1] += 1
	ans = 0
	for i in range(200):
		if not sl[i]:
			continue
		if len(sl[i]) == 1:
			ans = max(ans, 1)
			continue
		for j in range(len(sl[i]) // 2):
			le = sl[i][j]
			ri = sl[i][-1 - j]
			for k in range(200):
				if i == k:
					ans = max(ans, len(sl[i]))
					continue
				if sl[k]:
					ans = max(ans, 2 * (j + 1) + ss[k][ri] - ss[k][le])
	print(ans)


test_cases = int(input())
test = test_cases
data = []
test_case_length = []
while test_cases > 0:
	test_case_length.append(input())
	data.append(input().split())
	test_cases -= 1
for i in range(0, test):
	j = 0
	while j < int(test_case_length[i]) - 2:
		k = j + 2
		while k < int(test_case_length[i]):
			if data[i][j] == data[i][k]:
				print('YES')
				j = int(test_case_length[i]) - 2
				break
			k += 1
		if j == int(test_case_length[i]) - 3:
			print('NO')
			j = int(test_case_length[i]) - 2
			break
		j += 1


import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
ORDA = 97

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	newnumber = 0
	while number > 0:
		newnumber += number % base
		number //= base
	return newnumber

def cdiv(n, k):
	return n // k + (n % k != 0)
for _ in range(ii()):
	n = ii()
	a = li()
	d = {}
	for i in range(n):
		if a[i] in d:
			d[a[i]].append(i)
		else:
			d[a[i]] = [i]
	for i in d:
		if len(d[i]) > 2:
			print('YES')
			break
		elif len(d[i]) == 2:
			if d[i][1] - d[i][0] != 1:
				print('YES')
				break
	else:
		print('NO')


import io
import os
import sys
import math
import heapq
import bisect
import random
import string
import decimal
import functools
import fractions
import itertools
import collections
from sys import stdin, stdout
from io import BytesIO, IOBase
from math import gcd, floor, sqrt, log, factorial
from bisect import bisect_left, bisect_right
from collections import deque, Counter, defaultdict, OrderedDict, namedtuple, UserDict, UserList, UserString
MOD = 1000000007
sys.setrecursionlimit(100000000)
BUFSIZE = 8192
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	lastIndex = {}
	for (i, ch) in enumerate(arr):
		lastIndex[ch] = i
	res = 'NO'
	for (i, ch) in enumerate(arr):
		if lastIndex[ch] > i + 1:
			res = 'YES'
			break
	print(res)


for _ in '0' * int(input()):
	(n, s) = (int(input()), 'NO')
	ls = list(map(int, input().split()))
	for (i, e) in enumerate(ls):
		if e in ls[i + 2:]:
			s = 'YES'
	print(s)


t = int(input())
for i in range(t):
	input()
	a = input().split()
	for (pos, elem) in enumerate(a):
		if a[pos + 2:].count(elem) > 0:
			print('YES')
			break
	else:
		print('NO')


from itertools import *

def calc(arr, c):
	aS = 0
	bS = 0
	for x in arr:
		if x == c[0]:
			aS += 1
		else:
			bS += 1
	lp = 0
	rp = len(arr) - 1
	cA = aS
	cB = bS
	sA = 0
	pans = cB
	while lp < rp:
		if arr[lp] == c[1]:
			lp += 1
			cB -= 1
			continue
		if arr[rp] == c[1]:
			rp -= 1
			cB -= 1
			continue
		sA += 2
		lp += 1
		rp -= 1
		pans = max(pans, sA + cB)
	lp = 0
	rp = len(arr) - 1
	cA = aS
	cB = bS
	pans = max(pans, cA)
	sB = 0
	while lp < rp:
		if arr[lp] == c[0]:
			lp += 1
			cA -= 1
			continue
		if arr[rp] == c[0]:
			rp -= 1
			cA -= 1
			continue
		sB += 2
		lp += 1
		rp -= 1
		pans = max(pans, cA + sB)
	return pans

def fltr(arr, c):
	ret = []
	for x in arr:
		if x == c[0] or x == c[1]:
			ret.append(x)
	return calc(ret, c)
T = int(input())
for t in range(T):
	n = int(input())
	arr = [int(x) for x in input().split(' ')]
	dct = {}
	ans = 0
	unq = []
	for x in arr:
		if x not in dct:
			unq.append(x)
			dct[x] = 0
		dct[x] += 1
		ans = max(ans, dct[x])
	for c in combinations(unq, 2):
		ans = max(ans, fltr(arr, c))
	print(ans)


t = int(input())
for it in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	p = False
	for i in range(n):
		c = a[i]
		palindrome = False
		for j in range(i + 2, n):
			if a[j] == c:
				p = True
				palindrome = True
				print('YES')
				break
		if palindrome:
			break
	if not p:
		print('NO')


def main():
	op = ''
	t = int(input())
	for i in range(t):
		n = int(input())
		ip = [int(item) for item in input().split(' ')]
		x = 0
		op_t = 'NO\n'
		while x + 2 < len(ip):
			if ip[x] in ip[x + 2:]:
				op_t = 'YES\n'
				break
			x += 1
		op += op_t
	print(op)
main()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	flag = False
	for i in range(n):
		if l[i] not in d:
			d[l[i]] = [i]
		else:
			d[l[i]].append(i)
	for i in d:
		if len(d[i]) > 2:
			flag = True
			break
		if len(d[i]) >= 2:
			for j in range(1, len(d[i])):
				if d[i][j] - d[i][j - 1] > 1:
					flag = True
					break
			if flag:
				break
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
for l in range(t):
	n = int(input())
	d = {k: -1 for k in range(1, n + 1)}
	a = list(map(int, input().split()))
	for (k, v) in enumerate(a):
		if d[v] == -1:
			d[v] = k
		elif k - d[v] > 1:
			print('YES')
			break
	else:
		print('NO')


from sys import stdin, stdout
n = int(stdin.readline())
for i in range(n):
	ans = 'NO'
	a = stdin.readline()
	lst = [int(i) for i in stdin.readline().split()]
	prev = lst[0]
	check = [lst[0]]
	q = True
	for i in lst[1:]:
		if prev == i and q:
			q = False
			continue
		q = True
		prev = i
		if i in check:
			ans = 'YES'
			break
		check += [i]
	stdout.write(ans + '\n')


n = int(input())
for i in range(n):
	n1 = int(input())
	l = list(map(int, input().split()))
	lis = [0] * 5001
	lis[l[0]] = 1
	for j in range(1, n1):
		if lis[l[j]] > 1 or (lis[l[j]] == 1 and l[j - 1] != l[j]):
			print('YES')
			break
		lis[l[j]] += 1
	else:
		print('NO')


q = int(input())
for u in range(q):
	w = int(input())
	e = list(map(int, input().split()))
	if w < 3:
		print('NO')
	elif w == 3:
		if e[0] == e[2]:
			print('YES')
		else:
			print('NO')
	else:
		for i in range(len(e) - 2):
			if e[i] in e[i + 2:]:
				print('YES')
				break
		else:
			print('NO')


n = int(input())
for i in range(n):
	ans = 0
	a = int(input())
	b = input().split()
	br = list(reversed(b))
	s = set(b)
	for i in s:
		if a - br.index(i) - 1 - b.index(i) >= 2:
			ans = True
			break
	if ans:
		print('YES')
	else:
		print('NO')


t = int(input())
while t:
	garbage = int(input())
	z = list(map(int, input().rstrip().split(' ')))
	if garbage < 4:
		if z[0] == z[2]:
			print('YES')
		else:
			print('NO')
	else:
		r = 'NO'
		i = 0
		while i < len(z) - 2:
			for f in range(i + 2, len(z)):
				if z[i] == z[f]:
					r = 'YES'
					break
			i = i + 1
		print(r)
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	m = max(l)
	a = [-1] * (m + 1)
	b = [-1] * (m + 1)
	for i in range(n):
		if a[l[i]] == -1:
			a[l[i]] = i
	for i in range(n - 1, -1, -1):
		if b[l[i]] == -1:
			b[l[i]] = i
	f = 1
	for i in range(m + 1):
		if abs(a[i] - b[i]) > 1:
			f = 0
			break
	if not f:
		print('YES')
	else:
		print('NO')


T = int(input())
for i in range(T):
	n = int(input())
	L = list(map(int, input().split()))
	Dict = {}
	l = []
	stat = 1
	for i in range(n):
		if L[i] in l:
			if i - Dict[L[i]] > 1:
				print('YES')
				stat = 0
				break
		else:
			l.append(L[i])
			Dict[L[i]] = i
	if stat != 0:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	arr = [0] * max(li)
	for i in range(len(li) - 1):
		if li[i] != li[i + 1]:
			arr[li[i] - 1] += 1
	arr[li[-1] - 1] += 1
	count = 0
	abc = False
	xyz = False
	for i in range(len(li) - 2):
		if li[i] == li[i + 2]:
			abc = True
			xyz = True
			break
	for i in range(len(arr)):
		if arr[i] > 1:
			abc = True
			xyz = True
			break
	if not xyz:
		if len(set(li)) == 1:
			print('YES')
			abc = True
			continue
		elif not abc:
			print('NO')
	else:
		print('YES')


for _ in ' ' * int(input()):
	a = int(input())
	z = 'NO'
	k = list(map(int, input().split()))
	for i in range(a - 2):
		if k[i] in k[i + 2:]:
			z = 'YES'
			break
	print(z)


for w in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	st = {}
	for (i, x) in enumerate(a):
		if x in st:
			if st[x] < i - 1:
				print('YES')
				break
		else:
			st[x] = i
	else:
		print('NO')


import collections

def another_palindrome(arr, n):
	res = {}
	for (i, val) in enumerate(arr):
		if val in res:
			if i - res[val] > 1:
				return 'YES'
		else:
			res[val] = i
	return 'NO'
t = int(input())
for i in range(1, t + 1):
	n = int(input())
	arr = [int(s) for s in input().split(' ')]
	ans = another_palindrome(arr, n)
	print(ans)


def lcs_(X, Y):
	m = len(X)
	n = len(Y)
	L = [[0] * (n + 1)] * (m + 1)
	for i in range(n + 1):
		for j in range(n + 1):
			if i == 0 or j == 0:
				L[i][j] = 0
			elif X[i - 1] == Y[j - 1]:
				L[i][j] = L[i - 1][j - 1] + 1
			else:
				L[i][j] = max(L[i - 1][j], L[i][j - 1])
	return L[m][n]

def lps(string):
	rev = string[::-1]
	return lcs_(string, rev)
for i in range(int(input())):
	n = int(input())
	x = [int(q) for q in input().split()]
	ans = lps(x)
	if ans < 3:
		print('NO')
	else:
		print('YES')


def palindrome(a, n):
	for i in range(n - 2):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				return 'Yes'
	return 'No'
T = int(input())
while T > 0:
	n = int(input())
	a = list(map(int, input().split()))
	print(palindrome(a, n))
	T -= 1


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	ans = 'NO'
	a = list(map(int, stdin.readline().split()))
	for i in range(n):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				ans = 'YES'
				break
	print(ans)


import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 10 ** 9 + 7

def lcm(x, y):
	return x * y / gcd(x, y)

def fact(x, mod=mod):
	ans = 1
	for i in range(1, x + 1):
		ans = ans * i % mod
	return ans

def arr2D(n, m, default=0):
	lst = []
	for i in range(n):
		temp = [default] * m
		lst.append(temp)
	return lst

def sortDictV(x):
	return {k: v for (k, v) in sorted(x.items(), key=lambda item: item[1])}

def solve(n, lst):
	cnt = {}
	for i in range(n):
		if cnt.get(lst[i]) == None:
			cnt[lst[i]] = [1, i, i]
		else:
			cnt[lst[i]][2] = i
	for i in cnt:
		if cnt[i][1] + 1 < cnt[i][2]:
			return 'YES'
	return 'NO'
for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	print(solve(n, lst))


for _ in range(int(input())):
	(dic, found) = ({}, False)
	size = input()
	for (x, y) in enumerate(map(int, input().split())):
		if y not in dic:
			dic.update({y: x})
		elif x - dic[y] > 1:
			found = True
			break
	print('YES' if found else 'NO')


def main(n, a):
	len = max(a) + 1
	counts = [0] * len
	dups = [0] * len
	counts[a[0]] += 1
	for i in range(1, n):
		counts[a[i]] += 1
		if a[i - 1] == a[i]:
			dups[a[i - 1]] += 1
	if max(counts) >= 3:
		return 'YES'
	if max(dups) >= 2:
		return 'YES'
	temp = 0
	c = 0
	for i in range(len):
		if dups[i] == 0 and counts[i] >= 2:
			temp += counts[i]
		if dups[i] == 1:
			c += 1
	if 1 in counts:
		temp += 1
	if c >= 1:
		temp += 1
	if temp >= 3:
		return 'YES'
	return 'NO'
k = int(input())
for _ in range(k):
	n = int(input())
	a = list(map(int, input().split()))
	print(main(n, a))


def cal(v):
	d = {}
	for (i, x) in enumerate(v):
		if x in d:
			if i - d[x] > 1:
				return True
		else:
			d[x] = i
	return False
for _ in range(0, int(input())):
	n = int(input())
	v = map(int, input().split())
	print(['NO', 'YES'][cal(v)])


from collections import *
for _ in range(int(input())):
	n = int(input())
	l = [*map(int, input().split())]
	dic = Counter(l)
	flag = False
	for i in dic:
		if dic[i] >= 3:
			flag = True
			break
	if flag:
		print('YES')
	else:
		dic = {}
		for i in range(n):
			dic[l[i]] = []
		for i in range(n):
			dic[l[i]].append(i)
		s = set(l)
		for i in s:
			x = len(dic[i])
			if x > 1:
				for j in range(len(dic[i]) - 1):
					if dic[i][j + 1] - dic[i][j] > 1:
						flag = True
						break
			if flag:
				break
		if flag:
			print('YES')
		else:
			print('NO')


t = int(input())
while t:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	c = 0
	for i in range(n):
		if l[i] not in d:
			d[l[i]] = i
		elif l[i] in d:
			if i > 1 + d[l[i]]:
				c = 1
				break
	print('YES' if c == 1 else 'NO')


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	d = {}
	for i in l:
		try:
			d[i] += 1
		except:
			d[i] = 1
	for i in range(1, 27):
		e = d.copy()
		a = 0
		b = n - 1
		m = 0
		while a <= b:
			ans = max(m + max(e.values()), ans)
			while a < n and l[a] != i:
				e[l[a]] -= 1
				a += 1
			while b >= 0 and l[b] != i:
				e[l[b]] -= 1
				b -= 1
			m += 2
			if a < n and b >= 0:
				e[l[a]] -= 2
			a += 1
			b -= 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	ar = [int(x) for x in input().split()]
	b = False
	for i in range(n - 1):
		for j in range(i + 2, n):
			if ar[i] == ar[j]:
				b = True
				break
	if b == True:
		print('YES')
	else:
		print('NO')


t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = [int(x) for x in input().split()]
	flag = False
	for i in range(n):
		for j in range(i + 2, n):
			if arr[i] == arr[j]:
				flag = True
	print('YES' if flag else 'NO')


import sys
input = sys.stdin.readline
import heapq
import copy
import math

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

class Node:

	def _init_(self, val):
		self.data = val
		self.left = None
		self.right = None

class fen_tree:

	def __init__(self, list):
		self.array = [0] + list
		for idx in range(1, len(self.array)):
			idx2 = idx + (idx & -idx)
			if idx2 < len(self.array):
				self.array[idx2] += self.array[idx]

	def prefix_query(self, idx):
		result = 0
		while idx:
			result += self.array[idx]
			idx -= idx & -idx
		return result

	def prints(self):
		print(self.array)
		return

	def range_query(self, from_idx, to_idx):
		return self.prefix_query(to_idx) - self.prefix_query(from_idx - 1)

	def update(self, idx, add):
		while idx < len(self.array):
			self.array[idx] += add
			idx += idx & -idx

def pre_sum(arr):
	p = [0]
	for i in arr:
		p.append(p[-1] + i)
	p.pop(0)
	return p

def pre_back(arr):
	p = [0]
	for i in arr:
		p.append(p[-1] + i)
	p.pop(0)
	return p

def bin_search(arr, l, r, val):
	if arr[r] <= val:
		return r + 1
	if r - l < 2:
		if arr[l] > val:
			return l
		else:
			return r
	mid = int((l + r) / 2)
	if arr[mid] <= val:
		return bin_search(arr, mid, r, val)
	else:
		return bin_search(arr, l, mid, val)

def search_leftmost(arr, val):

	def helper(arr, l, r, val):
		print(l, r)
		if arr[l] == val:
			return l
		if r - l <= 1:
			if arr[r] == val:
				return r
			else:
				print('not found')
				return
		mid = int((r + l) / 2)
		if arr[mid] >= val:
			return helper(arr, l, mid, val)
		else:
			return helper(arr, mid, r, val)
	return helper(arr, 0, len(arr) - 1, val)

def search_rightmost(arr, val):

	def helper(arr, l, r, val):
		print(l, r)
		if arr[r] == val:
			return r
		if r - l <= 1:
			if arr[l] == val:
				return r
			else:
				print('not found')
				return
		mid = int((r + l) / 2)
		if arr[mid] > val:
			return helper(arr, l, mid, val)
		else:
			return helper(arr, mid, r, val)
	return helper(arr, 0, len(arr) - 1, val)

def pr_list(a):
	print(*a, sep=' ')

def main():
	tests = inp()
	mod = 1000000007
	limit = 10 ** 18
	for test in range(tests):
		n = inp()
		a = inlt()
		hashm = [0 for i in range(27)]
		counters = [[] for i in range(n)]
		for i in range(len(a)):
			hashm[a[i]] += 1
			counters[i] = copy.deepcopy(hashm)
		ans = 1
		for i in range(0, n - 1):
			for j in range(i + 1, n):
				j_onwards = [counters[n - 1][k] - counters[j - 1][k] for k in range(27)]
				max_x = max([min(j_onwards[k], counters[i][k]) for k in range(27)])
				if j - i == 1:
					ans = max(ans, 2 * max_x)
					continue
				bet = max([counters[j - 1][k] - counters[i][k] for k in range(27)])
				ans = max(ans, 2 * max_x + bet)
		print(ans)
main()


import sys
import math

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()
t = int(input())
for test in range(t):
	n = int(input())
	arr = get_array()
	dp = [[0 for i in range(27)] for i in range(n + 2)]
	for i in range(n):
		for j in range(27):
			dp[i + 1][j] = dp[i][j]
		dp[i + 1][arr[i]] += 1
	for j in range(27):
		dp[n + 1][j] = dp[n][j]
	ans = 0
	for i in range(n):
		for j in range(i, n):
			lx = 0
			ly = 0
			for x in range(27):
				ly = max(ly, dp[j + 1][x] - dp[i][x])
				if dp[i][x] == dp[n + 1][x] - dp[j + 1][x]:
					lx = max(lx, dp[i][x])
			ans = max(ans, 2 * lx + ly)
	print(ans)


n = int(input())
for i in range(n):
	x = int(input())
	y = list(map(int, input().split()))
	dicti = {}
	for j in y:
		dicti[j] = 0
	if len(list(dicti.values())) == x:
		print('NO')
		continue
	flag = False
	for j in y:
		dicti[j] += 1
		if dicti[j] == 3:
			flag = True
	if flag == True:
		print('YES')
		continue
	listur = list(dicti.values())
	nooftwo = listur.count(2)
	for m in range(len(y) - 1):
		if y[m] == y[m + 1]:
			nooftwo = nooftwo - 1
	if nooftwo >= 1:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	for i in range(n - 3 + 1):
		flag = False
		for j in range(n - 1, i + 1, -1):
			if l[i] == l[j]:
				print('YES')
				flag = True
				break
		if flag:
			break
	else:
		print('NO')


def palindrome(a):
	for (i, j) in enumerate(a):
		for (k, l) in enumerate(a):
			if j == l and k != i and (k != i + 1) and (i - 1 != k):
				return True
t = int(input())
for case in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	if len(a) < 3:
		print('NO')
	elif palindrome(a):
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split(' ')]
	index = [-1] * n
	for i in range(n):
		if index[a[i] - 1] > -1:
			if i - index[a[i] - 1] > 1:
				print('YES')
				break
		else:
			index[a[i] - 1] = i
	else:
		print('NO')


T = int(input())
for t in range(T):
	N = int(input())
	al = input().split()
	dsum = {}
	front = []
	ww = {}
	where = {}
	names = [str(i) for i in range(1, 27)]
	for name in names:
		where[name] = [0]
	for (i, a) in enumerate(al):
		for name in names:
			where[name].append(where[name][-1])
		where[a][-1] = where[a][-2] + 1
		if dsum.get(a):
			dsum[a] += 1
			ww[a].append(i)
		else:
			dsum[a] = 1
			ww[a] = [i]
		front.append(dsum[a])
	ans = 0
	for (i, a) in enumerate(al):
		sum_a = dsum[a]
		sum_f = front[i]
		sum_e = sum_a - sum_f
		if sum_e < sum_f:
			continue
		j = ww[a][sum_a - sum_f]
		for b in names:
			sum_l = where[b][i + 1]
			sum_r = where[b][j]
			sum_m = sum_r - sum_l
			if ans < sum_m + sum_f * 2:
				ans = sum_m + sum_f * 2
	if ans == 0:
		ans += 1
	print(ans)


R = lambda : map(int, input().split())
from collections import defaultdict
t = int(input())

def solve(a):
	d = defaultdict(int)
	d[a[0]] += 1
	m = 0
	current = 1
	for i in range(1, len(a)):
		if a[i] != a[i - 1]:
			d[a[i]] += 1
			current = 1
		else:
			current += 1
			m = max(m, current)
	if max(d.values()) > 1 or m > 2:
		return 'YES'
	return 'NO'
for _ in range(t):
	n = int(input())
	a = list(R())
	print(solve(a))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	m = max(a)
	mem = [[0] * n for i in range(m + 1)]
	position = {}
	for i in range(n):
		if a[i] not in position:
			position[a[i]] = []
		position[a[i]].append(i)
		for j in range(m + 1):
			mem[j][i] = mem[j][i - 1]
		mem[a[i]][i] += 1
	ans = 0
	for i in range(m + 1):
		if i in position:
			ans = max(ans, len(position[i]))
			for p in range(len(position[i])):
				left = position[i][p]
				right = position[i][len(position[i]) - p - 1]
				if left > right:
					break
				max_middle = 0
				for middle in range(m + 1):
					if i == middle:
						continue
					freq = mem[middle][right] - mem[middle][left]
					max_middle = max(max_middle, freq)
				if max_middle != 0:
					ans = max(ans, 2 * (p + 1) + max_middle)
	print(ans)


import collections
from functools import lru_cache

def solve(n, arr):
	reversedArr = arr[::-1]
	n = len(arr)
	dp = [[0] * (n + 1) for _ in range(n + 1)]
	for i in range(1, n + 1):
		for j in range(1, n + 1):
			if arr[i - 1] == reversedArr[j - 1]:
				dp[i][j] = dp[i - 1][j - 1] + 1
			else:
				dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
	return dp[n][n] >= 3

def lcs(arr1, arr2):

	@lru_cache(None)
	def dp(i, j):
		if i <= 0 or j <= 0:
			return 0
		if arr1[i - 1] == arr2[j - 1]:
			return 1 + dp(i - 1, j - 1)
		else:
			return max(dp(i - 1, j), dp(i, j - 1))
	return dp(len(arr1), len(arr2))
t = int(input().strip())
for i in range(t):
	n = int(input().strip())
	arr = list(map(int, input().strip().split()))
	print('YES' if solve(n, arr) else 'NO')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import defaultdict
from bisect import bisect_left as bis
for j in range(int(input())):
	n = int(input())
	vals = list(map(int, input().split()))
	dict1 = defaultdict(list)
	for s in range(n):
		dict1[vals[s]].append(s)
	lr = []
	maxy = 0
	for s in dict1.keys():
		maxy = max(maxy, len(dict1[s]))
		for b in range(len(dict1[s]) // 2):
			l1 = dict1[s][b]
			l2 = dict1[s][-1 - b]
			lr.append((l1, l2, 2 * (b + 1)))
	for s in range(len(lr)):
		for b in dict1.keys():
			seg = dict1[b]
			l1 = min(bis(seg, lr[s][0]), len(seg) - 1)
			l2 = min(bis(seg, lr[s][1]), len(seg) - 1)
			l2 = max(l2, 0)
			if seg[l2] > lr[s][1]:
				l2 -= 1
			length = l2 + 1 - l1
			if lr[s][0] < seg[l1] and seg[l2] < lr[s][1]:
				maxy = max(maxy, lr[s][2] + length)
	print(maxy)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
from collections import defaultdict
from itertools import permutations
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = int(input())
for ti in range(t):
	n = int(input())
	a = [int(i) - 1 for i in input().split(' ')]
	dp = [[0 for i in range(200)] for i in range(n)]
	for i in range(n):
		for j in range(200):
			dp[i][j] = dp[i - 1][j]
		dp[i][a[i]] += 1
	m = 1
	for i in range(200):
		(s, e, c) = (0, n - 1, 0)
		while s < e:
			while s < e and a[s] != i:
				s += 1
			while s < e and a[e] != i:
				e -= 1
			if s < e:
				c += 2
				for j in range(200):
					m = max(m, c + dp[e - 1][j] - dp[s][j])
				s += 1
				e -= 1
	print(m)


test = int(input())
for _ in range(test):
	n = int(input())
	arr = list(map(int, input().split()))
	f = 0
	for i in range(n):
		if arr[i] in arr[i + 2:]:
			f += 1
	if f > 0:
		print('YES')
	else:
		print('NO')


l = int(input())
for _ in range(l):
	b = int(input())
	a = list(map(int, input().split()))
	letters = {}
	for (index, n) in enumerate(a):
		if n not in letters:
			letters[n] = []
		letters[n].append(index)
	f = True
	for k in letters:
		ll = letters[k]
		if len(ll) > 1 and ll[-1] - ll[0] > 1:
			print('YES')
			f = False
			break
	if f:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	nums = list(map(int, input().split()))
	answer = 'NO'
	first = set()
	second = set()
	for i in range(n):
		if not nums[i] in first:
			first.add(nums[i])
		else:
			if nums[i] != nums[i - 1] or nums[i] in second:
				answer = 'YES'
				break
			second.add(nums[i])
	print(answer)


def getmid(s, e, count):
	mx = -1
	for i in range(201):
		mx = max(count[e][i] - count[s][i], mx)
	return mx
for tc in range(int(input())):
	n = int(input())
	a = list(map(int, input().split(' ')))
	count = n * [201 * [0]]
	pos = [[] for i in range(201)]
	for i in range(n):
		count[i] = count[i - 1].copy()
		count[i][a[i]] += 1
		pos[a[i]].append(i)
	ans = 0
	for i in range(len(a)):
		l = count[i][a[i]]
		j = pos[a[i]][-l]
		if i < j:
			ans = max(ans, 2 * l + getmid(i, j - 1, count))
		elif i == j:
			ans = max(ans, 2 * l - 1)
	print(ans)


test = int(input())
for _ in range(test):
	n = int(input())
	alist = [int(i) for i in input().split()]
	count = {}
	ind = {}
	for (i, value) in enumerate(alist):
		if value in count:
			count[value] += 1
			ind[value].append(i)
		else:
			count[value] = 1
			ind[value] = [i]
	blist = sorted(count.items(), key=lambda x: x[-1])
	ans = 'NO'
	for (key, value) in blist:
		if value <= 1:
			continue
		if ind[key][-1] - ind[key][0] > 1:
			ans = 'YES'
			break
	print(ans)


for i in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	X = x[::-1]
	S = list(set(x))
	length = len(S)
	for j in range(length):
		if len(x) - 1 - X.index(S[j]) - x.index(S[j]) > 1:
			print('YES')
			break
		elif j == length - 1:
			print('NO')


import sys
input = sys.stdin.buffer.readline
ans = []
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	f = [[0 for _ in range(26)] for _ in range(n)]
	f[0][a[0] - 1] += 1
	for i in range(1, n):
		for j in range(26):
			f[i][j] = f[i - 1][j]
		f[i][a[i] - 1] += 1
	maxLen = 1
	for i in range(-1, n - 1):
		for j in range(i + 1, n):
			tmp = []
			if i == -1:
				tmp = [0] * 26
			else:
				tmp = f[i]
			ar1 = [0] * 26
			maxAr2 = 0
			ar3 = [0] * 26
			for k in range(26):
				ar1[k] = tmp[k]
				maxAr2 = max(maxAr2, f[j][k] - tmp[k])
				ar3[k] = f[n - 1][k] - f[j][k]
			lenij = 0
			for k in range(26):
				lenij = max(lenij, min(ar1[k], ar3[k]))
			lenij *= 2
			lenij += maxAr2
			maxLen = max(maxLen, lenij)
	ans.append(maxLen)
print('\n'.join(map(str, ans)))


a = int(input())
for i in range(a):
	test = int(input())
	v = [int(i) for i in input().split()]
	flag = True
	for k in range(test):
		for j in range(test):
			if v[k] == v[j] and abs(k - j) >= 2:
				flag = False
				print('YES')
				break
		if flag == False:
			break
	else:
		print('NO')


n = int(input())
for p in range(n):
	x = int(input())
	s = 0
	l = list(map(int, input().split()))
	for i in l:
		if l.count(i) > 1:
			c = l.index(i)
			for k in range(c + 1, len(l)):
				if i == l[k]:
					if k - c > 1:
						print('YES')
						s = 1
						break
			if s == 1:
				break
	if s == 0:
		print('NO')


for _ in range(int(input())):
	a = input()
	lis = [x for x in map(int, input().split())]
	for i in range(len(lis) - 2):
		if lis[i + 2:].count(lis[i]) > 0:
			print('YES')
			break
	else:
		print('NO')


import sys
tc = int(sys.stdin.readline())
for _ in range(tc):
	n = int(sys.stdin.readline())
	arr = list(map(int, sys.stdin.readline().split()))
	find = False
	for i in range(n):
		if not find:
			for j in range(n):
				if arr[i] == arr[j]:
					if abs(i - j) >= 2:
						find = True
						break
		else:
			break
	print('YES' if find else 'NO')


def lps(str):
	n = len(str)
	L = [[0 for x in range(n)] for x in range(n)]
	for i in range(n):
		L[i][i] = 1
	for cl in range(2, n + 1):
		for i in range(n - cl + 1):
			j = i + cl - 1
			if str[i] == str[j] and cl == 2:
				L[i][j] = 2
			elif str[i] == str[j]:
				L[i][j] = L[i + 1][j - 1] + 2
			else:
				L[i][j] = max(L[i][j - 1], L[i + 1][j])
	return L[0][n - 1]
t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	len1 = lps(lst)
	if len1 >= 3:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	palin = [0] * (n + 1)
	non = 0
	for j in range(n):
		if palin[x[j]] == 0:
			palin[x[j]] = j + 1
		elif palin[x[j]] != j:
			print('YES')
			non = 1
			break
	if non == 0:
		print('NO')


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	graph = defaultdict(list)
	for i in range(n):
		graph[arr[i]].append(i)
	uni = list(set(arr))
	final = set()
	final.add(1)
	for i in uni:
		cur = i
		(l, r) = (0, len(graph[cur]) - 1)
		if len(graph[cur]) >= 2:
			final.add(2)
		cnt = 2
		while l < r:
			(low, high) = (graph[cur][l], graph[cur][r])
			d = defaultdict(int)
			for j in range(low + 1, high):
				if j < n:
					d[arr[j]] += 1
			ans = -1
			for j in d:
				ans = max(ans, d[j])
			final.add(cnt + ans)
			l += 1
			r -= 1
			cnt += 2
	final = list(final)
	print(max(final))


from collections import Counter
for _ in range(int(input())):
	input()
	l = list(map(int, input().split()))
	d = Counter(l)
	flag = False
	for item in d:
		if d[item] > 2:
			print('YES')
			flag = True
			break
		elif d[item] == 2:
			first = l.index(item)
			second = l[first + 1:].index(item) + first + 1
			if second - first > 1:
				flag = True
				print('YES')
				break
	if not flag:
		print('NO')


for _ in range(int(input())):
	x = int(input())
	mylist = list(map(int, input().split()))
	amr = dict()
	flag = False
	for i in range(x):
		if mylist[i] not in amr:
			amr[mylist[i]] = i
		elif mylist[i] in amr and amr[mylist[i]] < i - 1:
			flag = True
			break
	if flag == True:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	f = 0
	d = {}
	for i in range(n):
		if l[i] not in d:
			d[l[i]] = 1
		else:
			d[l[i]] += 1
			if l[i] != l[i - 1]:
				f = 1
				break
	if f == 1 or max(d.values()) > 2:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	x = 0
	dic = {}
	for i in range(n):
		if a[i] not in dic:
			dic[a[i]] = i
		elif i != dic[a[i]] + 1:
			x = 1
			break
	if x == 1:
		print('YES')
	else:
		print('NO')


for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 'NO'
	for i in range(n):
		for j in range(i + 1, n):
			if arr[i] == arr[j] and j - i != 1:
				ans = 'YES'
				break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	B = A.copy()
	B.reverse()
	X = [0] * n
	flag = 0
	for i in A:
		X[i - 1] += 1
	for i in range(len(X)):
		if X[i] >= 2:
			first = A.index(i + 1)
			second = B.index(i + 1)
			second = n - second - 1
			if second - first + 1 >= 3:
				print('YES')
				flag = 0
				break
		else:
			flag = 1
	if flag == 1:
		print('NO')


n1 = int(input())
for u in range(n1):
	n = int(input())
	a = list(map(int, input().split()))
	if len(a) <= 2:
		print('NO')
	else:
		di = {}
		for i in range(len(a)):
			if a[i] in di.keys():
				di[a[i]].append(i)
			else:
				di[a[i]] = [i]
		m = sorted(di, key=lambda x: len(di[x]), reverse=True)
		if len(di[m[0]]) >= 3:
			print('YES')
		elif len(di[m[0]]) == 2:
			t = False
			for i in filter(lambda x: len(di[x]) == 2, m):
				if di[i][0] != di[i][1] - 1:
					print('YES')
					t = True
					break
			if not t:
				print('NO')
		else:
			print('NO')


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	di = {}
	for i in range(n):
		if a[i] not in di:
			di[a[i]] = []
		di[a[i]].append(i)
	flag = 'NO'
	for i in di:
		if len(di[i]) >= 3:
			flag = 'YES'
			break
	if flag == 'NO':
		for i in di:
			if len(di[i]) == 2:
				if di[i][1] - di[i][0] > 1:
					flag = 'YES'
					break
	print(flag)


def go(ls):
	ok = False
	for i in range(len(ls)):
		for j in range(i + 2, len(ls)):
			if ls[i] == ls[j]:
				ok = True
	return 'NO' if not ok else 'YES'
t = int(input())
for i in range(t):
	t = input()
	ls = list(map(int, input().split()))
	print(go(ls))


import sys
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]

def main():
	for _ in range(II()):
		n = II()
		aa = LI()
		cs = [[0] for _ in range(26)]
		for a in aa:
			for i in range(26):
				cs[i].append(cs[i][-1])
			cs[a - 1][-1] += 1
		ans = 0
		for l in range(n):
			for r in range(l + 1, n + 1):
				x = y = -1
				for i in range(26):
					csi = cs[i]
					cur = min(csi[l], csi[n] - csi[r])
					if cur > x:
						x = cur
					cur = csi[r] - csi[l]
					if cur > y:
						y = cur
				ans = max(ans, x * 2 + y)
		print(ans)
main()


t = int(input())
while t != 0:
	t -= 1
	n = int(input())
	a = [int(x) for x in input().split()]
	flag = 0
	for i in range(n - 1):
		if a[i] >= a[i + 1]:
			flag = 1
			break
	if flag == 0:
		print('NO')
	else:
		flag = 0
		x = [[] for x in range(n)]
		for i in range(n):
			x[a[i] - 1].append(i)
			if len(x[a[i] - 1]) > 1:
				for j in range(len(x[a[i] - 1]) - 1):
					for k in range(j + 1, len(x[a[i] - 1])):
						if x[a[i] - 1][k] - x[a[i] - 1][j] > 1:
							flag = 1
							break
					if flag == 1:
						break
				if flag == 1:
					break
		if flag == 1:
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	f = (n + 1) * [0]
	x = (n + 1) * [-1]
	z = 0
	for i in range(n):
		f[a[i]] += 1
		if f[a[i]] == 3:
			z = -1
			break
		if x[a[i]] == -1:
			x[a[i]] = i
		elif i - x[a[i]] == 1:
			x[a[i]] = i
		else:
			z = -1
			break
	if z == -1:
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	input()
	B = False
	data = input().split()
	for x in range(len(data)):
		for y in range(x + 2, len(data)):
			if data[x] == data[y]:
				print('YES')
				B = True
				break
		if B:
			break
	else:
		print('NO')


import collections
for _ in range(int(input())):
	k = int(input())
	d = list(map(int, input().split()))
	cnt = collections.defaultdict(list)
	for (i, v) in enumerate(d):
		cnt[v].append(i)
	for (k, v) in cnt.items():
		if len(v) > 2:
			print('YES')
			break
		elif len(v) == 2:
			if v[1] - v[0] > 1:
				print('YES')
				break
	else:
		print('NO')


tc = int(input())
for i in range(tc):
	n = int(input())
	lst = [int(x) for x in input().split()]
	flag = 0
	for x in range(len(lst) - 1):
		y = x + 1
		while y < len(lst):
			if lst[x] == lst[y] and y - x >= 2:
				flag = 1
				break
			y = y + 1
	if flag == 1:
		print('YES')
	else:
		print('NO')


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	r = list(reversed(l))
	flag = 0
	d = Counter(l)
	for i in range(n):
		if d[l[i]] > 1:
			b = r.index(l[i])
			if n - 1 - b > i + 1:
				print('YES')
				flag = 1
				break
	if flag == 0:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	has = [0] * 50000
	for i in range(n):
		has[lis[i]] += 1
	if max(has) >= 3:
		print('YES')
	elif max(has) <= 1:
		print('NO')
	else:
		c = 0
		for i in range(n - 1):
			if has[lis[i]] == 2 and lis[i + 1] != lis[i]:
				print('YES')
				c = 1
				break
			elif has[lis[i]] == 2 and lis[i + 1] == lis[i]:
				has[lis[i]] = 0
		if c == 0:
			print('NO')


n = int(input())

def palindrome(ls):

	def inner():
		return any([ls[i] == ls[j] for i in range(len(ls)) for j in range(i + 2, len(ls))]) and 'YES' or 'NO'
	print(inner())
for i in range(n):
	m = int(input())
	ls = list(map(int, input().split()))
	palindrome(ls)


n = int(input())
for i in range(n):
	a_l = int(input())
	l = list(map(int, input().split()))
	if a_l == 3:
		if l[0] == l[2]:
			print('YES')
		else:
			print('NO')
	else:
		dicti = {}
		flag = False
		for j in range(a_l):
			if l[j] not in dicti:
				dicti[l[j]] = j
			elif j - dicti[l[j]] >= 2:
				flag = True
				break
		if flag == True:
			print('YES')
		else:
			print('NO')


for i in range(int(input())):
	x = int(input())
	y = list(map(int, input().split()))
	z = False
	if x >= 3:
		for k in range(len(y)):
			if y[k] in y[k + 2:]:
				z = True
				break
	print(['NO', 'YES'][z])


no_tests = int(input())
ans = []
while no_tests > 0:
	length = int(input())
	chars = list(map(int, input().split()))
	poss = []
	over = {}
	for i in range(len(chars)):
		if chars[i] not in over.keys():
			for j in range(i + 1, len(chars)):
				if chars[i] == chars[j] and abs(i - j) > 1:
					poss.append(chars[i])
					break
		over[chars[i]] = 0
	if len(poss) >= 1:
		ans.append('YES')
	else:
		ans.append('NO')
	no_tests -= 1
for i in range(len(ans)):
	if i == len(ans) - 1:
		print(ans[i], end='')
	else:
		print(ans[i])


def palindrome_checker(seq):
	ref = {}
	for (i, j) in enumerate(seq):
		if j not in ref:
			ref[j] = i
		elif i - ref[j] > 1:
			return 'YES'
	return 'NO'
t = int(input())
for x in range(t):
	n = int(input())
	seq = list(map(int, input().split()))
	print(palindrome_checker(seq))


from collections import Counter

def I():
	return list(map(int, input().split()))
for __ in range(int(input())):
	n = int(input())
	s = I()
	f = 0
	for i in range(n - 2):
		for j in range(i + 2, n):
			if s[i] == s[j]:
				f = 1
				break
	print('YES') if f else print('NO')


T = int(input())
for t in range(T):
	N = int(input())
	A = [int(_) for _ in input().split()]
	prefC = [[0] * (N + 1) for _ in range(26)]
	for c in range(26):
		for i in range(N):
			prefC[c][i + 1] = prefC[c][i]
			if A[i] == c + 1:
				prefC[c][i + 1] += 1
	answer = 0
	for l in range(N):
		for r in range(l, N):
			cntin = max([prefC[c][r + 1] - prefC[c][l] for c in range(26)])
			cntout = max([min(prefC[c][l], prefC[c][N] - prefC[c][r + 1]) for c in range(26)])
			answer = max(answer, 2 * cntout + cntin)
	print(answer)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	seen = [False for _a in range(n + 1)]
	ok = False
	for i in range(n - 2):
		seen[a[i]] = True
		if seen[a[i + 2]]:
			ok = True
			break
	if ok:
		print('YES')
	else:
		print('NO')


def palin(arr, n):
	for i in range(n - 2):
		if arr[i] in arr[i + 2:]:
			return 'YES'
	return 'NO'
t = int(input())
for x in range(t):
	m = int(input())
	arr = list(map(int, input().split()))
	print(palin(arr, m))


def solve2():
	N = int(input())
	L = list(map(int, input().split()))
	for i in range(1, N - 1):
		if set(L[:i]) & set(L[i + 1:]):
			print('YES')
			break
	else:
		print('NO')
T = int(input())
for _ in range(T):
	solve2()


from sys import stdin
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin

def ii():
	return int(stdin.readline())

def fi():
	return float(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def fmi():
	return map(float, stdin.readline().split())

def li():
	return list(mi())

def lsi():
	x = list(stdin.readline())
	x.pop()
	return x

def si():
	return stdin.readline()
for _ in range(ii()):
	n = ii()
	s = [[] for _ in range(n + 1)]
	a = li()
	f = 0
	for i in range(n):
		s[a[i]].append(i)
		if len(s[a[i]]) > 2 or (len(s[a[i]]) > 1 and s[a[i]][-1] - s[a[i]][-2] > 1):
			f = 1
			break
	print(['NO', 'YES'][f])


t = int(input())
for g in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	breaks = 0
	for i in range(1, n - 1):
		x = set(a[0:i])
		y = set(a[i + 1:n])
		if x & y:
			print('YES')
			breaks = 1
			break
	if breaks == 0:
		print('NO')


t = int(input())
l = []
for knjnf in range(0, t):
	n = int(input())
	ss = input()
	s = ss.split()
	while len(s) != 0:
		h = s[0]
		del s[0]
		a = s.count(h)
		if a > 1:
			l.append('YES')
			break
		elif a == 1:
			if s[0] != h:
				l.append('YES')
				break
			else:
				del s[0]
	if len(s) == 0:
		l.append('NO')
for i in l:
	print(i)


def solve():
	n = int(input())
	l = [[] for _ in range(6000)]
	a = list(map(int, input().split()))
	for i in range(n):
		l[a[i]].append(i)
		if len(l[a[i]]) > 2:
			print('YES')
			return
		if l[a[i]][0] <= i - 2:
			print('YES')
			return
	print('NO')
t = int(input())
for _ in range(t):
	solve()


def go():
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for (i, aa) in enumerate(a):
		if aa in d:
			if i - d[aa] > 1:
				return 'YES'
		d[aa] = d.get(aa, i)
	return 'NO'
t = int(input())
for _ in range(t):
	print(go())


q = int(input())
for t in range(q):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	answ = False
	for i in range(n):
		if arr[i] in arr[i + 2:]:
			answ = True
	if answ:
		print('YES')
	else:
		print('NO')


from sys import stdin, gettrace

def input():
	return stdin.buffer.readline()
t = int(input())
for ti in range(t):
	n = int(input())
	a = [int(i) - 1 for i in input().split()]
	dp = [[0 for i in range(200)] for i in range(n)]
	for i in range(n):
		for j in range(200):
			dp[i][j] = dp[i - 1][j]
		dp[i][a[i]] += 1
	m = 1
	for i in range(200):
		(s, e, c) = (0, n - 1, 0)
		while s < e:
			while s < e and a[s] != i:
				s += 1
			while s < e and a[e] != i:
				e -= 1
			if s < e:
				c += 2
				for j in range(200):
					m = max(m, c + dp[e - 1][j] - dp[s][j])
				s += 1
				e -= 1
	print(m)


t = int(input())
for q in range(0, t):
	(_, a) = (int(input()), list(map(int, input().split())))
	Ans = 'NO'
	for i in range(0, len(a)):
		for j in range(i + 2, len(a)):
			if a[i] == a[j]:
				Ans = 'YES'
	print(Ans)


import sys
from collections import Counter

def main():
	for _ in range(int(sys.stdin.readline())):
		n = int(sys.stdin.readline())
		l = [int(y) for y in sys.stdin.readline().split()]
		d = Counter(l)
		flag = 0
		for i in d.values():
			if i > 2:
				print('YES')
				flag = 1
				break
		if flag == 1:
			continue
		for i in d:
			if d[i] == 2:
				x = l.index(i)
				if l[x + 1] != i:
					print('YES')
					flag = 1
					break
		if flag == 1:
			continue
		print('NO')
main()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	x = {}
	f = 0
	for i in l:
		if i not in x:
			x[i] = 1
		else:
			x[i] += 1
		if x[i] >= 3:
			f = 1
			print('YES')
			break
	if f == 0:
		p = l[0]
		x[l[0]] -= 1
		for i in range(1, n):
			if l[i] == p:
				x[l[i]] -= 1
			elif x[p] >= 1:
				f = 1
				break
			else:
				p = l[i]
				x[p] -= 1
		if f:
			print('YES')
		else:
			print('NO')


m = int(input())
for j in range(m):
	n = int(input())
	mas = list(map(int, input().split()))
	b = False
	kol = [0 for i in range(n)]
	for i in range(n):
		kol[mas[i] - 1] = kol[mas[i] - 1] + 1
		if kol[mas[i] - 1] == 3:
			b = True
			break
		if kol[mas[i] - 1] == 2:
			if mas[i - 1] != mas[i]:
				b = True
	if b:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	array = [int(s) for s in input().split()]
	dict_ = {}
	ans = 1
	for i in range(len(array)):
		if array[i] not in dict_:
			dict_[array[i]] = [i]
		else:
			dict_[array[i]].append(i)
	for j in range(1, 27):
		pref = [0]
		for k in range(len(array)):
			if array[k] == j:
				pref.append(pref[-1] + 1)
			else:
				pref.append(pref[-1])
		dict_[j] = pref
	ans = 1
	for key in dict_.keys():
		ans = max(ans, dict_[key][-1])
		for k in dict_[key]:
			left = -1
			right = len(dict_[key])
			while right - left > 1:
				middle = (right + left) // 2
				if dict_[key][-1] - dict_[key][middle] >= k:
					left = middle
				else:
					right = middle
			right_ = left
			left = -1
			right = len(dict_[key])
			while right - left > 1:
				middle = (right + left) // 2
				if dict_[key][middle] >= k:
					right = middle
				else:
					left = middle
			left_ = right - 1
			if left_ == len(dict_[key]) or left_ >= right_ or right_ == len(dict_[key]):
				continue
			max_ = 0
			for ke in dict_.keys():
				if ke == key:
					continue
				curr = dict_[ke][right_] - dict_[ke][left_]
				if curr > max_:
					max_ = curr
			if max_ + k * 2 > ans:
				ans = max_ + k * 2
	print(ans)


import sys
input = sys.stdin.readline

def read():
	return list(map(int, input().split()))
for _ in range(int(input())):
	n = int(input())
	a = read()
	ans = 'NO'
	for i in range(n - 2):
		if ans == 'YES':
			break
		for j in range(i + 2, n):
			if a[i] == a[j]:
				ans = 'YES'
				break
	for i in range(n - 3):
		st = {}
		mx = 0
		if ans == 'YES':
			break
		for j in range(i + 1, n):
			if a[j] == a[i]:
				if mx >= 2:
					ans = 'YES'
					break
			if a[j] not in st:
				st[a[j]] = 1
			else:
				st[a[j]] += 1
			mx = max(mx, st[a[j]])
	print(ans)


def pallen(L):
	n = len(L)
	counter = [0] * (n + 1)
	for i in range(0, n):
		counter[L[i]] += 1
	if max(counter) >= 3:
		return 'YES'
	elif max(counter) <= 1:
		return 'NO'
	else:
		for i in range(0, n):
			for j in range(i + 2, n):
				if L[i] == L[j]:
					return 'YES'
		return 'NO'
t = int(input())
for i in range(0, t):
	n = int(input())
	L = list(map(int, input().split()))
	print(pallen(L))


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	ans = -1
	for i in range(n - 1):
		if s[i] in s[i + 1:]:
			if s[i + 1:].index(s[i]) != 0:
				ans = 1
				break
			elif i + 2 <= n - 1:
				if s[i + 2] == s[i]:
					ans = 1
					break
	if ans == 1:
		print('YES')
	else:
		print('NO')


n = int(input())
ans = []
for i in range(n):
	input()
	arr = list(map(int, input().split()))
	indexes = {}
	found = False
	for i in range(len(arr)):
		if arr[i] in indexes.keys():
			indexes[arr[i]].append(i)
			if len(indexes[arr[i]]) == 3:
				ans.append('YES')
				found = True
				break
		else:
			indexes[arr[i]] = [i]
	if found:
		continue
	for i in indexes.keys():
		if len(indexes[i]) == 2:
			(a, b) = indexes[i]
			if b - a > 1:
				ans.append('YES')
				found = True
				break
	if not found:
		ans.append('NO')
print(*ans, sep='\n')


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	ok = 0
	b = [0] * (n + 1)
	while i < n:
		b[a[i]] += 1
		if b[a[i]] > 1 and a[i - 1] != a[i]:
			ok = 1
		if b[a[i]] > 2:
			ok = 1
		i += 1
	if ok == 1:
		print('YES')
	else:
		print('NO')


N = int(input())
n = [0 for i in range(N)]
data = [0 for i in range(N)]
split = [0 for i in range(N)]
numbers = [[] for i in range(N)]
palindrome = [False for i in range(N)]
countNumbers = [[-1 for j in range(5111)] for i in range(N)]
for i in range(N):
	n[i] = int(input())
	data[i] = input()
	split[i] = data[i].split(' ')
for i in range(N):
	for j in split[i]:
		numbers[i].append(int(j))
for i in range(N):
	for j in range(n[i]):
		if countNumbers[i][numbers[i][j]] == -1:
			countNumbers[i][numbers[i][j]] = j
		elif abs(j - countNumbers[i][numbers[i][j]]) > 1:
			palindrome[i] = True
for i in range(N):
	if palindrome[i]:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	f = 0
	b = {}
	a = list(map(int, input().split()))
	for i in range(n):
		if a[i] not in b:
			b[a[i]] = [i]
		else:
			b[a[i]].append(i)
			if max(b[a[i]]) - min(b[a[i]]) >= 2:
				f = 1
				break
	if f == 1:
		print('YES')
	else:
		print('NO')


num_t = int(input())
for _ in range(num_t):
	n = int(input())
	a = [int(i) for i in input().split()]
	d = {}
	for (i, e) in enumerate(a):
		if e in d and d[e] < i - 1:
			print('YES')
			break
		if e not in d:
			d[e] = i
	else:
		print('NO')


import sys
import math

def read_line():
	return sys.stdin.readline()[:-1]

def read_int():
	return int(sys.stdin.readline())

def read_int_line():
	return [int(v) for v in sys.stdin.readline().split()]

def lps(str, n):
	L = [[0 for x in range(n)] for x in range(n)]
	for i in range(n):
		L[i][i] = 1
	for cl in range(2, n + 1):
		for i in range(n - cl + 1):
			j = i + cl - 1
			if str[i] == str[j] and cl == 2:
				L[i][j] = 2
			elif str[i] == str[j]:
				L[i][j] = L[i + 1][j - 1] + 2
			else:
				L[i][j] = max(L[i][j - 1], L[i + 1][j])
	return L[0][n - 1]
t = read_int()
for i in range(t):
	n = read_int()
	a = read_int_line()
	f = lps(a, n)
	if f >= 3:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(j) for j in input().split()]
	arr = [-1] + arr + [-1]
	flag = 0
	for i in range(1, n + 1):
		lol = arr.count(arr[i])
		if lol == 2 and arr[i] != arr[i - 1] and (arr[i] != arr[i + 1]) or lol >= 3:
			flag = 1
			break
	if flag == 1:
		print('YES')
	else:
		print('NO')


def solve(n, a):
	for i in range(n - 2):
		if a[i] in a[i + 2:n]:
			return 'YES'
	return 'NO'
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	print(solve(n, a))


t = int(input())
res = []
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	f = 0
	for i in range(n):
		if d.get(a[i], 0) == 0:
			d[a[i]] = [i]
		else:
			d[a[i]] = d[a[i]] + [i]
	for i in d.keys():
		if len(d[i]) >= 3:
			f = 1
			break
		if len(d[i]) == 2 and d[i][1] - d[i][0] >= 2:
			f = 1
			break
	print('YES' if f else 'NO')


for i in range(int(input())):
	a = int(input())
	t = list(map(int, input().split()))
	g = t[::-1]
	h = 0
	p = set(t)
	for j in p:
		s = t.count(j)
		if s >= 3:
			print('YES')
			h += 1
			break
		elif s == 2:
			if a - 1 - g.index(j) - t.index(j) > 1:
				print('YES')
				h += 1
				break
	if h == 0:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	di = {}
	ans = 'NO'
	for i in range(n):
		if l[i] not in d:
			d[l[i]] = i
			di[l[i]] = 1
		elif di[l[i]] == 1:
			if i - d[l[i]] > 1:
				ans = 'YES'
				break
			else:
				di[l[i]] += 1
		elif di[l[i]] > 1:
			ans = 'YES'
			break
	print(ans)


def sub_lists(list):
	for i in list:
		if list.count(i) > 2:
			return True
		elif list.count(i) == 2:
			start = list.index(i)
			end = list.index(i, start + 1)
			if end - start > 1:
				return True
	return False
tc = int(input())
for j in range(tc):
	x = int(input())
	l1 = input().split(' ')
	if sub_lists(l1):
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	flage = False
	for i in range(n):
		if l[i] in l[i + 2:n]:
			flage = True
			break
	if flage:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	flag = 0
	for i in range(n):
		for j in range(i + 2, n):
			if l[i] == l[j]:
				print('YES')
				flag = 1
				break
		if flag == 1:
			break
	if flag == 0:
		print('NO')


x = int(input())
for i in range(x):
	a = int(input())
	l = list(map(int, input().split()))
	m = 0
	for j in l:
		if l.count(j) >= 2:
			t = l.index(j)
			for k in range(t + 1, len(l)):
				if j == l[k]:
					if k - t > 1:
						print('YES')
						m = 1
						break
			if m == 1:
				break
	if m == 0:
		print('NO')


from collections import Counter
import sys

def main():

	def input():
		return sys.stdin.readline()[:-1]
	q = int(input())
	for _ in range(q):
		n = int(input())
		a = list(map(int, input().split()))
		ans = 1
		b = [[0] * n for k in range(27)]
		for k in range(n):
			b[a[k]][k] += 1
		for k in range(1, n):
			for l in range(27):
				b[l][k] += b[l][k - 1]
		for k in range(n):
			for l in range(k, n):
				(c, d) = (0, 0)
				for m in range(27):
					c = max(c, min(b[m][k], b[m][-1] - b[m][l]))
					d = max(d, b[m][l] - b[m][k])
				ans = max(ans, 2 * c + d)
		print(ans)
main()


from sys import stdin, stdout
input = stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	pre = [[] for i in range(27)]
	for i in range(1, 27):
		s = 0
		for j in a:
			if j == i:
				s += 1
			pre[i].append(s)
	ans = 0
	for i in range(n):
		for j in range(i, n):
			count1 = 0
			count2 = 0
			for k in range(1, 27):
				if i == 0:
					count1 = max(count1, 2 * min(0, pre[k][-1] - pre[k][j]))
					count2 = max(count2, pre[k][-1] - (0 + (pre[k][-1] - pre[k][j])))
				else:
					count1 = max(count1, 2 * min(pre[k][i - 1], pre[k][-1] - pre[k][j]))
					count2 = max(count2, pre[k][-1] - (pre[k][i - 1] + (pre[k][-1] - pre[k][j])))
			ans = max(ans, count1 + count2)
	print(ans)


from math import ceil
for i in range(int(input())):
	d = {}
	n = int(input())
	a = list(map(int, input().split()))
	res = 'NO'
	for j in a:
		if d.get(j, 0) == 0:
			d[j] = 1
		else:
			d[j] += 1
			if d[j] >= 3:
				res = 'YES'
				break
	if res == 'YES':
		print(res)
	else:
		if a[0] != a[1] and d[a[0]] == 2:
			res = 'YES'
		if a[n - 1] != a[n - 2] and d[a[n - 1]] == 2:
			res = 'YES'
		for j in range(1, n - 1):
			if a[j] != a[j - 1] and a[j] != a[j + 1] and (d[a[j]] == 2):
				res = 'YES'
				break
		print(res)


from sys import stdin
import sys
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().split()))
	dp = [[0] * (n + 1) for i in range(26)]
	ans = 0
	for i in range(n):
		for c in range(26):
			dp[c][i + 1] = dp[c][i]
		dp[arr[i] - 1][i + 1] += 1
	for c in range(26):
		ans = max(ans, dp[c][n])
	if len(set(arr)) == 1:
		print(ans)
		continue
	for i in range(n):
		for j in range(i, n):
			inc = 0
			outc = 0
			for c in range(26):
				inc = max(inc, dp[c][j + 1] - dp[c][i])
				outc = max(outc, min(dp[c][i], dp[c][n] - dp[c][j + 1]))
			ans = max(ans, inc + 2 * outc)
	print(ans)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	ar = [int(x) for x in input().split()]
	num = []
	for i in range(27):
		br = [0] * (n + 1)
		num.append(br)
	for i in range(n):
		for j in range(27):
			num[j][i + 1] += num[j][i]
		num[ar[i]][i + 1] += 1
	ans = 0
	for j in range(1, n + 1):
		for k in range(j, n + 1):
			ans1 = 0
			ans2 = 0
			for i in range(1, 27):
				ans1 = max(ans1, num[i][k] - num[i][j - 1])
				ans2 = max(ans2, min(num[i][j - 1], num[i][n] - num[i][k]) * 2)
			ans = max(ans, ans1 + ans2)
	print(ans)


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	ans = 0
	arr = list(map(int, input().strip().split()))
	cnt = [[0 for i in range(201)] for j in range(n)]
	freq = defaultdict(int)
	ind = defaultdict(list)
	for j in range(n):
		freq[arr[j]] += 1
		for i in range(201):
			cnt[j][i] = (cnt[j - 1][i] if j > 0 else 0) + (i == arr[j])
		ans = max(ans, freq[arr[j]])
		ind[arr[j]].append(j)
	for a in range(201):
		for sz in range(1, n + 1):
			if 2 * sz <= len(ind[a]):
				r = ind[a][sz - 1]
				l = ind[a][len(ind[a]) - sz]
				bb = 0
				for b in range(201):
					if b != a:
						bb = max(bb, cnt[l][b] - cnt[r][b])
				ans = max(ans, bb + 2 * sz)
			else:
				break
	print(ans)


import sys

def main():
	t = int(sys.stdin.readline())
	for _ in range(t):
		n = int(sys.stdin.readline())
		a = [int(x) for x in sys.stdin.readline().split(' ')]
		ans = 'NO'
		for (i, e) in enumerate(a):
			if e in a[i + 2:]:
				ans = 'YES'
				break
		print(ans)
main()


for _ in range(int(input())):
	n = int(input())
	arr = list(map(lambda x: int(x) - 1, input().split()))
	nxt = [[-1] * 26 for _ in range(n)]
	prv = [[-1] * 26 for _ in range(n)]
	fst = [-1] * 26
	lst = [-1] * 26
	initc = [0] * 26
	for i in range(n):
		initc[arr[i]] += 1
		if fst[arr[i]] == -1:
			fst[arr[i]] = i
		else:
			for j in range(i - 1, -1, -1):
				if arr[j] == arr[i]:
					prv[i][arr[i]] = j
					nxt[j][arr[i]] = i
					break
	for i in range(n - 1, -1, -1):
		if lst[arr[i]] == -1:
			lst[arr[i]] = i
	ans = 0
	for i in range(26):
		cnt = initc[:]
		ans = max(cnt[i], ans)
		cnt[i] = 0
		if fst[i] == -1:
			continue
		l = fst[i]
		r = lst[i]
		if l == r:
			ans = max(1, ans)
			continue
		real_l = 0
		real_r = n - 1
		prsf = 2
		ans = max(ans, max(cnt))
		while l < r:
			while l > real_l:
				cnt[arr[real_l]] -= 1
				real_l += 1
			while r < real_r:
				cnt[arr[real_r]] -= 1
				real_r -= 1
			ans = max(ans, max(cnt) + prsf)
			l = nxt[l][i]
			r = prv[r][i]
			prsf += 2
	print(ans)


import sys
import os
from io import BytesIO, IOBase

def cel(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def prr(a, sep=' '):
	print(sep.join(map(str, a)))

def dd():
	return defaultdict(int)

def ddl():
	return defaultdict(list)

def ddd():
	return defaultdict(defaultdict(int))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import defaultdict
from bisect import bisect_left as bl

def num(tmp, i, j):
	l = bl(tmp, i)
	r = bl(tmp, j)
	return r - l
for _ in range(ii()):
	n = ii()
	a = li()
	d = ddl()
	mx = 1
	for i in range(n):
		d[a[i]].append(i)
	for i in d:
		tmp = d[i]
		mx = max(mx, len(tmp))
		for j in range(len(tmp) // 2):
			for k in d:
				if k != i:
					tmpn = num(d[k], tmp[j], tmp[len(tmp) - 1 - j])
					mx = max(2 * (j + 1) + tmpn, mx)
	print(mx)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	flag = False
	for i in range(n):
		set_l = set()
		set_r = set()
		for j in range(i):
			set_l.add(a[j])
		for j in range(i + 1, n):
			set_r.add(a[i + 1])
		if set_l & set_r:
			flag = True
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().strip().split(' ')))
	f = 0
	for j in range(1, n - 1):
		s = set(lst[:j])
		inter = s.intersection(lst[j + 1:])
		l = len(inter)
		if l > 0:
			print('YES')
			f = 1
			break
	if f == 0:
		print('NO')


from sys import exit
from collections import Counter

def i():
	return input()

def ii():
	return int(input())

def iis():
	return map(int, input().split())

def liis():
	return list(map(int, input().split()))

def print_array(a):
	print(' '.join(map(str, a)))
t = ii()
for _ in range(t):
	n = ii()
	a = liis()
	contador = {}
	for (idx, i) in enumerate(a):
		if i not in contador:
			contador[i] = []
		contador[i].append(idx)
	evens = 0
	odds = 0
	can = False
	for (k, v) in contador.items():
		if len(v) > 2:
			can = True
		elif len(v) == 2:
			if abs(v[0] - v[1]) > 1:
				can = True
		if can:
			break
	if can:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	f = 0
	for i in range(len(l)):
		if l[i] in d:
			x = d[l[i]]
			if len(x) == 2:
				print('YES')
				f = 1
				break
			if len(x) == 1 and x[-1] + 1 < i:
				print('YES')
				f = 1
				break
			x.append(i)
			d[l[i]] = x
		else:
			d[l[i]] = [i]
	if f == 1:
		continue
	else:
		print('NO')


t = int(input())
for tt in range(t):
	n = int(input())
	a = input().split(' ')
	for x in range(n):
		a[x] = int(a[x])
	var = False
	for i in range(n - 2):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				print('YES')
				var = True
				break
			elif i == n - 3 and j == n - 1:
				print('NO')
		if var:
			break


k = int(input())
for i in range(k):
	f = 0
	n = int(input())
	s = list(map(int, input().split()))
	for p in range(1, n - 1):
		(A, B) = (set(s[0:p]), set(s[p + 1:]))
		if A & B:
			f = 1
	if f:
		print('YES')
	else:
		print('NO')


def palindrome(n, array):
	for i in range(n - 2):
		if array[i] in array[i + 2:]:
			return 'YES'
	return 'NO'
t = int(input())
array = []
for i in range(t):
	a = int(input())
	b = input().split(' ')
	array.append([a, b])
for elements in array:
	print(palindrome(elements[0], elements[1]))


def rindex(l, x):
	ans = l[::-1].index(x)
	return len(l) - 1 - ans
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split(' ')))
	ans = False
	for i in l:
		if rindex(l, i) - l.index(i) >= 2:
			ans = True
			break
	if ans:
		print('YES')
	else:
		print('NO')


t = int(input())
for j in range(t):
	n = int(input())
	lista = list(map(int, input().split()))
	lista1 = []
	count = 0
	listr = []
	end = False
	for i in range(1, n):
		if lista[i] in lista[:i] and lista.index(lista[i]) != i - 1:
			print('YES')
			end = True
			break
	if end == False:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	key = 0
	for i in range(n):
		try:
			if l[i] in l[i + 2:]:
				key = 1
				break
		except:
			pass
	if key == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list((int(i) for i in input().split(' ')))
	mp = {i: [] for i in range(n + 10)}
	for i in range(n):
		mp[a[i]].append(i)
	for (k, z) in mp.items():
		if len(z) == 0:
			continue
		if z[0] + 1 < z[-1]:
			print('YES')
			break
	else:
		print('NO')


t = int(input())
for _ in range(t):
	res = 0
	n = int(input())
	a = [int(i) - 1 for i in input().split()]
	(ll, lr) = ([[0] * 201], [[0] * 201])
	ll[0][a[0]] = 1
	lr[0][a[-1]] = 1
	lookup = [[-1] * (n + 1) for i in range(201)]
	c = [0] * 201
	for i in a:
		c[i] += 1
	for i in a[1:]:
		ll.append(ll[-1][:])
		ll[-1][i] += 1
	for i in a[:-1][::-1]:
		lr.append(lr[-1][:])
		lr[-1][i] += 1
	lr = lr[::-1]
	temp = [0] * 201
	for i in range(n - 1, -1, -1):
		temp[a[i]] += 1
		lookup[a[i]][temp[a[i]]] = i
	res = max(temp)
	for i in range(201):
		for j in range(n):
			v = ll[j][i]
			j2 = lookup[i][v]
			if j2 <= j:
				continue
			tres = 0
			for k in range(201):
				t = c[k] - ll[j][k] - lr[j2][k]
				if tres < t:
					tres = t
			tres += 2 * v
			res = max(res, tres)
	print(res)


import sys
input = sys.stdin.readline
from math import *
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = [int(x) for x in input().split()]
	ff = [{} for i in range(201)]
	fl = [{} for i in range(201)]
	count = [0 for i in range(201)]
	cal = [[0 for i in range(201)] for i in range(n + 1)]
	for i in range(n):
		for j in range(1, 201):
			if i == 0:
				cal[i][j] = int(a[i] == j)
			else:
				cal[i][j] = cal[i - 1][j] + int(a[i] == j)
	for i in range(n):
		count[a[i]] += 1
		ff[a[i]][count[a[i]]] = i
	count = [0 for i in range(201)]
	for i in range(n - 1, -1, -1):
		count[a[i]] += 1
		fl[a[i]][count[a[i]]] = i
	maxi = 1
	for i in range(1, 201):
		for j in ff[i]:
			l = ff[i][j]
			if j not in fl[i]:
				maxi = max(maxi, j)
				continue
			r = fl[i][j]
			c = 2 * j
			g = 0
			for k in range(1, 201):
				if r - 1 < l:
					continue
				g = cal[r - 1][k] - cal[l][k]
				maxi = max(maxi, c + g)
	print(maxi)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	pref = [[0 for i in range(n + 1)] for j in range(26)]
	for j in range(0, n):
		for c in range(0, 26):
			pref[c][j + 1] = pref[c][j]
		pref[arr[j] - 1][j + 1] += 1
	ans = 0
	for c in range(1, 27):
		ans = max(ans, pref[c - 1][n - 1])
	for j in range(n):
		l = j
		for r in range(l, n):
			cin = 0
			cout = 0
			for c in range(1, 27):
				cin = max(cin, pref[c - 1][r + 1] - pref[c - 1][l])
				cout = max(cout, min(pref[c - 1][l], pref[c - 1][n] - pref[c - 1][r + 1]) * 2)
			ans = max(ans, cout + cin)
	print(ans)


for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	z = 0
	d = {}
	for i in range(len(l)):
		if l[i] in d:
			d[l[i]].append(i)
		else:
			d[l[i]] = [i]
	ans = 'NO'
	for key in d:
		if len(d[key]) >= 3:
			ans = 'YES'
			break
		elif len(d[key]) == 2 and abs(d[key][0] - d[key][1]) > 1:
			ans = 'YES'
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	k = False
	for i in range(n):
		for j in range(n - 1, i + 1, -1):
			if l[i] == l[j]:
				k = True
				break
	if k:
		print('YES')
	else:
		print('NO')


from pprint import pprint
qq = int(input())
for _ in range(qq):
	n = int(input())
	dat = list(map(int, input().split()))
	f = False
	for i in range(n):
		for j in range(i + 2, n):
			if dat[i] == dat[j]:
				f = True
	print('YES' if f else 'NO')


from collections import defaultdict

def solve():
	N = int(input())
	A = list(map(int, input().split()))
	D = defaultdict(list)
	Dv = defaultdict(int)
	dp = [[0 for i in range(N + 1)] for j in range(201)]
	for (i, x) in enumerate(A):
		Dv[x] += 1
		D[x].append(i + 1)
		dp[x][i + 1] += 1
	for i in range(201):
		for j in range(1, N + 1):
			dp[i][j] += dp[i][j - 1]
	ans = max(Dv.values())
	for value in D.keys():
		S = len(D[value])
		for i in range(1, S // 2 + 1):
			left = D[value][i - 1]
			right = D[value][-i]
			for j in range(201):
				ans = max(ans, i * 2 + dp[j][right - 1] - dp[j][left])
	print(ans)

def main():
	T = int(input())
	for _ in range(T):
		solve()
main()


t = int(input())
for i in range(t):
	n = int(input())
	num1 = list(map(int, input().split()))
	num2 = []
	for i in num1:
		num2.append(num1.count(i))
	if max(num2) >= 3:
		print('YES')
	elif max(num2) == 2:
		a = num2.count(2)
		s = 0
		for i in range(n - 1):
			if num1[i] == num1[i + 1] and num2[i] == 2:
				s += 1
			else:
				continue
		if s == int(a / 2):
			print('NO')
		else:
			print('YES')
	else:
		print('NO')


q = int(input())
for rwe in range(q):
	n = int(input())
	l = list(map(int, input().split()))
	dasie = False
	d = {}
	for i in range(n):
		d[l[i]] = []
	for i in range(n):
		d[l[i]].append(i)
	for elt in d:
		if len(d[elt]) > 2:
			dasie = True
			break
		if len(d[elt]) < 2:
			continue
		if len(d[elt]) == 2:
			if d[elt][0] != d[elt][1] - 1:
				dasie = True
	if dasie:
		print('YES')
	else:
		print('NO')


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = defaultdict(lambda : 0)
	for i in range(n):
		cc = c[a[i]]
		if i >= 2:
			if c[a[i]] >= 1 and a[i - 1] != a[i] or (c[a[i]] >= 2 and a[i - 1] == a[i]):
				print('YES')
				break
		c[a[i]] += 1
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	new_a = []
	now = a[0]
	s = 1
	for i in range(1, n):
		if a[i] == now:
			s += 1
		else:
			new_a.append([now, s])
			s = 1
			now = a[i]
	new_a.append([now, s])
	p = {}
	a = list(set(a))
	for i in range(len(a)):
		p[a[i]] = [0]
	for i in range(len(new_a)):
		for j in p.keys():
			if j == new_a[i][0]:
				p[j].append(p[j][-1] + new_a[i][1])
			else:
				p[j].append(p[j][-1])
	ans = -1
	for i in range(len(new_a)):
		for j in range(i, len(new_a)):
			mx = -1
			good_color = -1
			for k in a:
				counter_color = p[k][j + 1] - p[k][i]
				if counter_color > mx:
					mx = max(mx, counter_color)
					good_color = k
			for k in a:
				first = p[k][i] - p[k][0]
				second = p[k][len(new_a)] - p[k][j + 1]
				ans = max(ans, mx + 2 * min(first, second))
	for i in a:
		ans = max(ans, p[i][-1])
	print(ans)


t = int(input())
for c in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	m = {}
	for i in range(n):
		if a[i] not in m:
			m[a[i]] = [i]
		else:
			m[a[i]].append(i)
	for i in range(n):
		length = len(m[a[i]])
		if m[a[i]][length - 1] - m[a[i]][0] > 1:
			print('YES')
			break
	else:
		print('NO')


from sys import stdin
input = lambda : stdin.readline().strip()
from math import ceil, sqrt, factorial, gcd
from collections import deque
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = {i: 0 for i in range(1, 27)}
	ans = 0
	for i in l:
		d[i] += 1
		ans = max(ans, d[i])
	z = {i: 0 for i in range(1, 27)}
	for i in range(n):
		z[l[i]] += 1
		t = [0] * 27
		for j in range(i + 1, n):
			t[l[j]] += 1
			if l[i] != l[j]:
				ans = max(ans, 2 * min(z[l[i]], d[l[i]] - z[l[i]] - t[l[i]]) + t[l[j]])
	print(ans)


def fun(L):
	for i in range(len(L) - 2):
		for j in range(i + 2, len(L)):
			if L[j] == L[i]:
				return True
	return False
n = int(input())
while n > 0:
	n -= 1
	m = int(input())
	L = list(map(int, input().split()))
	if len(L) < 3:
		print('NO')
	elif fun(L):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	input()
	h = {}
	for (i, a) in enumerate(input().split()):
		if a in h:
			if h[a] < i - 1:
				print('YES')
				break
		else:
			h[a] = i
	else:
		print('NO')


from math import *
from copy import *
from string import *
from random import *
from sys import stdin
from sys import maxsize
from operator import *
from itertools import *
from collections import Counter
from collections import defaultdict

def solve(l):
	n = len(l)
	d = dict(Counter(l))
	r = l[::-1]
	for i in d:
		if d[i] > 2:
			return 'YES'
		elif d[i] == 2:
			a = l.index(i)
			b = n - r.index(i) - 1
			if abs(a - b) > 1:
				return 'YES'
	return 'NO'
T = int(input())
for _ in range(T):
	N = int(stdin.readline())
	l = list(map(int, stdin.readline().rstrip().split()))
	print(solve(l))


from collections import defaultdict

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	V = max(a)
	ans = 0
	cnt = [[0] * (n + 1) for i in range(V + 1)]
	for (i, v) in enumerate(a):
		cnt[v][i + 1] = 1
	for i in range(1, n + 1):
		for j in range(V + 1):
			cnt[j][i] += cnt[j][i - 1]
	subcnt = [defaultdict(int) for i in range(V + 1)]
	appear_cnt = [0] * (V + 1)
	for (i, v) in enumerate(a):
		subcnt[v][appear_cnt[v] + 1] = i + 1
		appear_cnt[v] += 1
	for x in range(1, V + 1):
		for y in range(1, V + 1):
			if x == y:
				ans = max(ans, cnt[x][n])
			else:
				if cnt[x][n] * cnt[y][n] == 0:
					continue
				xcnt = cnt[x][n]
				for c in range(1, xcnt // 2 + 1):
					lx = subcnt[x][c]
					rx = subcnt[x][xcnt - c + 1]
					ans = max(ans, 2 * c + cnt[y][rx - 1] - cnt[y][lx])
	print(ans)
	return

def main():
	t = int(input())
	for i in range(t):
		solve()
	return
main()


import sys
import os
import math
import copy
from math import gcd
from bisect import bisect
from io import BytesIO, IOBase
from math import sqrt, floor, factorial, gcd, log, ceil
from collections import deque, Counter, defaultdict
from itertools import permutations, combinations
from itertools import accumulate

def Int():
	return int(sys.stdin.readline())

def Mint():
	return map(int, sys.stdin.readline().split())

def Lstr():
	return list(sys.stdin.readline().strip())

def Str():
	return sys.stdin.readline().strip()

def Mstr():
	return map(str, sys.stdin.readline().strip().split())

def List():
	return list(map(int, sys.stdin.readline().split()))

def Hash():
	return dict()

def Mod():
	return 1000000007

def Ncr(n, r, p):
	return fact[n] * (ifact[r] * ifact[n - r] % p) % p

def Most_frequent(list):
	return max(set(list), key=list.count)

def Mat2x2(n):
	return [List() for _ in range(n)]

def Lcm(x, y):
	return x * y // gcd(x, y)

def dtob(n):
	return bin(n).replace('0b', '')

def btod(n):
	return int(n, 2)

def common(l1, l2):
	return set(l1).intersection(l2)

def solution():
	for _ in range(Int()):
		n = Int()
		a = List()
		d = {}
		mukul = 0
		for i in range(n):
			if a[i] not in d:
				d[a[i]] = 1
			else:
				d[a[i]] += 1
				if a[i] != a[i - 1]:
					mukul = 1
					break
		if mukul == 1 or max(d.values()) >= 3:
			print('YES')
		else:
			print('NO')
solution()


cases = int(input())
for _ in range(cases):
	n = int(input())
	nums = list(map(int, input().split()))
	pos = [-1] * (n + 1)
	get = False
	for (idx, n) in enumerate(nums):
		if pos[n] == -1:
			pos[n] = idx
		elif idx - pos[n] >= 2:
			get = True
	if get:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	dic = {}
	palindrome = False
	for i in range(len(arr)):
		if arr[i] in dic:
			if i - 1 != dic[arr[i]]:
				palindrome = True
				break
		else:
			dic[arr[i]] = i
	if palindrome:
		print('YES')
	else:
		print('NO')


T = int(input())
for t in range(T):
	n = int(input())
	arr = list(map(int, input().split()))
	dic = {}
	check = True
	for i in range(n):
		c = arr[i]
		if c in dic and dic[c] != i - 1:
			print('YES')
			check = False
			break
		if c not in dic:
			dic[c] = i
	if check:
		print('NO')


def compute(arr, n):
	dic = {}
	for i in range(n):
		try:
			temp = dic[arr[i]]
			temp.append(i)
			if len(dic[arr[i]]) == 3:
				return 'YES'
			elif temp[1] - temp[0] > 1:
				return 'YES'
		except KeyError:
			dic[arr[i]] = [i]
	return 'NO'
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(compute(arr, n))


x = int(input())
for a in range(0, x):
	y = int(input())
	z = input().split()
	f = True
	d = {}
	for b in range(0, y):
		z[b] = int(z[b])
		if not z[b] in d:
			d[z[b]] = [b]
		else:
			d[z[b]].append(b)
	for b in range(0, y):
		if len(d[z[b]]) > 1:
			if d[z[b]][-1] - d[z[b]][0] > 1:
				print('YES')
				f = False
				break
	if f:
		print('NO')


import bisect
import sys
input = sys.stdin.buffer.readline
t = int(input())
num = 200
results = []
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	bad = {}
	good = []
	answers = []
	for i in range(1, num + 1):
		bad[i] = 1
		if i in arr:
			good.append(i)
			bad[i] = 0
	for j in good:
		prefix = list()
		prefix.append(0)
		for i in range(n):
			if arr[i] == j:
				prefix.append(prefix[-1] + 1)
			else:
				prefix.append(prefix[-1])
		for i in range(1, n + 1):
			if j != arr[i - 1]:
				continue
			right = prefix[i]
			right_ind = i
			left_ind = bisect.bisect_right(prefix, prefix[-1] - right) - 1
			if left_ind >= len(prefix):
				left_ind = len(prefix) - 1
			left = prefix[-1] - prefix[left_ind]
			mid = 0
			if left_ind >= right_ind:
				answers.append((left_ind, right_ind, 2 * min(right, left)))
	res = 0
	for j in good:
		prefix = list()
		prefix.append(0)
		for i in range(n):
			if arr[i] == j:
				prefix.append(prefix[-1] + 1)
			else:
				prefix.append(prefix[-1])
		ans = 0
		for (l_i, r_i, nn) in answers:
			mid = prefix[l_i] - prefix[r_i]
			if mid + nn > ans:
				ans = mid + nn
		if ans > res:
			res = ans
		if prefix[-1] > res:
			res = prefix[-1]
	results.append(str(res))
print('\n'.join(results))


def lps(str):
	n = len(str)
	L = [[0 for x in range(n)] for x in range(n)]
	for i in range(n):
		L[i][i] = 1
	for cl in range(2, n + 1):
		for i in range(n - cl + 1):
			j = i + cl - 1
			if str[i] == str[j] and cl == 2:
				L[i][j] = 2
			elif str[i] == str[j]:
				L[i][j] = L[i + 1][j - 1] + 2
			else:
				L[i][j] = max(L[i][j - 1], L[i + 1][j])
	return L[0][n - 1]
t = int(input())
for i in range(t):
	arr = int(input())
	p = list(map(int, input().split()))
	val = lps(p)
	if val >= 3:
		print('yes')
	else:
		print('no')


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	int_sum = [[0 for _ in range(27)] for _ in range(n + 1)]
	for i in range(1, n + 1):
		for j in range(1, 27):
			if a[i - 1] == j:
				int_sum[i][j] = int_sum[i - 1][j] + 1
			else:
				int_sum[i][j] = int_sum[i - 1][j]
	ans = 0
	for i in range(n + 1):
		for j in range(i, n + 1):
			(max_a, max_b) = (0, 0)
			for k in range(1, 27):
				max_a = max(max_a, min(int_sum[n][k] - int_sum[j][k], int_sum[i][k]))
				max_b = max(max_b, int_sum[j][k] - int_sum[i][k])
			ans = max(ans, max_a * 2 + max_b)
	print(ans)


def list_input():
	return list(map(int, input().split()))

def mult_input():
	return map(int, input().split())
for nt in range(int(input())):
	n = int(input())
	l = list_input()
	if n == 1:
		print(1)
		continue
	elif n == 2:
		if l[0] == l[1]:
			print(2)
		else:
			print(1)
		continue
	if len(set(l)) == n:
		print(1)
		continue
	counts = [[0 for i in range(n)] for j in range(26)]
	for i in range(1, 27):
		count = 0
		for j in range(n):
			if l[j] == i:
				count += 1
			counts[i - 1][j] = count
	ans = 0
	for i in range(1, 27):
		for j in range(1, 27):
			start = 0
			end = n - 1
			first = 0
			while start < end:
				if l[start] == i and l[end] == i:
					first += 1
					second = counts[j - 1][end - 1] - counts[j - 1][start]
					ans = max(first * 2 + second, ans)
					start += 1
					end -= 1
				elif l[start] == i:
					end -= 1
				elif l[end] == i:
					start += 1
				else:
					end -= 1
					start += 1
	print(ans)


alph = 'abcdefghijklmnopqrstuvwxyz'
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = []
	for j in range(n):
		d.append([0, 0])
	for j in range(n):
		if d[a[j] - 1][0] == 0:
			d[a[j] - 1][0] = j + 1
		else:
			d[a[j] - 1][1] = j + 1
	for j in range(n):
		if d[j][1] - d[j][0] > 1:
			print('YES')
			break
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	lst = input().split()
	lst = [int(i) for i in lst]
	dct = {}
	for i in range(n):
		if dct.get(lst[i], -1) == -1:
			dct[lst[i]] = i
		elif i - dct[lst[i]] > 1:
			print('YES')
			break
	else:
		print('NO')


from sys import stdin, gettrace
if not gettrace():

	def input():
		return next(stdin)[:-1]

def main():

	def solve():
		n = int(input())
		aa = [int(a) - 1 for a in input().split()]
		counts = [[0] * 26]
		pos = [[] for _ in range(26)]
		for (i, a) in enumerate(aa):
			counts.append(counts[-1].copy())
			counts[-1][a] += 1
			pos[a].append(i)
		mx = 0
		for (i, a) in enumerate(aa):
			ca = counts[i + 1][a]
			ps = pos[a][-ca]
			if ps > i:
				for b in range(26):
					mx = max(counts[ps][b] - counts[i + 1][b] + ca * 2, mx)
			elif ps == i:
				mx = max(ca * 2 - 1, mx)
		print(mx)
	q = int(input())
	for _ in range(q):
		solve()
main()


from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
from itertools import permutations, combinations, groupby
import functools
import sys
import bisect
import string
import math
import time
import random

def Golf():
	(*a,) = map(int, open(0))

def I():
	return int(input())

def S_():
	return input()

def IS():
	return input().split()

def LS():
	return [i for i in input().split()]

def LI():
	return [int(i) for i in input().split()]

def LI_():
	return [int(i) - 1 for i in input().split()]

def NI(n):
	return [int(input()) for i in range(n)]

def NI_(n):
	return [int(input()) - 1 for i in range(n)]

def StoLI():
	return [ord(i) - 97 for i in input()]

def ItoS(n):
	return chr(n + 97)

def LtoS(ls):
	return ''.join([chr(i + 97) for i in ls])

def GI(V, E, Directed=False, index=0):
	org_inp = []
	g = [[] for i in range(n)]
	for i in range(E):
		inp = LI()
		org_inp.append(inp)
		if index == 0:
			inp[0] -= 1
			inp[1] -= 1
		if len(inp) == 2:
			(a, b) = inp
			g[a].append(b)
			if not Directed:
				g[b].append(a)
		elif len(inp) == 3:
			(a, b, c) = inp
			aa = (inp[0], inp[2])
			bb = (inp[1], inp[2])
			g[a].append(bb)
			if not Directed:
				g[b].append(aa)
	return (g, org_inp)

def GGI(h, w, search=None, replacement_of_found='.', mp_def={'#': 1, '.': 0}):
	mp = [1] * (w + 2)
	found = {}
	for i in range(h):
		s = input()
		for char in search:
			if char in s:
				found[char] = (i + 1) * (w + 2) + s.index(char) + 1
				mp_def[char] = mp_def[replacement_of_found]
		mp += [1] + [mp_def[j] for j in s] + [1]
	mp += [1] * (w + 2)
	return (h + 2, w + 2, mp, found)

def TI(n):
	return GI(n, n - 1)

def bit_combination(k, n=2):
	rt = []
	for tb in range(n ** k):
		s = [tb // n ** bt % n for bt in range(k)]
		rt += [s]
	return rt

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
YN = ['YES', 'NO']
Yn = ['Yes', 'No']
mo = 10 ** 9 + 7
inf = float('inf')
l_alp = string.ascii_lowercase
input = lambda : sys.stdin.readline().rstrip()

def ran_input():
	n = random.randint(4, 16)
	(rmin, rmax) = (1, 10)
	a = [random.randint(rmin, rmax) for _ in range(n)]
	return (n, a)
show_flg = False
show_flg = True
t = I()
for _ in range(t):
	n = I()
	a = LI()
	ans = 'No'
	for i in range(n - 2):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				ans = 'Yes'
	print(ans)


for i in range(int(input())):
	n = int(input())
	s = input().split()
	for e in range(n):
		s[e] = int(s[e])
	ok = False
	for i in range(n):
		for j in range(i + 2, n):
			if s[i] == s[j]:
				ok = True
	print('YES' if ok else 'NO')


t = int(input())
while t != 0:
	n = int(input())
	A = map(int, input().split())
	B = list(A)
	D = set(B)
	tmp = 0
	for i in D:
		x = B.index(i)
		if x < n - 2:
			if i in B[x + 2:]:
				tmp = 1
				break
	if tmp == 1:
		print('YES')
	else:
		print('NO')
	t -= 1


t = int(input())
for i in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	flag = 0
	for k in range(0, n - 2):
		for l in range(k + 2, n):
			if a[k] == a[l]:
				flag = 1
				break
		if flag == 1:
			break
	if flag == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	el_map = {arr[0]: 0}
	found = False
	for i in range(1, n):
		if el_map.get(arr[i], -1) != -1:
			if i - el_map[arr[i]] > 1:
				found = True
		else:
			el_map[arr[i]] = i
	if found:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	_ = int(input())
	numbers = [int(t) for t in input().split()]
	positions = {}
	result = 'NO'
	for (i, num) in enumerate(numbers):
		if numbers[i] not in positions:
			positions[numbers[i]] = i
		elif abs(positions[numbers[i]] - i) > 1:
			result = 'YES'
			break
	print(result)


import sys
input = sys.stdin.readline
t = int(input())
ans = []
for _ in range(t):
	n = int(input())
	(*a,) = map(int, input().split())
	if n == 1:
		ans.append(1)
		continue
	cnt = [[x == i for x in a] for i in range(1, 27)]
	for i in range(n - 1):
		for j in range(26):
			cnt[j][i + 1] += cnt[j][i]
	ret = 0
	for i in range(n):
		for j in range(i, n):
			tmp1 = tmp2 = 0
			for x in range(26):
				tmp1 = max(tmp1, min(cnt[x][i], cnt[x][-1] - cnt[x][j]))
				tmp2 = max(tmp2, cnt[x][j] - cnt[x][i])
			ret = max(ret, tmp1 * 2 + tmp2)
	ans.append(ret)
print('\n'.join(map(str, ans)))


n = int(input())
for i in range(n):
	t = int(input())
	h = 1
	s = list(map(int, input().split()))
	for i in range(len(s)):
		if s.count(s[i]) > 1:
			if s[i + 1:].count(s[i]) == 1:
				if not s[i + 1:].index(s[i]) == 0:
					print('YES')
					h = 0
					break
			elif s[i + 1:].count(s[i]) > 1:
				print('YES')
				h = 0
				break
	if h == 1:
		print('NO')


t = int(input())
for tt in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = [[]] * (n + 1)
	for i in range(n):
		x = a[i]
		if len(cnt[x]) == 0:
			cnt[x] = []
		cnt[x].append(i)
	found = 0
	for lst in cnt:
		for i in range(len(lst)):
			for j in range(len(lst)):
				if i == j:
					continue
				if lst[j] - lst[i] > 1:
					found = 1
					break
	if not found:
		print('no')
	else:
		print('yes')


I = input
exec(int(I()) * "I();a=I().split();print('NYOE S'[any(x in a[i+2:]for i,x in enumerate(a))::2]);")


def check(l, r, a, n):
	for l in range(n - 2):
		for r in range(l + 2, n):
			if a[l] == a[r]:
				return 1
	return 0
t = int(input())
while t != 0:
	n = int(input())
	a = list(map(int, input().split()))
	if check(0, 0, a, n) == 1:
		print('YES')
	else:
		print('NO')
	t = t - 1


import os
import sys
from io import BytesIO, IOBase
from collections import Counter, defaultdict

def solution(n, arr):
	counts = Counter(arr)
	indices = defaultdict(list)
	if any((x > 1 for x in counts.values())):
		for i in range(n):
			indices[arr[i]].append(i)
		for li in indices.values():
			for i in range(len(li) - 1):
				if li[-1] - li[0] > 1:
					print('YES')
					return
	print('NO')

def main():
	for _ in range(r_int()):
		n = r_int()
		arr = r_array()
		solution(n, arr)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def r_array():
	return [int(x) for x in input().split()]

def r_int():
	return int(input())

def p_array(arr):
	for x in arr:
		print(x, end=' ')
	print()
main()


from math import *
inf = 10 ** 10
zzz = int(input())
for zz in range(zzz):
	n = int(input())
	a = [int(i) for i in input().split()]
	ha = False
	for i in range(n):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				ha = True
				break
	if ha:
		print('YES')
	else:
		print('NO')


t = int(input())
for l in range(t):
	n = int(input())
	a = input().split()
	l1 = []
	for i in a:
		l1.append(int(i))
	l2 = l1[::-1]
	c1 = 0
	for i in range(n):
		k = l1.index(l1[i])
		j = l2.index(l1[i])
		j = n - j - 1
		if j - k > 1:
			print('YES')
			c1 = 1
			break
	if c1 == 0:
		print('NO')


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = dict(Counter(a))
	s = set()
	f = 0
	for i in range(n):
		if a[i] not in s:
			s.add(a[i])
		elif a[i] != a[i - 1] or c[a[i]] >= 3:
			print('YES')
			f = 1
			break
	if f == 0:
		print('NO')


from collections import Counter
from collections import defaultdict
import math
t = int(input())
for _ in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	d = defaultdict(lambda : 0)
	d[a[0]] = d[a[0]] + 1
	f = 1
	for i in range(1, n):
		if a[i] != a[i - 1]:
			d[a[i]] = d[a[i]] + 1
			if d[a[i]] == 2:
				print('YES')
				f = 0
				break
	c = Counter(a)
	v = c.values()
	if max(v) >= 3 and f == 1:
		print('YES')
		f = 0
	if f == 1:
		print('NO')


def main():
	from collections import defaultdict as dd
	t = 1
	t = int(input())
	for _ in range(t):
		d = dd(lambda : [])
		n = int(input())
		a = list(map(int, input().split()))
		for (idx, i) in enumerate(a):
			d[i].append(idx)
		ok = 1
		for i in d:
			if max(d[i]) - min(d[i]) > 1:
				print('YES')
				ok = 0
				break
		if ok:
			print('NO')
	return
main()


from sys import stdin

def input():
	return stdin.buffer.readline()

def main():

	def solve():
		n = int(input())
		l = list(map(int, input().split()))
		sl = [[] for i in range(201)]
		ss = [[0] * 201]
		for i in range(n):
			sl[l[i]].append(i)
			ss.append(ss[-1].copy())
			ss[-1][l[i]] += 1
		ans = 0
		for i in range(1, 201):
			if not sl[i]:
				continue
			if len(sl[i]) == 1:
				ans = max(ans, 1)
				continue
			for j in range(len(sl[i]) // 2):
				le = sl[i][j]
				ri = sl[i][-1 - j]
				for k in range(1, 201):
					if i == k:
						ans = max(ans, len(sl[i]))
						continue
					if sl[k]:
						ans = max(ans, 2 * (j + 1) + ss[ri][k] - ss[le][k])
		print(ans)
	t = int(input())
	for q in range(t):
		solve()
main()


for i in range(int(input())):
	t = int(input())
	l = [int(x) for x in input().split()]
	ans = 0
	if t == 3:
		ll = l.copy()
		ll.reverse()
		if l == ll:
			ans = 1
	else:
		for j in range(1, len(l) - 1):
			if l[j - 1] in l[j + 1:]:
				ans = 1
				break
	if ans == 1:
		print('YES')
	else:
		print('NO')


tc = int(input())
while tc > 0:
	n = int(input())
	a = list(map(int, input().split()))
	cnt = [[0] * 26]
	for i in range(1, n + 1):
		cnt.append(cnt[i - 1].copy())
		cnt[i][a[i - 1] - 1] += 1
	chars = set(filter(lambda c: cnt[n][c] > 0, range(26)))
	ans = 0
	for l in range(n):
		for r in range(l, n):
			max_in = 0
			max_out = 0
			for c in chars:
				max_in = max(max_in, cnt[r + 1][c] - cnt[l][c])
				max_out = max(max_out, min(cnt[l][c], cnt[n][c] - cnt[r + 1][c]) * 2)
			ans = max(ans, max_in + max_out)
	print(ans)
	tc -= 1


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	B = list(set(A))
	d = {}
	flag = 0
	for i in B:
		d[i] = []
	for i in range(N):
		d[A[i]].append(i)
	for i in d.keys():
		if len(d[i]) > 2:
			flag = 1
			break
		elif len(d[i]) == 2:
			if abs(d[i][0] - d[i][1]) > 1:
				flag = 1
				break
	print('YES') if flag == 1 else print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	i = 0
	j = n - 1
	t = True
	while i <= j:
		if arr[i] != arr[j]:
			t = False
			break
		i += 1
		j -= 1
	if t:
		print('YES')
	else:
		for i in range(n - 2):
			if arr[i] in arr[i + 2:]:
				t = True
				break
		if t:
			print('YES')
		else:
			print('NO')


from collections import Counter
n = int(input())
for _ in range(n):
	n = int(input())
	li = list(map(int, input().split()))
	for i in range(1, n - 1):
		left = Counter(li[:i])
		right = Counter(li[i + 1:])
		if any([right[i] for i in left]):
			print('YES')
			break
	else:
		print('NO')


t = int(input())
for i in range(t):
	f = 0
	n = int(input())
	l = list(map(int, input().split()))
	if len(l) == 1 or len(l) == 2:
		print('NO')
	else:
		for j in range(n):
			if l.count(l[j]) > 2:
				print('YES')
				f = 1
				break
			elif l.count(l[j]) == 2:
				if l[j] in l[j + 2:]:
					f = 1
					print('YES')
					break
		if f == 0:
			print('NO')


from collections import defaultdict

def run():
	n = int(input())
	nums = [int(i) for i in input().split()]
	mydic = defaultdict(int)
	mydic[nums[0]] = 1
	for i in range(1, n):
		if mydic[nums[i]] >= 1 and nums[i - 1] != nums[i]:
			print('YES')
			return
		elif mydic[nums[i]] == 2:
			print('YES')
			return
		else:
			mydic[nums[i]] += 1
	print('NO')
t = int(input())
for i in range(t):
	run()


def compute(a, b, isAdd=True):
	ans = [0] * len(a)
	for i in range(0, len(a)):
		if isAdd:
			ans[i] = a[i] + b[i]
		else:
			ans[i] = a[i] - b[i]
	return ans

def solve(arr):
	n = len(arr)
	if n == 0:
		print(0)
		return
	distinctElements = list(set(arr))
	d = len(distinctElements)
	mapElementToIndex = {}
	for i in range(0, d):
		mapElementToIndex[distinctElements[i]] = i
	totalFre = [0] * d
	for i in range(0, len(arr)):
		totalFre[mapElementToIndex[arr[i]]] += 1
	presentCount = [0] * d
	rigthIndex = [n] * d
	rightFre = [[0] * d for i in range(0, d)]
	leftFre = [0] * d
	finalAns = 0
	for i in range(0, n):
		index = mapElementToIndex[arr[i]]
		leftFre[index] += 1
		r = rigthIndex[index]
		for j in range(r - 1, i, -1):
			if arr[j] == arr[i]:
				rigthIndex[index] = j
				presentCount[index] += 1
				rightFre[index][index] += 1
				inbetweenFre = compute(totalFre, compute(leftFre, rightFre[index]), False)
				ans = 2 * presentCount[index] + max(inbetweenFre)
				finalAns = max(ans, finalAns)
				break
			else:
				rightFre[index][mapElementToIndex[arr[j]]] += 1
	print(max(1, finalAns))
t = int(input())
for i in range(0, t):
	input()
	arr = list(map(lambda x: int(x), input().split()))
	solve(arr)


T = int(input())
for _ in range(T):
	l = int(input())
	seq = [int(x) for x in input().split()]
	found = False
	for i in range(len(seq) - 2):
		for j in range(i + 2, len(seq)):
			if seq[i] == seq[j]:
				found = True
				break
		if found:
			break
	if found:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	nums = list(input().split())
	found = False
	contains = set()
	for i in range(1, n):
		if nums[i] in contains:
			found = True
		contains.add(nums[i - 1])
	if found:
		print('YES')
	else:
		print('NO')


def run():
	import sys
	sys.stdin = open('/home/punit/Documents/Cpp Programs/input.txt', 'r')
	sys.stdout = open('/home/punit/Documents/Cpp Programs/output.txt', 'w')
	from math import ceil, sqrt, floor
for _ in range(int(input())):
	n = int(input())
	li = [int(x) for x in input().split()]
	y = set(li)
	d = dict()
	for i in range(n):
		if li[i] not in d.keys():
			d[li[i]] = [i]
		else:
			d[li[i]].append(i)
	flag = 0
	for i in d.values():
		for j in range(len(i) - 1):
			if i[j + 1] - i[j] > 1:
				flag = 1
				break
			elif len(i) >= 3:
				flag = 1
				break
		if flag:
			break
	if flag:
		print('YES')
	else:
		print('NO')


testcases = int(input())
for testcase in range(testcases):
	getlen = int(input())
	getinput = str(input())
	arr = getinput.split()
	arrint = []
	dicts = {}
	for i in arr:
		getinti = int(i)
		arrint.append(getinti)
		if getinti not in dicts:
			dicts[getinti] = 1
		else:
			dicts[getinti] += 1
	tempdict = dicts
	flag = 0
	for i in range(getlen):
		if dicts[arrint[i]] >= 3:
			flag = 1
			print('YES')
			break
		if dicts[arrint[i]] == 2:
			if i + 1 < getlen and arrint[i + 1] != arrint[i]:
				print('YES')
				flag = 1
				break
		dicts[arrint[i]] = dicts[arrint[i]] - 1
	if flag == 0:
		print('NO')


import sys, math, itertools
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify, nlargest
from copy import deepcopy
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))

def inpl_1():
	return list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))
M = 201
for _ in range(inp()):
	n = inp()
	A = inpl()
	aa = [[0] * (n + 1) for _ in range(M)]
	numind = [[] for _ in range(M)]
	for (i, x) in enumerate(A):
		numind[x].append(i)
		for j in range(M):
			aa[j][i + 1] = aa[j][i] + (x == j)
	res = 0
	for i in range(M):
		res = max(res, aa[i][-1])
	for a in range(1, M):
		for cnta in range(1, aa[a][-1] // 2 + 1):
			l = numind[a][cnta - 1]
			r = numind[a][aa[a][-1] - cnta]
			for b in range(1, M):
				if a == b:
					continue
				cntb = aa[b][r] - aa[b][l]
				ans = cnta * 2 + cntb
				res = max(res, ans)
	print(res)


def solve():
	n = int(input())
	a = [int(i) - 1 for i in input().split(' ')]
	dp = [[0 for i in range(200)] for i in range(n)]
	for i in range(n):
		for j in range(200):
			dp[i][j] = dp[i - 1][j]
		dp[i][a[i]] += 1
	m = 1
	for i in range(200):
		(s, e, c) = (0, n - 1, 0)
		while s < e:
			while s < e and a[s] != i:
				s += 1
			while s < e and a[e] != i:
				e -= 1
			if s < e:
				c += 2
				for j in range(200):
					m = max(m, c + dp[e - 1][j] - dp[s][j])
				s += 1
				e -= 1
	print(m)

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()


from collections import deque
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	for i in range(1, 27):
		for j in range(1, 27):
			(i_count, j_count) = (0, 0)
			deq = deque()
			for x in arr:
				if x == i:
					deq.append(x)
					i_count += 1
				elif x == j:
					deq.append(x)
					j_count += 1
			i_move = i_count // 2
			(left, right) = (0, len(deq) - 1)
			(left_seq, right_seq) = (deque(), deque())
			max_len = max(0, j_count)
			itr = 0
			while left < right and i_move > 0:
				while left < right and deq[left] == j:
					left += 1
					j_count -= 1
				while left < right and deq[right] == j:
					j_count -= 1
					right -= 1
				itr += 1
				i_move -= 1
				max_len = max(2 * itr + j_count, max_len)
				left += 1
				right -= 1
			ans = max(ans, max_len)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	valid = False
	for i in range(n):
		if l[i] in d:
			if i - d[l[i]] > 1:
				valid = True
		else:
			d[l[i]] = i
	if valid:
		print('YES')
	else:
		print('NO')


def solve(n, nums):
	counter = [None for i in range(n + 1)]
	for (i, num) in enumerate(nums):
		if counter[num] == None:
			counter[num] = i
		elif counter[num] != i - 1:
			return True
	return False

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		nums = list(map(int, input().split()))
		result = solve(n, nums)
		if result == True:
			print('YES')
		else:
			print('NO')
	return 0
main()


t = int(input())
for k in range(t):
	n = int(input())
	arr = list(input().split())
	check = False
	for i in range(n):
		for j in range(n):
			if arr[i] == arr[j] and abs(i - j) > 1:
				check = True
				break
		if check:
			break
	print('YES' if check else 'NO')


t = t = int(input())
while t > 0:
	n = input()
	arr = [int(x) for x in input().split()]
	flag = False
	for i in range(len(arr) - 1):
		if arr[i] in arr[i + 2:]:
			print('YES')
			flag = True
			break
	if flag == False:
		print('NO')
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(j) for j in input().split()]
	ok = False
	(first, last) = ({}, {})
	for j in range(len(a)):
		if a[j] in first:
			last[a[j]] = j
		else:
			first[a[j]] = j
	for key in first:
		if key in last and last[key] - first[key] > 1:
			ok = True
			break
	if ok == True:
		print('Yes')
	else:
		print('NO')


def RA():
	l = input().split()
	return [int(s) for s in l]
pos = {}

def l3p(a, b):
	i = 0
	j = 0
	aPos = pos[a]
	bPos = pos[b]
	arr = []
	while i < len(aPos) and j < len(bPos):
		if aPos[i] < bPos[j]:
			arr.append(a)
			i += 1
		else:
			arr.append(b)
			j += 1
	arr += [a] * (len(aPos) - i)
	arr += [b] * (len(bPos) - j)
	x = 0
	y = len(bPos)
	l = 0
	r = len(arr) - 1
	best = 1
	while l < r:
		while l < r and (not arr[l] == a):
			y -= 1
			l += 1
		while l < r and (not arr[r] == a):
			y -= 1
			r -= 1
		if l == r:
			break
		x += 1
		best = max(2 * x + y, best)
		l += 1
		r -= 1
	return best

def solve():
	global pos
	n = int(input())
	arr = RA()
	for i in range(n):
		num = arr[i]
		if num in pos:
			pos[num].append(i)
		else:
			pos[num] = [i]
	ans = 1
	for a in pos:
		for b in pos:
			if a == b:
				ans = max(ans, len(pos[a]))
			else:
				ans = max(ans, l3p(a, b))
	print(ans)
	pos = {}
t = int(input())
for i in range(t):
	solve()


t = int(input())
for qwe in range(t):
	n = int(input())
	listt = list(map(int, input().split()))
	flag = False
	for i in range(n):
		try:
			ind = listt.index(listt[i], i + 2, n)
			print('YES')
			flag = True
			break
		except:
			pass
	if not flag:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	l = 0
	h = n - 1
	m = 0
	if n < 3:
		print('NO')
		continue
	else:
		can = False
		for i in range(n):
			for j in range(i + 2, n):
				if arr[i] == arr[j]:
					can = True
					break
	if can:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	done = False
	while len(a) >= 3:
		if a[0] in a[2:]:
			done = True
			break
		a.remove(a[0])
	if done == True:
		print('YES')
	else:
		print('NO')


T = int(input())
for i in range(T):
	N = int(input())
	f = 0
	l = [int(x) for x in input().split()]
	for j in range(N - 2):
		for k in range(j + 2, N):
			if l[j] == l[k]:
				f = 1
				break
		if f == 1:
			break
	if f == 1:
		print('YES')
	else:
		print('NO')


def solve(n, a):
	for i in range(n - 2):
		for j in range(i + 2, n):
			if a[i] == a[j]:
				return 'Yes'
	return 'No'
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	y = solve(n, a)
	print(y)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def main():
	t = I()
	rr = []
	for _ in range(t):
		n = I()
		s = LI()
		f = False
		t = set()
		for i in range(1, n):
			if s[i] in t:
				f = True
				break
			t.add(s[i - 1])
		rr.append(IF(f, 'YES', 'NO'))
	return JA(rr, '\n')
print(main())


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(j) for j in input().split()]
	o = 0
	for k in a:
		if a.count(k) > 1:
			s = a.index(k)
			if k in a[s + 2:n]:
				print('YES')
				o = 1
				break
	if not o:
		print('NO')


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	ans = 'NO'
	d = {}
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = i
		elif i - 1 > d[a[i]]:
			ans = 'YES'
	return ans
for _ in range(int(input())):
	print(solve())


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	foo = False
	for j in range(n):
		for k in range(n - 1, -1, -1):
			if a[j] == a[k] and abs(j - k) > 1:
				foo = True
	if foo:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 3:
		if a == a[::-1]:
			print('YES')
		else:
			print('NO')
	else:
		o = 0
		for i in range(n):
			for j in range(i + 2, n):
				if a[i] == a[j]:
					o = 1
		if o == 1:
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	(d, flag) = (dict(), False)
	for i in range(len(arr)):
		if arr[i] not in d.keys():
			d[arr[i]] = [i, i]
		else:
			d[arr[i]][-1] = i
	for i in d.keys():
		if d[i][1] - d[i][0] > 1:
			flag = True
	if flag:
		print('YES')
	else:
		print('NO')


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = dict(Counter(a))
	f = 0
	for i in b.keys():
		if b[i] >= 3:
			print('YES')
			f = 1
			break
	if f == 1:
		continue
	i = 0
	while i < n - 1:
		if i == 0 and b[a[i]] > 1 and (a[i + 1] != a[i]):
			print('YES')
			f = 1
			break
		elif b[a[i]] > 1 and a[i + 1] != a[i] and (a[i - 1] != a[i]):
			print('YES')
			f = 1
			break
		i += 1
	if f == 1:
		continue
	print('NO')


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	d = {}
	flag = 0
	for i in range(len(arr)):
		if arr[i] in d.keys():
			d[arr[i]] += 1
			if d[arr[i]] == 3:
				flag = 1
				break
			elif arr[i - 1] != arr[i]:
				flag = 1
				break
		else:
			d[arr[i]] = 1
	if flag == 0:
		print('NO')
	else:
		print('YES')


t = int(input())
for tt in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = set()
	g = 0
	s.add(a[0])
	for i in range(2, n):
		if a[i] in s:
			g = 1
		s.add(a[i - 1])
	print('YES' if g else 'NO')


def f(N, L):
	V = [0] * (N + 1)
	for (i, a) in enumerate(L):
		if V[a] > 0:
			r = V[a]
			if L[i - 1] == a:
				r -= 1
			if r > 0:
				return True
		V[a] += 1
	return False
for _ in range(int(input())):
	N = int(input())
	L = list(map(int, input().split()))
	if f(N, L):
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	x = []
	y = []
	z = []
	b = 0
	for j in a:
		x.append(a.count(j))
	if max(x) >= 3:
		print('YES')
	else:
		if max(x) == 2:
			for k in range(len(a)):
				if a.count(a[k]) == 2:
					y.append(k)
					z.append(a[k])
			for l in range(len(z)):
				c = z[l]
				z.reverse()
				d = z.index(c)
				d = len(z) - 1 - d
				z.reverse()
				if y[d] - y[l] > 1:
					b = b + 1
			if b >= 1:
				print('YES')
			else:
				print('NO')
		if max(x) == 1:
			print('NO')


import sys
input = sys.stdin.readline
from collections import defaultdict as dd
t = int(input())
import bisect
while t:
	d = dd(list)
	n = int(input())
	l = list(map(int, input().split()))
	dis = [0] * 27
	di = []
	for i in range(n):
		d[l[i]].append(i)
		dis[l[i]] = 1
	for i in range(27):
		if dis[i] == 1:
			di.append(i)
	n = len(di)
	mx = -1
	for ii in range(n):
		i = di[ii]
		mx = max(mx, len(d[i]))
		for jj in range(n):
			j = di[jj]
			le = 0
			re = len(d[i]) - 1
			while le < re:
				indl = bisect.bisect_right(d[j], d[i][le])
				indr = bisect.bisect_left(d[j], d[i][re])
				if indr <= indl:
					break
				mx = max(mx, le + 1 + indr - indl + len(d[i]) - re)
				le += 1
				re -= 1
	print(mx)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = dict()
	for i in l:
		d[str(i)] = []
	for i in range(len(l)):
		d[str(l[i])].append(i)
	ans = 'NO'
	for (a, b) in d.items():
		if len(b) >= 3:
			ans = 'YES'
			break
	for (a, b) in d.items():
		if len(b) == 2:
			if b[0] != b[1] - 1:
				ans = 'YES'
				break
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	palin3 = False
	for i in range(len(a) - 2):
		if a[i] in a[i + 2:]:
			palin3 = True
			break
	if palin3:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
import bisect
import math

def st():
	return list(stdin.readline().strip())

def inp():
	return int(stdin.readline())

def li():
	return list(map(int, stdin.readline().split()))

def mp():
	return map(int, stdin.readline().split())

def pr(n):
	stdout.write(str(n) + '\n')

def soe(limit):
	l = [1] * (limit + 1)
	prime = []
	for i in range(2, limit + 1):
		if l[i]:
			for j in range(i * i, limit + 1, i):
				l[j] = 0
	for i in range(2, limit + 1):
		if l[i]:
			prime.append(i)
	return prime

def segsoe(low, high):
	limit = int(high ** 0.5) + 1
	prime = soe(limit)
	n = high - low + 1
	l = [0] * (n + 1)
	for i in range(len(prime)):
		lowlimit = low // prime[i] * prime[i]
		if lowlimit < low:
			lowlimit += prime[i]
		if lowlimit == prime[i]:
			lowlimit += prime[i]
		for j in range(lowlimit, high + 1, prime[i]):
			l[j - low] = 1
	for i in range(low, high + 1):
		if not l[i - low]:
			if i != 1:
				print(i)

def power(a, n):
	r = 1
	while n:
		if n & 1:
			r = r * a
			n = n - 1
		else:
			a = a * a
			n = n >> 1
	return r

def occur(l, x):
	for i in range(len(l)):
		if l[i] == x:
			return i

def solve():
	n = inp()
	d = {}
	l = li()
	x = l[::-1]
	for i in l:
		d[i] = d.get(i, 0) + 1
	c = 0
	for i in d:
		if d[i] >= 2:
			a = occur(l, i)
			b = occur(x, i)
			b = n - b - 1
			if b - a > 1:
				c = 1
				break
	if c:
		pr('YES')
	else:
		pr('NO')
for _ in range(inp()):
	solve()


def ans(a):
	b = [0 for i in range(len(a) + 2)]
	b[a[0]] = 1
	for i in range(1, len(a)):
		x = a[i]
		if b[x] == 2 or (b[x] == 1 and a[i - 1] != a[i]):
			return 'YES'
		b[x] += 1
	return 'NO'
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	print(ans(a))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	f = {}
	o = {}
	for j in range(n):
		if a[j] in f:
			f[a[j]] = f[a[j]] + 1
			o[a[j]].append(j)
		else:
			f[a[j]] = 1
			o[a[j]] = [j]
	for k in f:
		if f[k] >= 3:
			print('YES')
			break
		elif f[k] == 2 and o[k][0] + 1 != o[k][1]:
			print('YES')
			break
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	mnozina = set()
	out = 'NO'
	for i in range(n):
		if a[i] in mnozina and a[i - 1] != a[i]:
			out = 'YES'
			break
		elif a[i] in mnozina and a[i - 1] == a[i] and (a[i - 2] == a[i]):
			out = 'YES'
			break
		elif a[i] not in mnozina:
			mnozina.add(a[i])
	print(out)


from collections import Counter
T = int(input())
for _ in range(T):
	n = int(input())
	a = [int(x) for x in input().split(' ')]
	for i in range(n - 2):
		if a[i] in a[i + 2:]:
			print('YES')
			break
	else:
		print('NO')


maxc = 30
T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	s = [{c: 0 for c in range(maxc)} for _ in range(n)]
	s[0][a[0]] = 1
	for i in range(1, n):
		for c in range(maxc):
			s[i][c] = s[i - 1][c] + (c == a[i])
	p = {c: [] for c in range(maxc)}
	for i in range(n):
		p[a[i]].append(i)
	ans = 1
	for x in range(maxc):
		nx = len(p[x])
		ans = max(ans, nx)
		for i in range(nx // 2):
			l = p[x][i]
			r = p[x][nx - i - 1]
			if l <= r:
				for y in range(maxc):
					ans = max(ans, (i + 1) * 2 + (s[r - 1][y] - s[l][y]))
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	orig_arr = list(map(int, input().split()))
	hash_table = {}
	hash_table[orig_arr[0]] = 0
	palin = False
	for j in range(2, n):
		if orig_arr[j] in hash_table:
			print('YES')
			break
		else:
			hash_table[orig_arr[j - 1]] = j - 1
	else:
		print('NO')


import sys
input = sys.stdin.readline
from collections import *
from math import *
from bisect import *
t = int(input())
ref = {'1': '2', '2': '3', '3': '4', '4': '5', '5': '6', '6': '7', '7': '8', '8': '9', '9': '1'}
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	ind = dict()
	for i in range(n):
		if a[i] not in ind:
			ind[a[i]] = [i + 1]
		else:
			ind[a[i]].append(i + 1)
	s = 0
	for i in ind.keys():
		cc = 0
		for k in range(len(ind[i]) // 2):
			cc += 1
			p1 = ind[i][k]
			p2 = ind[i][-1 - k]
			for j in ind.keys():
				ll = bisect_right(ind[j], p1)
				rr = bisect_left(ind[j], p2)
				s = max(2 * cc + (rr - ll), s)
	print(max(s, 1))


N = int(5000.0 + 3)
t = int(input())
all_res = []
for _ in range(t):
	adj = [[] for _ in range(N)]
	n = int(input())
	a = list(map(int, input().split()))
	(max_diff, x, y) = (0, -1, -1)
	for i in range(n):
		adj[a[i]].append(i)
		if len(adj[a[i]]) > 1 and max_diff < adj[a[i]][-1] - adj[a[i]][0]:
			max_diff = adj[a[i]][-1] - adj[a[i]][0]
			(x, y) = (adj[a[i]][0], adj[a[i]][-1])
	if x == -1 or y == -1:
		all_res.append('NO')
		continue
	for i in set(a):
		(l, r) = (0, len(adj[i]) - 1)
		found = False
		while l <= r:
			m = l + r >> 1
			if x < adj[i][m] < y:
				found = True
				break
			if x >= adj[i][m]:
				l = m + 1
			if adj[i][m] >= y:
				r = m - 1
		if found:
			all_res.append('YES')
			break
	else:
		all_res.append('NO')
print('\n'.join(all_res))


from collections import *
from sys import *
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = [int(x) for x in stdin.readline().split()]
	c = Counter(a)
	flag = 0
	i = 0
	while True:
		if i > n - 3:
			break
		if a[i] == a[i + 1] and a[i] == a[i + 2]:
			flag = 1
			break
		if c[a[i]] > 1:
			if a[i] != a[1 + i]:
				flag = 1
				break
			else:
				c[a[i]] -= 1
				i = i + 1
		else:
			i += 1
	if flag == 0:
		print('NO')
	else:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	b = 0
	for j in range(len(l)):
		if l[j] not in d:
			d[l[j]] = j
		elif d[l[j]] == j - 1:
			continue
		else:
			b = 1
			break
	if b == 1:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	flag = 0
	for j in range(n):
		for k in range(j + 2, n):
			if lis[j] == lis[k]:
				flag = 1
	print('YES' if flag == 1 else 'NO')


t = int(input())
for _ in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	d = set()
	d.add(x[0])
	f = 0
	for i in range(1, n):
		if x[i] not in d:
			d.add(x[i])
		elif x[i - 1] != x[i]:
			print('YES')
			f = 1
			break
		elif i >= 2:
			if x[i - 2] == x[i]:
				print('YES')
				f = 1
				break
	if f == 0:
		print('NO')


from collections import defaultdict
from sys import stdin
input = stdin.readline
for ti in range(int(input().strip())):
	n = int(input())
	a = [int(x) for x in input().strip().split()]
	d = defaultdict(list)
	ans = 0
	cnt = [[0] * (n + 1) for _ in range(201)]
	for i in range(n):
		d[a[i]].append(i)
		for j in range(201):
			cnt[j][i] = cnt[j][i - 1]
		cnt[a[i]][i] += 1
	for di in d:
		dil = len(d[di])
		for dj in range(dil // 2):
			(si, ei) = (d[di][dj], d[di][dil - dj - 1])
			mc = 0
			for j in range(201):
				mc = max(mc, cnt[j][ei - 1] - cnt[j][si])
			ans = max(ans, 2 * (dj + 1) + mc)
	for di in d:
		ans = max(ans, len(d[di]))
	print(ans)


import sys

def solve():
	sys.stdin.readline().rstrip('\n')
	arr = sys.stdin.readline().rstrip('\n').split(' ')
	arr = [int(_) for _ in arr]
	left = [0] * len(arr)
	right = [0] * len(arr)
	m = {}
	for i in range(len(arr)):
		if arr[i] in m:
			left[i] = m[arr[i]]
			m[arr[i]] += 1
		else:
			m[arr[i]] = 1
	m.clear()
	for i in range(len(arr) - 1, -1, -1):
		if arr[i] in m:
			right[i] = m[arr[i]]
			m[arr[i]] += 1
		else:
			m[arr[i]] = 1
	for i in range(len(arr)):
		if left[i] > 0 and right[i] > 0:
			print('YES')
			return
		if left[i] > 0:
			if arr[i] != arr[i - 1]:
				print('YES')
				return
		if right[i] > 0:
			if arr[i] != arr[i + 1]:
				print('YES')
				return
	print('NO')
	return
T = int(sys.stdin.readline().rstrip('\n'))
for i in range(T):
	solve()


def func(i, j, a, b):
	if arr[j] == b:
		return prefix[j][b] - prefix[i][b] - 1
	else:
		return prefix[j][b] - prefix[i][b]
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	prefix = [[0] * 27 for i in range(n)]
	count = []
	for i in range(27):
		count += [arr.count(i)]
	for i in range(n):
		prefix[i][arr[i]] += 1
	for i in range(1, n):
		for j in range(27):
			prefix[i][j] += prefix[i - 1][j]
	maxi = 0
	for a in range(27):
		for b in range(27):
			i = 0
			j = n - 1
			if arr[0] == a:
				icount = 1
			else:
				icount = 0
			if arr[-1] == a:
				jcount = 1
			else:
				jcount = 0
			while i < j:
				if icount <= jcount:
					maxi = max(maxi, 2 * icount + func(i, j, a, b))
				if icount <= jcount:
					i += 1
					if arr[i] == a:
						icount += 1
				else:
					j -= 1
					if arr[j] == a:
						jcount += 1
	if maxi == 0:
		print(1)
	else:
		print(maxi)


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	dic = {}
	flag = 0
	for i in range(n):
		if arr[i] in dic:
			dic[arr[i]].append(i)
		else:
			dic[arr[i]] = [i]
	for i in dic.keys():
		if len(dic[i]) > 2:
			flag = 1
			break
		elif len(dic[i]) == 2:
			if abs(dic[i][0] - dic[i][1]) > 1:
				flag = 1
				break
	if flag == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
maxa = 27
for _ in range(t):
	n = int(input())
	ar = [int(x) for x in input().split()]
	tbc = []
	maxleng = 0
	leng = 1
	ftot = [[0 for j in range(n)] for i in range(maxa + 1)]
	btot = [[0 for j in range(n)] for i in range(maxa + 1)]
	for i in range(n):
		for j in range(1, maxa + 1):
			ftot[j][i] = ftot[j][i - 1]
			if ar[i] == j:
				ftot[j][i] += 1
	arrev = ar[::-1]
	for i in range(n):
		for j in range(1, maxa + 1):
			btot[j][i] = btot[j][i - 1]
			if arrev[i] == j:
				btot[j][i] += 1
	for i in range(len(btot)):
		btot[i] = btot[i][::-1]
	for i in range(n):
		for j in range(i, n):
			if ar[i] == ar[j]:
				val = ar[i]
				leng = 1 + ftot[val][j] - ftot[val][i]
				maxext = 0
				for k in range(1, maxa + 1):
					if k != val:
						ext = 2 * min(ftot[k][i], btot[k][j])
						if ext > maxext:
							maxext = ext
				leng += maxext
				if leng > maxleng:
					maxleng = leng
	print(maxleng)


t = int(input())
for _ in range(t):
	n = int(input())
	s = [int(i) for i in input().split()]
	prev = [[0] * 201] * n
	pos = [[] for i in range(201)]
	for i in range(n):
		prev[i] = prev[i - 1].copy()
		prev[i][s[i]] += 1
		pos[s[i]].append(i)
	ans = 0
	for i in range(201):
		ans = max(ans, len(pos[i]))
		for j in range(1, len(pos[i]) // 2 + 1):
			m = 0
			for k in range(201):
				m = max(m, prev[pos[i][-j] - 1][k] - prev[pos[i][j - 1]][k])
			ans = max(ans, m + 2 * j)
	print(ans)


test_cases = int(input())
for i in range(test_cases):
	found = False
	in_string_length = int(input())
	in_string = list(map(int, input().split()))
	for j in range(in_string_length - 2):
		c = in_string[j]
		if not found and c in in_string[j + 2:]:
			found = True
			print('YES')
			break
	if not found:
		print('NO')


from collections import deque

def solve2_d(a):
	d1 = deque((i for i in range(len(a)) if a[i] == 1))
	d2 = deque((i for i in range(len(a)) if a[i] == 2))
	d1_left = []
	d1_right = []
	best = len(d2)
	while len(d1) >= 2:
		d1_left.append(d1.popleft())
		d1_right.append(d1.pop())
		while len(d2) > 0 and d2[0] < d1_left[-1]:
			d2.popleft()
		while len(d2) > 0 and d2[-1] > d1_right[-1]:
			d2.pop()
		best = max(best, len(d1_left) + len(d1_right) + len(d2))
	return best
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(v) for v in input().split()]
	best = -1
	for x in range(1, 26 + 1):
		for y in range(1, 26 + 1):
			a2 = [v for v in a if v == x or v == y]
			a2 = [1 if v == x else 2 for v in a2]
			best = max(best, sum((1 for v in a2 if v == 1)))
			best = max(best, sum((1 for v in a2 if v == 2)))
			if x != y:
				best = max(best, solve2_d(a2))
	print(best)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	for i in range(5001):
		c = [-1]
		b.append(c)
	for i in range(n):
		d = a[i]
		if b[d][-1] == i:
			continue
		else:
			b[d].append(i + 1)
	ch = 0
	for i in range(1, 5001):
		if len(b[i]) > 2:
			ch = 1
			break
	if ch == 1:
		print('YES')
	else:
		print('NO')


from collections import Counter, defaultdict, deque
from heapq import heappop, heappush, heapify
import sys, bisect, math, itertools, fractions, pprint
sys.setrecursionlimit(10 ** 8)
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))

def inpln(n):
	return list((int(sys.stdin.readline()) for i in range(n)))
T = inp()
for _ in range(T):
	n = inp()
	a = inpl()
	s = set()
	d = defaultdict(int)
	for (i, t) in enumerate(a):
		if t in s:
			if d[t] != i - 1:
				print('YES')
				break
			d[t] = -1
		else:
			s.add(t)
			d[t] = i
	else:
		print('NO')


for _ in range(int(input())):
	w = int(input())
	data = list(map(int, input().split()))
	flag = False
	for i in range(w - 2):
		if data[i] in data[i + 2:]:
			flag = True
			break
	if flag == True:
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	al = list(map(int, input().split()))
	for i in range(n):
		for j in range(i + 2, n):
			if al[i] == al[j]:
				print('YES')
				return
	print('NO')

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()


for _ in range(int(input())):
	n = int(input())
	dic = {i: [] for i in range(1, n + 1)}
	l = [int(i) for i in input().split()]
	for (i, v) in enumerate(l):
		dic[v].append(i)
	for i in range(1, n + 1):
		if len(dic[i]) >= 2:
			if abs(dic[i][0] - dic[i][-1]) > 1:
				print('YES')
				break
	else:
		print('NO')


from sys import stdin
import sys
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().split()))
	dp = [[0] * (n + 1) for i in range(200)]
	pos = [[] for i in range(200)]
	for i in range(n):
		for c in range(200):
			dp[c][i + 1] = dp[c][i]
		dp[arr[i] - 1][i + 1] += 1
		pos[arr[i] - 1].append(i)
	ans = 0
	for i in range(200):
		ans = max(ans, len(pos[i]))
		for j in range(len(pos[i]) // 2):
			l = pos[i][j] + 1
			r = pos[i][len(pos[i]) - j - 1] - 1
			for k in range(200):
				res = dp[k][r + 1] - dp[k][l]
				ans = max(ans, 2 * (j + 1) + res)
	print(ans)