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.

Difficulty: EASY

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


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')


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)


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')


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')


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 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')


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')


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')


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')


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)


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 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')


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')


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())


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 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()


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 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')


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')


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
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)


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')


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')


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])


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)


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')


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')


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')


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


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())
	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')


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 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)


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')


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
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)


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 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()


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')


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')


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')


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())):
	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
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)


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 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 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')


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')


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')


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')


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')


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')


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())
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)


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')


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')


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))


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')


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')


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()


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)


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')


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())
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')


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)


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')


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 = [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')


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')


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())
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


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')


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)


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 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())


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')


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())


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()


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')


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')


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)


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')


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')


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')


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')


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')


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 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')


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')


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


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')


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()


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')


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')


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 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')


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


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 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')


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')


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')


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')


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')


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')


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 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)


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')


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')


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))


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')


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')


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')


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')


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 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())
	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')


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 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')


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')


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())

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')


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


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')


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')


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')


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)


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')


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')


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))


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')


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')


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')


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')


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 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

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')


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()


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')


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')


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')


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 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')


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 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())


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())
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()


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')


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)))


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')


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')


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')


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')


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')


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')


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')


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')


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')


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()


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()))]


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')


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))


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')


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


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')


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()


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')


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')


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')


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')


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')


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')


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 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')


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)


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 _ 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')


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')


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()


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')


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')


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')


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)


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')


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())
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])


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')


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')


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')


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())
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')


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)


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')


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')


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')


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))


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()


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()


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')


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)


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))


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')


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')


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

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 = 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)


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')


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')


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')


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')


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()


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')


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')


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())
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 ' ' * 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')


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')


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')


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')


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))


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')


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)


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')


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')


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)


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
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')


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()


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')


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')


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)


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')


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')


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()


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')


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')


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')


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 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')


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))


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()


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')


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')


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)


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')


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()


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')


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')


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()


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')


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')


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 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')


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')


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')


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()


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')


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')


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')


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()


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')


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')


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()