import sys
input = sys.stdin.readline
from math import *
t=int(input())
while t>0:
    t-=1
    n=int(input())
    a=[int(x) for x in input().split()]
    ff=[{} for i in range(201)]
    fl=[{} for i in range(201)]
    count=[0 for i in range(201)]
    cal=[[0 for i in range(201)] for i in range(n+1)]
    for i in range(n):
        for j in range(1,201):
            if i==0:
                cal[i][j]=int(a[i]==j)
            else:
                cal[i][j]=cal[i-1][j]+int(a[i]==j)
    for i in range(n):
            count[a[i]]+=1
            ff[a[i]][count[a[i]]]=i
    count=[0 for i in range(27)]
    for i in range(n-1,-1,-1):
            count[a[i]]+=1
            fl[a[i]][count[a[i]]]=i
    maxi=1

    for i in range(1,201):
        for j in ff[i]:
            l=ff[i][j]
            if j not in fl[i]:
                maxi=max(maxi,j)
                continue
            r=fl[i][j]
            c=2*j

            #print(c,maxi,i,fl[i][j],ff[i][j])
            g=0
            for k in range(1,201):
                if r-1<l:
                    continue
                g=cal[r-1][k]-cal[l][k]
                maxi=max(maxi,c+g)
    print(maxi)


import sys
input = sys.stdin.readline
from math import *
t=int(input())
while t>0:
    t-=1
    n=int(input())
    a=[int(x) for x in input().split()]
    ff=[{} for i in range(201)]
    fl=[{} for i in range(201)]
    count=[0 for i in range(201)]
    cal=[[0 for i in range(201)] for i in range(n+1)]
    for i in range(n):
        for j in range(1,201):
            if i==0:
                cal[i][j]=int(a[i]==j)
            else:
                cal[i][j]=cal[i-1][j]+int(a[i]==j)
    for i in range(n):
            count[a[i]]+=1
            ff[a[i]][count[a[i]]]=i
    count=[0 for i in range(201)]
    for i in range(n-1,-1,-1):
            count[a[i]]+=1
            fl[a[i]][count[a[i]]]=i
    maxi=1

    for i in range(1,201):
        for j in ff[i]:
            l=ff[i][j]
            if j not in fl[i]:
                maxi=max(maxi,j)
                continue
            r=fl[i][j]
            c=2*j

            #print(c,maxi,i,fl[i][j],ff[i][j])
            g=0
            for k in range(1,201):
                if r-1<l:
                    continue
                g=cal[r-1][k]-cal[l][k]
                maxi=max(maxi,c+g)
    print(maxi)


def find_leftmost_right(a, k):
    return indices[a][-k]

def most_repeats(j1, j2):
    if j2 >= j1:
        return max(repeats[j0][j2]-repeats[j0][j1-1] for j0 in range(200))
    return 0

T = int(input())

for t in range(T):
    N = int(input())
    A = list(map(int, input().split()))

    repeats = [[0]*N for _ in range(200)]
    indices = [[] for _ in range(200)]
    for i in range(0, N):
        if i > 0:
            for j in range(200):
                repeats[j][i] = repeats[j][i-1]
        repeats[A[i]-1][i] += 1
        indices[A[i]].append(i)

    result = 1
    for i1 in range(len(A)):
        c = repeats[A[i1]-1][i1]
        i2 = find_leftmost_right(A[i1], c)

        if i2 > i1:
            # print("{}\n c is {} i1 is {} i2 is {} most_repeats is {}".
            #       format(A, c, i1, i2, most_repeats(i1 + 1, i2 - 1)))
            result = max(result, 2 * c + most_repeats(i1+1, i2-1))
    print(result)


from sys import stdin, gettrace

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

T = int(input())

for t in range(T):
    N = int(input())
    A = list(map(int, input().split()))

    repeats = [[0]*N for _ in range(200)]
    # indices = [[] for _ in range(200)]
    for i in range(0, N):
        if i > 0:
            for j in range(200):
                repeats[j][i] = repeats[j][i-1]
        repeats[A[i]-1][i] += 1
        # indices[A[i]-1].append(i)

    result = 1
    for a in range(200):
        i1 = 0
        i2 = len(A)-1
        while i2 > i1:
            while i1 < len(A) and A[i1]-1 != a:
                i1 += 1
            while i2 >= 0 and A[i2]-1 != a:
                i2 -= 1
            if i2 > i1:
                c = repeats[A[i1]-1][i1]
                mr = max(repeats[j0][i2-1]-repeats[j0][i1] for j0 in range(200))
                result = max(result, 2 * c + mr)
            i1 += 1
            i2 -= 1

    print(result)


from sys import stdin, gettrace

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

T = int(input())

for t in range(T):
    N = int(input())
    A = list(map(int, input().split()))

    repeats = [[0]*N for _ in range(200)]
    indices = [[] for _ in range(200)]
    for i in range(0, N):
        if i > 0:
            for j in range(200):
                repeats[j][i] = repeats[j][i-1]
        repeats[A[i]-1][i] += 1
        indices[A[i]-1].append(i)

    result = 1
    for i1 in range(len(A)):
        c = repeats[A[i1]-1][i1]
        i2 = indices[A[i1]-1][-c]
        if i2 > i1:
            mr = max(repeats[j0][i2-1]-repeats[j0][i1] for j0 in range(200))
            result = max(result, 2 * c + mr)
    print(result)


T = int(input())

for t in range(T):
    N = int(input())
    A = list(map(int, input().split()))

    repeats = [[0]*N for _ in range(200)]
    indices = [[] for _ in range(200)]
    for i in range(0, N):
        if i > 0:
            for j in range(200):
                repeats[j][i] = repeats[j][i-1]
        repeats[A[i]-1][i] += 1
        indices[A[i]-1].append(i)

    result = 1
    for i1 in range(len(A)):
        c = repeats[A[i1]-1][i1]
        i2 = indices[A[i1]-1][-c]
        if i2 > i1:
            mr = max(repeats[j0][i2-1]-repeats[j0][i1] for j0 in range(200))
            result = max(result, 2 * c + mr)
    print(result)


def find_leftmost_right(a, k):
    return indices[a-1][-k]

def most_repeats(j1, j2):
    if j2 >= j1:
        return max(repeats[j0][j2]-repeats[j0][j1-1] for j0 in range(200))
    return 0

T = int(input())

for t in range(T):
    N = int(input())
    A = list(map(int, input().split()))

    repeats = [[0]*N for _ in range(200)]
    indices = [[] for _ in range(200)]
    for i in range(0, N):
        if i > 0:
            for j in range(200):
                repeats[j][i] = repeats[j][i-1]
        repeats[A[i]-1][i] += 1
        indices[A[i]-1].append(i)

    result = 1
    for i1 in range(len(A)):
        c = repeats[A[i1]-1][i1]
        i2 = find_leftmost_right(A[i1], c)

        if i2 > i1:
            # print("{}\n c is {} i1 is {} i2 is {} most_repeats is {}".
            #       format(A, c, i1, i2, most_repeats(i1 + 1, i2 - 1)))
            result = max(result, 2 * c + most_repeats(i1+1, i2-1))
    print(result)


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

def main():
    t = int(input())

    for _ in range(t):
        n = int(input())
        A = list(map(int, input().split()))
        C = [[0]*(n+1) for _ in range(200)]
        P = [[] for _ in range(200)]
        for i in range(n):
            C[A[i]-1][i+1] = 1
            P[A[i]-1].append(i)
        #print(C[0:10])

        for i in range(200):
            for j in range(1, n+1):
                C[i][j] += C[i][j-1]
        #print(P[0:10])

        ans = 0
        for i in range(200):
            if len(P[i]) >= ans:
                ans = len(P[i])
            if len(P[i]) == 0 or len(P[i]) == 1:
                continue
            for j in range(len(P[i])//2+1):
                l = P[i][j]
                r = P[i][-(j+1)]
                if r <= l:
                    continue
                for k in range(200):
                    temp = 2*(j+1)+C[k][r]-C[k][l+1]
                    if temp >= ans:
                        ans = temp
                    #print(i, j, k, l, r, ans)
        print(ans)

if __name__ == '__main__':
    main()


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

import sys
input = sys.stdin.buffer.readline

def main():
    t = int(input())

    for _ in range(t):
        n = int(input())
        A = list(map(int, input().split()))
        C = [[0]*(n+1) for _ in range(200)]
        P = [[] for _ in range(200)]
        for i in range(n):
            for j in range(200):
                if j == A[i]-1:
                    C[j][i+1] = C[j][i]+1
                    P[j].append(i)
                else:
                    C[j][i+1] = C[j][i]
        #print(C[0:10])

        #for i in range(200):
            #for j in range(1, n+1):
                #C[i][j] += C[i][j-1]
        #print(P[0:10])

        ans = 0
        for i in range(200):
            if len(P[i]) >= ans:
                ans = len(P[i])
            if len(P[i]) == 0 or len(P[i]) == 1:
                continue
            for j in range(len(P[i])//2+1):
                l = P[i][j]
                r = P[i][-(j+1)]
                if r <= l:
                    continue
                for k in range(200):
                    temp = 2*(j+1)+C[k][r]-C[k][l+1]
                    if temp >= ans:
                        ans = temp
                    #print(i, j, k, l, r, ans)
        print(ans)

if __name__ == '__main__':
    main()


import sys
input = sys.stdin.buffer.readline

def main():
    t = int(input())

    for _ in range(t):
        n = int(input())
        A = list(map(int, input().split()))
        C = [[0]*(n+1) for _ in range(200)]
        P = [[] for _ in range(200)]
        for i in range(n):
            C[A[i]-1][i+1] = 1
            P[A[i]-1].append(i)
        #print(C[0:10])

        for i in range(200):
            for j in range(1, n+1):
                C[i][j] += C[i][j-1]
        #print(P[0:10])

        ans = 0
        for i in range(200):
            ans = max(ans, len(P[i]))
            if len(P[i]) == 0 or len(P[i]) == 1:
                continue
            for j in range(len(P[i])//2+1):
                l = P[i][j]
                r = P[i][-(j+1)]
                if r <= l:
                    continue
                for k in range(200):
                    ans = max(ans, 2*(j+1)+C[k][r]-C[k][l+1])
                    #print(i, j, k, l, r, ans)
        print(ans)

if __name__ == '__main__':
    main()


import sys
input = sys.stdin.buffer.readline

def main():
    t = int(input())

    for _ in range(t):
        n = int(input())
        A = list(map(int, input().split()))
        C = [[0]*(n+1) for _ in range(200)]
        P = [[] for _ in range(200)]
        for i in range(n):
            C[A[i]-1][i+1] = 1
            P[A[i]-1].append(i)
        #print(C[0:10])

        for i in range(200):
            for j in range(1, n+1):
                C[i][j] += C[i][j-1]
        #print(P[0:10])

        ans = 0
        for i in range(200):
            if len(P[i]) >= ans:
                ans = len(P[i])
            if len(P[i]) == 0 or len(P[i]) == 1:
                continue
            for j in range(len(P[i])//2+1):
                l = P[i][j]
                r = P[i][-(j+1)]
                if r <= l:
                    continue
                for k in range(200):
                    temp = 2*(j+1)+C[k][r]-C[k][l+1]
                    if temp >= ans:
                        ans = temp
                    #print(i, j, k, l, r, ans)
        print(ans)

if __name__ == '__main__':
    main()


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

#import sys
#input = sys.stdin.buffer.readline

def main():
    t = int(input())

    for _ in range(t):
        n = int(input())
        A = list(map(int, input().split()))
        C = [[0]*(n+1) for _ in range(200)]
        P = [[] for _ in range(200)]
        for i in range(n):
            for j in range(200):
                if j == A[i]-1:
                    C[j][i+1] = C[j][i]+1
                    P[j].append(i)
                else:
                    C[j][i+1] = C[j][i]
        #print(C[0:10])

        #for i in range(200):
            #for j in range(1, n+1):
                #C[i][j] += C[i][j-1]
        #print(P[0:10])

        ans = 0
        for i in range(200):
            if len(P[i]) >= ans:
                ans = len(P[i])
            if len(P[i]) == 0 or len(P[i]) == 1:
                continue
            for j in range(len(P[i])//2+1):
                l = P[i][j]
                r = P[i][-(j+1)]
                if r <= l:
                    continue
                for k in range(200):
                    temp = 2*(j+1)+C[k][r]-C[k][l+1]
                    if temp >= ans:
                        ans = temp
                    #print(i, j, k, l, r, ans)
        print(ans)

if __name__ == '__main__':
    main()


#import math
#from functools import lru_cache
#import heapq
from collections import defaultdict
#from collections import Counter
#from sys import stdout
#from sys import setrecursionlimit
from sys import stdin
input = stdin.readline
for ti in range(int(input().strip())):
    n = int(input())
    a = [int(x) for x in input().strip().split()]
    d = defaultdict(list)
    ans = 0
    cnt = [[0]*(n+1) for _ in range(201)]
    for i in range(n):
        d[a[i]].append(i)
        for j in range(201): cnt[j][i] = cnt[j][i-1]
        cnt[a[i]][i]+=1
    #print(cnt[:4])
    for di in d:
        dil = len(d[di])
        for dj in range(dil//2):
            si, ei = d[di][dj], d[di][dil - dj - 1]
            mc = 0
            for j in range(201):
                mc = max(mc, cnt[j][ei-1] - cnt[j][si])
            ans = max(ans, 2*(dj+1) + mc)
    for di in d: ans = max(ans, len(d[di]))
    print(ans)


from sys import stdin, gettrace

# if not gettrace():
#     def input():
#         return next(stdin)[:-1]

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

def main():
    def solve():

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

    q = int(input())
    for _ in range(q):
        solve()

if __name__ == "__main__":
    main()


t = int(input())

for ti in range(t):
	n = int(input())
	a = [int(i) - 1 for i in input().split(' ')]

	dp = [[0 for i in range(200)] for i in range(n)]
	for i in range(n):
		for j in range(200):
			dp[i][j] = dp[i - 1][j]
		dp[i][a[i]] += 1

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


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


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


import java.util.*
import java.math.*

fun readLn() = readLine()!!
fun readInt() = readLn().toInt()
fun readInts()  = readLn().split(" ").map{it.toInt()}
fun readLong() = readLn().toLong()
fun readLongs()  = readLn().split(" ").map{it.toLong()}

val out = mutableListOf<String>()
fun printLine(s: String) { out.add(s) }
fun output() { println(out.joinToString("\n")) }

fun main(){
    var t = readInt()
    for(tc in 1..t){
        var n = readInt()
        var a = readInts()
        var cnt = arrayOfNulls<IntArray>(201)
        for(j in 1..200){
            cnt[j] = IntArray(n+1)
            for(i in 0 until n){
                cnt[j]!![i+1] = cnt[j]!![i]
                if(a[i]==j)
                    cnt[j]!![i+1]++
            }
        }
        fun get(l:Int, r:Int):Int {
            var ans = 0
            for(j in 1..200)
                ans = Math.max(ans,cnt[j]!![r]-cnt[j]!![l-1])
            return ans
        }
        var idx = Array(201,{ArrayList<Int>()})
        for(i in 0 until n){
            idx[a[i]].add(i+1)
        }
        var ans = get(1,n)
        for(j in 1..200){
            var sz = idx[j].size
            for(i in 0 until sz/2)
                ans = Math.max(ans,(i+1)*2+get(idx[j][i]+1,idx[j][sz-i-1]-1))
        }
        printLine(ans.toString())
    }
    output()
}


import java.util.*
import java.io.*
//import kotlin.math.*

fun main() {
	val inputStream = System.`in`
	val outputStream = System.out
	val `in` = Main.InputReader(inputStream)
	val out = PrintWriter(outputStream)
	val solver = Main.TaskD()
	solver.solve(1, `in`, out)
	out.close()
}

object Main {
	@JvmStatic
	fun main(args: Array<String>) {
		val inputStream = System.`in`
		val outputStream = System.out
		val `in` = InputReader(inputStream)
		val out = PrintWriter(outputStream)
		val solver = TaskD()
		solver.solve(1, `in`, out)
		out.close()
	}

	internal class TaskD {
		fun solve(testNumber: Int, `in`: InputReader, out: PrintWriter) {
			val INF = 1000000000
			var s = Array<Array<Int>>(200010, {Array<Int>(201, {-INF})})

			var tests = `in`.nextInt()
			for (test in 1..tests)
			{
				var n = `in`.nextInt()
				var a = Array<Int>(n, {`in`.nextInt()})
				var v = Array<ArrayList<Int>>(201, {arrayListOf()})
				for (i in 1..n)
				{
					for (j in 1..200)
					{
						s[i][j] = s[i-1][j]
					}
					s[i][a[i-1]] += 1
					v[a[i-1]].add(i)
				}
				var res = 0
				for (j in 1..200)
				{
					var len = v[j].size
					for (k in 0..len/2)
					{
						var x = 0
						var y = 0
						if (k == 0)
						{
							x = 0
							y = n+1
						}
						else
						{
							x = v[j][k-1]
							y = v[j][len-k]
						}
						for (i in 1..200)
						{
							res = Math.max(res, k*2+s[y-1][i]-s[x][i])
						}
					}
				}
				out.println(res)
			}

		}

	}

	internal class InputReader(stream: InputStream) {
		var reader: BufferedReader
		var tokenizer: StringTokenizer? = null

		init {
			reader = BufferedReader(InputStreamReader(stream), 32768)
			tokenizer = null
		}

		operator fun next(): String {
			while (tokenizer == null || !tokenizer!!.hasMoreTokens()) {
				try {
					tokenizer = StringTokenizer(reader.readLine())
				} catch (e: IOException) {
					throw RuntimeException(e)
				}

			}
			return tokenizer!!.nextToken()
		}

		fun nextInt(): Int {
			return Integer.parseInt(next())
		}

		fun nextLong(): Long {
		    return next().toLong()
		}

	}
}


import java.util.*
import java.io.*
//import kotlin.math.*

fun main() {
	val inputStream = System.`in`
	val outputStream = System.out
	val `in` = Main.InputReader(inputStream)
	val out = PrintWriter(outputStream)
	val solver = Main.TaskD()
	solver.solve(1, `in`, out)
	out.close()
}

object Main {
	@JvmStatic
	fun main(args: Array<String>) {
		val inputStream = System.`in`
		val outputStream = System.out
		val `in` = InputReader(inputStream)
		val out = PrintWriter(outputStream)
		val solver = TaskD()
		solver.solve(1, `in`, out)
		out.close()
	}

	internal class TaskD {
		fun solve(testNumber: Int, `in`: InputReader, out: PrintWriter) {
			val INF = 1000000000
			var s = Array<IntArray>(200010, {IntArray(201, {-INF})})

			var tests = `in`.nextInt()
			for (test in 1..tests)
			{
				var n = `in`.nextInt()
				var a = Array<Int>(n, {`in`.nextInt()})
				var v = Array<ArrayList<Int>>(201, {arrayListOf()})
				for (i in 1..n)
				{
					for (j in 1..200)
					{
						s[i][j] = s[i-1][j]
					}
					s[i][a[i-1]] += 1
					v[a[i-1]].add(i)
				}
				var res = 0
				for (j in 1..200)
				{
					var len = v[j].size
					for (k in 0..len/2)
					{
						var x = 0
						var y = 0
						if (k == 0)
						{
							x = 0
							y = n+1
						}
						else
						{
							x = v[j][k-1]
							y = v[j][len-k]
						}
						for (i in 1..200)
						{
							res = Math.max(res, k*2+s[y-1][i]-s[x][i])
						}
					}
				}
				out.println(res)
			}

		}

	}

	internal class InputReader(stream: InputStream) {
		var reader: BufferedReader
		var tokenizer: StringTokenizer? = null

		init {
			reader = BufferedReader(InputStreamReader(stream), 32768)
			tokenizer = null
		}

		operator fun next(): String {
			while (tokenizer == null || !tokenizer!!.hasMoreTokens()) {
				try {
					tokenizer = StringTokenizer(reader.readLine())
				} catch (e: IOException) {
					throw RuntimeException(e)
				}

			}
			return tokenizer!!.nextToken()
		}

		fun nextInt(): Int {
			return Integer.parseInt(next())
		}

		fun nextLong(): Long {
		    return next().toLong()
		}

	}
}


import java.util.*
import java.io.*
//import kotlin.math.*

fun main() {
	val inputStream = System.`in`
	val outputStream = System.out
	val `in` = Main.InputReader(inputStream)
	val out = PrintWriter(outputStream)
	val solver = Main.TaskD()
	solver.solve(1, `in`, out)
	out.close()
}

object Main {
	@JvmStatic
	fun main(args: Array<String>) {
		val inputStream = System.`in`
		val outputStream = System.out
		val `in` = InputReader(inputStream)
		val out = PrintWriter(outputStream)
		val solver = TaskD()
		solver.solve(1, `in`, out)
		out.close()
	}

	internal class TaskD {
		fun solve(testNumber: Int, `in`: InputReader, out: PrintWriter) {
			val INF = 1000000000
			var s = Array<IntArray>(201, {IntArray(200010, {0})})

			var tests = `in`.nextInt()
			for (test in 1..tests)
			{
				var n = `in`.nextInt()
				var a = Array<Int>(n, {`in`.nextInt()})
				var v = Array<ArrayList<Int>>(201, {arrayListOf()})
				for (j in 1..200)
				{
					for (i in 1..n)
					{
						s[j][i] = s[j][i-1]
						if (a[i-1] == j) s[j][i] += 1
					}
				}
				for (i in 1..n)
				{
					v[a[i-1]].add(i)
				}
				var res = 0
				for (j in 1..200)
				{
					var len = v[j].size
					for (k in 0..len/2)
					{
						var x = 0
						var y = 0
						if (k == 0)
						{
							x = 0
							y = n+1
						}
						else
						{
							x = v[j][k-1]
							y = v[j][len-k]
						}
						for (i in 1..200)
						{
							res = Math.max(res, k*2+s[i][y-1]-s[i][x])
						}
					}
				}
				out.println(res)
			}

		}

	}

	internal class InputReader(stream: InputStream) {
		var reader: BufferedReader
		var tokenizer: StringTokenizer? = null

		init {
			reader = BufferedReader(InputStreamReader(stream), 32768)
			tokenizer = null
		}

		operator fun next(): String {
			while (tokenizer == null || !tokenizer!!.hasMoreTokens()) {
				try {
					tokenizer = StringTokenizer(reader.readLine())
				} catch (e: IOException) {
					throw RuntimeException(e)
				}

			}
			return tokenizer!!.nextToken()
		}

		fun nextInt(): Int {
			return Integer.parseInt(next())
		}

		fun nextLong(): Long {
		    return next().toLong()
		}

	}
}


import java.util.*
import java.io.*
//import kotlin.math.*

fun main() {
	val inputStream = System.`in`
	val outputStream = System.out
	val `in` = Main.InputReader(inputStream)
	val out = PrintWriter(outputStream)
	val solver = Main.TaskD()
	solver.solve(1, `in`, out)
	out.close()
}

object Main {
	@JvmStatic
	fun main(args: Array<String>) {
		val inputStream = System.`in`
		val outputStream = System.out
		val `in` = InputReader(inputStream)
		val out = PrintWriter(outputStream)
		val solver = TaskD()
		solver.solve(1, `in`, out)
		out.close()
	}

	internal class TaskD {
		fun solve(testNumber: Int, `in`: InputReader, out: PrintWriter) {
			val INF = 1000000000

			var tests = `in`.nextInt()
			for (test in 1..tests)
			{
				var n = `in`.nextInt()
				var a = Array<Int>(n, {`in`.nextInt()})
				var s = Array<Array<Int>>(n+1, {Array<Int>(201, {-INF})})
				var v = Array<ArrayList<Int>>(201, {arrayListOf()})
				for (i in 1..n)
				{
					for (j in 1..200)
					{
						s[i][j] = s[i-1][j]
					}
					s[i][a[i-1]] += 1
					v[a[i-1]].add(i)
				}
				var res = 0
				for (j in 1..200)
				{
					var len = v[j].size
					for (k in 0..len/2)
					{
						var x = 0
						var y = 0
						if (k == 0)
						{
							x = 0
							y = n+1
						}
						else
						{
							x = v[j][k-1]
							y = v[j][len-k]
						}
						for (i in 1..200)
						{
							res = Math.max(res, k*2+s[y-1][i]-s[x][i])
						}
					}
				}
				out.println(res)
			}

		}

	}

	internal class InputReader(stream: InputStream) {
		var reader: BufferedReader
		var tokenizer: StringTokenizer? = null

		init {
			reader = BufferedReader(InputStreamReader(stream), 32768)
			tokenizer = null
		}

		operator fun next(): String {
			while (tokenizer == null || !tokenizer!!.hasMoreTokens()) {
				try {
					tokenizer = StringTokenizer(reader.readLine())
				} catch (e: IOException) {
					throw RuntimeException(e)
				}

			}
			return tokenizer!!.nextToken()
		}

		fun nextInt(): Int {
			return Integer.parseInt(next())
		}

		fun nextLong(): Long {
		    return next().toLong()
		}

	}
}


import java.util.*
import java.io.*
//import kotlin.math.*

fun main() {
	val inputStream = System.`in`
	val outputStream = System.out
	val `in` = Main.InputReader(inputStream)
	val out = PrintWriter(outputStream)
	val solver = Main.TaskD()
	solver.solve(1, `in`, out)
	out.close()
}

object Main {
	@JvmStatic
	fun main(args: Array<String>) {
		val inputStream = System.`in`
		val outputStream = System.out
		val `in` = InputReader(inputStream)
		val out = PrintWriter(outputStream)
		val solver = TaskD()
		solver.solve(1, `in`, out)
		out.close()
	}

	internal class TaskD {
		fun solve(testNumber: Int, `in`: InputReader, out: PrintWriter) {
			val INF = 1000000000
			var s = Array<IntArray>(201, {IntArray(200010, {0})})

			var tests = `in`.nextInt()
			for (test in 1..tests)
			{
				var n = `in`.nextInt()
				var a = Array<Int>(n, {`in`.nextInt()})
				var v = Array<ArrayList<Int>>(201, {arrayListOf()})
				for (j in 1..200)
				{
					for (i in 1..n)
					{
						s[j][i] = s[j][i-1]
						if (a[i-1] == j) s[j][i] += 1
					}
				}
				for (i in 1..n)
				{
					v[a[i-1]].add(i)
				}
				var res = 0
				for (j in 1..200)
				{
					var len = v[j].size
					for (k in 0..len/2)
					{
						var x = 0
						var y = 0
						if (k == 0)
						{
							x = 0
							y = n+1
						}
						else
						{
							x = v[j][k-1]
							y = v[j][len-k]
						}
						for (i in 1..200)
						{
							res = Math.max(res, k*2+s[y-1][i]-s[x][i])
						}
					}
				}
				out.println(res)
			}

		}

	}

	internal class InputReader(stream: InputStream) {
		var reader: BufferedReader
		var tokenizer: StringTokenizer? = null

		init {
			reader = BufferedReader(InputStreamReader(stream), 32768)
			tokenizer = null
		}

		operator fun next(): String {
			while (tokenizer == null || !tokenizer!!.hasMoreTokens()) {
				try {
					tokenizer = StringTokenizer(reader.readLine())
				} catch (e: IOException) {
					throw RuntimeException(e)
				}

			}
			return tokenizer!!.nextToken()
		}

		fun nextInt(): Int {
			return Integer.parseInt(next())
		}

		fun nextLong(): Long {
		    return next().toLong()
		}

	}
}


import java.util.*
import java.io.*
//import kotlin.math.*

fun main() {
	val inputStream = System.`in`
	val outputStream = System.out
	val `in` = Main.InputReader(inputStream)
	val out = PrintWriter(outputStream)
	val solver = Main.TaskD()
	solver.solve(1, `in`, out)
	out.close()
}

object Main {
	@JvmStatic
	fun main(args: Array<String>) {
		val inputStream = System.`in`
		val outputStream = System.out
		val `in` = InputReader(inputStream)
		val out = PrintWriter(outputStream)
		val solver = TaskD()
		solver.solve(1, `in`, out)
		out.close()
	}

	internal class TaskD {
		fun solve(testNumber: Int, `in`: InputReader, out: PrintWriter) {
			val INF = 1000000000
			var s = Array<IntArray>(200010, {IntArray(201, {-INF})})

			var tests = `in`.nextInt()
			for (test in 1..tests)
			{
				var n = `in`.nextInt()
				var a = IntArray(n, {`in`.nextInt()})
				var v = Array<ArrayList<Int>>(201, {arrayListOf()})
				for (i in 1..n)
				{
					for (j in 1..200)
					{
						s[i][j] = s[i-1][j]
					}
					s[i][a[i-1]] += 1
					v[a[i-1]].add(i)
				}
				var res = 0
				for (j in 1..200)
				{
					var len = v[j].size
					for (k in 0..len/2)
					{
						var x = 0
						var y = 0
						if (k == 0)
						{
							x = 0
							y = n+1
						}
						else
						{
							x = v[j][k-1]
							y = v[j][len-k]
						}
						for (i in 1..200)
						{
							res = Math.max(res, k*2+s[y-1][i]-s[x][i])
						}
					}
				}
				out.println(res)
			}

		}

	}

	internal class InputReader(stream: InputStream) {
		var reader: BufferedReader
		var tokenizer: StringTokenizer? = null

		init {
			reader = BufferedReader(InputStreamReader(stream), 32768)
			tokenizer = null
		}

		operator fun next(): String {
			while (tokenizer == null || !tokenizer!!.hasMoreTokens()) {
				try {
					tokenizer = StringTokenizer(reader.readLine())
				} catch (e: IOException) {
					throw RuntimeException(e)
				}

			}
			return tokenizer!!.nextToken()
		}

		fun nextInt(): Int {
			return Integer.parseInt(next())
		}

		fun nextLong(): Long {
		    return next().toLong()
		}

	}
}


@file:Suppress("NOTHING_TO_INLINE", "EXPERIMENTAL_FEATURE_WARNING", "OVERRIDE_BY_INLINE")

import java.io.PrintWriter
import java.util.PriorityQueue
import kotlin.math.*
import kotlin.random.Random
import kotlin.collections.sort as _sort
import kotlin.collections.sortDescending as _sortDescending
import kotlin.io.println as iprintln

/** @author Spheniscine */
fun main() { _writer.solve(); _writer.flush() }
fun PrintWriter.solve() {
    val numCases = readInt()
    case@ for(case in 1..numCases) {
        //print("Case #$case: ")

        object {
            val n = readInt()
            val A = readIntArray(n)

            val aMax = A.max()!!

            val pref = Array(aMax+1) { IntArray(n+1) }

            val pos = Array(aMax+1) { IntList() }.also { pos ->
                for(i in 0 until n) {
                    pos[A[i]].add(i)
                    pref[A[i]][i+1] = 1
                }

                for(a in 1..aMax) {
                    for(i in 1..n) {
                        pref[a][i] += pref[a][i-1]
                    }
                }
            }

            fun cnt(a: Int, l: Int, rx: Int) = pref[a][rx] - pref[a][l]

            val cnt = IntArray(aMax+1)

            var ans = 0
            fun yield(v: Int) { if(v > ans) ans = v }

            init {
                for(a in A) {
                    val c = cnt[a]
                    val posa = pos[a]
                    val l = posa[c] + 1
                    val rx = posa[posa.lastIndex - c]
                    if(rx > l) {
                        val x = (c+1) * 2
                        for(b in 1..aMax) yield(x + cnt(b, l, rx))
                    }
                    cnt[a]++
                }

                yield(cnt.max()!!)

                println(ans)
            }
        }
    }
}

class IntList(initialCapacity: Int = 12) {
    private var _arr = IntArray(initialCapacity)
    private val capacity get() = _arr.size
    var size = 0
        private set
    inline val lastIndex get() = size - 1
    inline val indices get() = 0 until size

    constructor(copyFrom: IntArray): this(copyFrom.size) { copyFrom.copyInto(_arr); size = copyFrom.size }
    constructor(copyFrom: Collection<Int>): this(copyFrom.size) { _arr = copyFrom.toIntArray(); size = copyFrom.size }

    fun contentEquals(other: IntList): Boolean {
        return this === other || size == other.size && indices.all { this[it] == other[it] }
    }

    private fun grow(minCapacity: Int = 8) {
        val newCapacity = maxOf(minCapacity, capacity + (capacity shr 1))
        _arr = _arr.copyOf(newCapacity)
    }

    fun ensureCapacity(minCapacity: Int) { if(capacity < minCapacity) grow(minCapacity) }

    operator fun get(index: Int): Int {
        require(index in 0 until size)
        return _arr[index]
    }

    operator fun set(index: Int, value: Int) {
        require(index in 0 until size)
        _arr[index] = value
    }

    fun add(value: Int) {
        if(size == capacity) grow()
        _arr[size++] = value
    }

    fun add(index: Int, element: Int) {
        if(size == capacity) grow()
        _arr.copyInto(_arr, index + 1, index, size)
        size++
        set(index, element)
    }

    fun clear() { size = 0 }

    fun removeAt(index: Int): Int {
        val e = get(index)
        _arr.copyInto(_arr, index, index + 1, size)
        size--
        return e
    }

    operator fun iterator() = object: IntIterator() {
        private var pos = 0
        override fun hasNext() = pos < size
        override fun nextInt() = get(pos++)
    }

    inline fun isEmpty() = size == 0
    inline fun isNotEmpty() = size != 0

    fun pop() = _arr[--size]

    fun swap(i: Int, j: Int) { val t = this[i]; this[i] = this[j]; this[j] = t }
    fun reverse() {
        for(i in 0 until size / 2) swap(i, lastIndex - i)
    }

    fun shuffle(rnd: Random = random) = _shuffle(rnd, _arr::get, _arr::set, size)
    fun sort() { shuffle(); _arr._sort(0, size) }
    fun sortDescending() { sort(); reverse() }

    fun joinToString(separator: CharSequence) = if(size == 0) "" else let {
        buildString {
            append(it[0])
            for (i in 1 until size) {
                append(separator).append(it[i])
            }
        }
    }

    override fun toString() = "[" + joinToString(", ") + "]"

    fun toIntArray() = _arr.copyOf(size)
    fun toList() = List(size, ::get)

    inline fun first() = get(0)
    inline fun last() = get(lastIndex)
}

inline fun IntList(size: Int, init: (Int) -> Int) = IntList(size).apply {
    for(i in 0 until size) { add(init(i)) }
}
inline fun IntArray.toIntList() = IntList(this)
inline fun Collection<Int>.toIntList() = IntList(this)
inline fun intListOf(vararg values: Int) = IntList(values)

fun IntList.max() = (1 until size).fold(this[0]) { acc, i -> max(acc, this[i]) }
fun IntList.min() = (1 until size).fold(this[0]) { acc, i -> min(acc, this[i]) }
fun IntList.getOrNull(i: Int) = if(i in indices) get(i) else null
inline fun IntList.count(predicate: (Int) -> Boolean) = indices.count { predicate(this[it]) }
fun IntList.copyOf() = IntList(size, ::get)

/** IO */
//@JvmField val INPUT = File("input.txt").inputStream()
//@JvmField val OUTPUT = File("output.txt").outputStream()
@JvmField val INPUT = System.`in`
@JvmField val OUTPUT = System.out

@JvmField val _reader = INPUT.bufferedReader()
fun readLine(): String? = _reader.readLine()
fun readLn() = _reader.readLine()!!
@JvmField var _ln = ""
@JvmField var _lnPtr = 0
fun read(): String {
    while (_lnPtr >= _ln.length) {
        _ln = readLine() ?: return ""
        _lnPtr = 0
    }
    var j = _ln.indexOf(' ', _lnPtr)
    if(j < 0) j = _ln.length
    val res = _ln.substring(_lnPtr, j)
    _lnPtr = j + 1
    return res
}
fun readRem(): String /* reads remainder of current line */ =
    _ln.substring(min(_lnPtr, _ln.length)).also { _lnPtr = _ln.length }
fun readInt() = read().toInt()
fun readDouble() = read().toDouble()
fun readLong() = read().toLong()
fun readStrings(n: Int) = List(n) { read() }
fun readLines(n: Int) = List(n) { readLn() }
fun readInts(n: Int) = List(n) { read().toInt() }
fun readIntArray(n: Int) = IntArray(n) { read().toInt() }
fun readDoubles(n: Int) = List(n) { read().toDouble() }
fun readDoubleArray(n: Int) = DoubleArray(n) { read().toDouble() }
fun readLongs(n: Int) = List(n) { read().toLong() }
fun readLongArray(n: Int) = LongArray(n) { read().toLong() }

@JvmField val _writer = PrintWriter(OUTPUT, false)

/** shuffles and sort overrides to avoid quicksort attacks */
private inline fun <T> _shuffle(rnd: Random, get: (Int) -> T, set: (Int, T) -> Unit, size: Int) {
    // Fisher-Yates shuffle algorithm
    for (i in size - 1 downTo 1) {
        val j = rnd.nextInt(i + 1)
        val temp = get(i)
        set(i, get(j))
        set(j, temp)
    }
}

@JvmField var _random: Random? = null
val random get() = _random ?: Random(0x594E215C123 * System.nanoTime()).also { _random = it }

fun IntArray.shuffle(rnd: Random = random) = _shuffle(rnd, ::get, ::set, size)
fun IntArray.sort() { shuffle(); _sort() }
fun IntArray.sortDescending() { shuffle(); _sortDescending() }

fun LongArray.shuffle(rnd: Random = random) = _shuffle(rnd, ::get, ::set, size)
fun LongArray.sort() { shuffle(); _sort() }
fun LongArray.sortDescending() { shuffle(); _sortDescending() }

fun DoubleArray.shuffle(rnd: Random = random) = _shuffle(rnd, ::get, ::set, size)
fun DoubleArray.sort() { shuffle(); _sort() }
fun DoubleArray.sortDescending() { shuffle(); _sortDescending() }

fun CharArray.shuffle(rnd: Random = random) = _shuffle(rnd, ::get, ::set, size)
inline fun CharArray.sort() { _sort() }
inline fun CharArray.sortDescending() { _sortDescending() }

inline fun <T: Comparable<T>> Array<out T>.sort() = _sort()
inline fun <T: Comparable<T>> Array<out T>.sortDescending() = _sortDescending()
inline fun <T: Comparable<T>> MutableList<out T>.sort() = _sort()
inline fun <T: Comparable<T>> MutableList<out T>.sortDescending() = _sortDescending()

fun `please stop removing these imports IntelliJ`() { iprintln(max(1, 2)) }

/** additional commons */
inline fun <T> Iterable<T>.sumByLong(func: (T) -> Long) = fold(0L) { acc, t -> acc + func(t) }
inline fun <T> Sequence<T>.sumByLong(func: (T) -> Long) = fold(0L) { acc, t -> acc + func(t) }
inline fun <T> Array<T>.sumByLong(func: (T) -> Long) = fold(0L) { acc, t -> acc + func(t) }
fun IntArray.sumLong() = fold(0L, Long::plus)


import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.*
import kotlin.math.max

fun main() {
    val jin = BufferedReader(InputStreamReader(System.`in`))
    val out = StringBuilder()
    for (c in 1..jin.readLine().toInt()) {
        val n = jin.readLine().toInt()
        val tokenizer = StringTokenizer(jin.readLine())
        val array = IntArray(n) { tokenizer.nextToken().toInt() }
        val freq = IntArray(201)
        val freqs = IntArray(n + 1)
        var answer = 0
        for (k in 1..200) {
            freq.fill(0)
            freqs.fill(0)
            freqs[0] = 200
            for (j in 0 until n) {
                freqs[freq[array[j]]]--
                freq[array[j]]++
                freqs[freq[array[j]]]++
            }
            var m = freq.max()!!
            answer = max(answer, m)
            var l = -1
            var r = n
            for (amt in 1..n) {
                l++
                r--
                while (l < r && array[l] != k) {
                    freqs[freq[array[l]]]--
                    freq[array[l]]--
                    freqs[freq[array[l]]]++
                    l++
                }
                while (r > l && array[r] != k) {
                    freqs[freq[array[r]]]--
                    freq[array[r]]--
                    freqs[freq[array[r]]]++
                    r--
                }
                if (l >= r) {
                    break
                }
                freqs[freq[array[l]]]--
                freq[array[l]]--
                freqs[freq[array[l]]]++
                freqs[freq[array[r]]]--
                freq[array[r]]--
                freqs[freq[array[r]]]++
                while (freqs[m] == 0) {
                    m--
                }
                answer = max(answer, (2 * amt) + m)
            }
        }
        out.appendln(answer)
    }
    print(out)
}


import java.util.*;
import java.io.*;
import static java.lang.Math.*;
import static java.lang.System.*;
public class E
{
    public static void main(String[]args){
        InputReader sc = new InputReader(System.in);
        PrintWriter pw =new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        int cases=sc.nextInt();
        for(int z=0;z<cases;z++){
        	int len=sc.nextInt();
        	int[]arr=new int[len];
        	for(int a=0;a<len;a++){
        		arr[a]=sc.nextInt();
        	}
        	int[][]integrate=new int[len+1][200];
        	for(int a=1;a<len+1;a++){
        		integrate[a][arr[a-1]-1]++;
        		for(int b=0;b<200;b++){
        			integrate[a][b]+=integrate[a-1][b];
        		}

        	}
        	ArrayList<Integer>[] backloca = new ArrayList[200];
        	for(int a=0;a<200;a++){
        		backloca[a]=new ArrayList<Integer>();
        	}
        	for (int a=len-1; a>-1; a--) {
        		backloca[arr[a]-1].add(a);
        	}
        	//indexing not matched error
        	int max=0;
        	for(int i=0;i<200;i++){
        		max=Math.max(max,integrate[len][i]);
        	}
        	int[]curinte=new int[200];
        	for(int a=0;a<len;a++){
        		curinte[arr[a]-1]++;
        		int back=backloca[arr[a]-1].get(curinte[arr[a]-1]-1);
        		int front=a+1;
        		if(back<=a)continue;
        		for(int b=0;b<200;b++){
        			max=Math.max(max,curinte[arr[a]-1]*2+integrate[back][b]-integrate[front][b]);
        		}

        	}
        	System.out.println(max);
        }

        pw.flush();
    }

    static class InputReader
    {
        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar, snumChars;
        private SpaceCharFilter filter;
        public InputReader(InputStream stream)
        {
                this.stream = stream;
        }
        public int snext()
        {
                if (snumChars == -1)
                        throw new InputMismatchException();
                if (curChar >= snumChars)
                {
                        curChar = 0;
                        try
                        {
                                snumChars = stream.read(buf);
                        } catch (IOException e)
                        {
                                throw new InputMismatchException();
                        }
                        if (snumChars <= 0)
                                return -1;
                }
                return buf[curChar++];
        }
        public int nextInt()
        {
                int c = snext();
                while (isSpaceChar(c))
                {
                        c = snext();
                }
                int sgn = 1;
                if (c == '-')
                {
                        sgn = -1;
                        c = snext();
                }
                int res = 0;
                do
                {
                        if (c < '0' || c > '9')
                                throw new InputMismatchException();
                        res *= 10;
                        res += c - '0';
                        c = snext();
                } while (!isSpaceChar(c));
                return res * sgn;
        }
        public long nextLong()
        {
                int c = snext();
                while (isSpaceChar(c))
                {
                        c = snext();
                }
                int sgn = 1;
                if (c == '-')
                {
                        sgn = -1;
                        c = snext();
                }
                long res = 0;
                do
                {
                        if (c < '0' || c > '9')
                                throw new InputMismatchException();
                        res *= 10;
                        res += c - '0';
                        c = snext();
                } while (!isSpaceChar(c));
                return res * sgn;
        }
        public double nextDouble()
        {
                int c = snext();
                while (isSpaceChar(c))
                {
                        c = snext();
                }
                int sgn = 1;
                if (c == '-')
                {
                        sgn = -1;
                        c = snext();
                }
                double res = 0;
                while(c<='9'&&c>='0'){
                    res*=10;
                    res+=c-'0';
                    c=snext();
                }
                if(c=='.'){
                    double decimal=0;
                    long multiplier=1;
                    c=snext();
                    do
                    {
                        if (c < '0' || c > '9')
                                throw new InputMismatchException();
                        decimal *= 10;
                        decimal += c - '0';
                        multiplier*=10;
                        c = snext();
                    } while (!isSpaceChar(c));
                    return sgn*(res+decimal/multiplier);
                }else{
                    if(!isSpaceChar(c)){
                        throw new InputMismatchException();
                    }
                    return sgn*res;
                }
        }
        public int[] nextIntArray(int n)
        {
                int a[] = new int[n];
                for (int i = 0; i < n; i++)
                {
                        a[i] = nextInt();
                }
                return a;
        }
        public long[] nextLongArray(int n)
        {
                long a[] = new long[n];
                for (int i = 0; i < n; i++)
                {
                        a[i] = nextLong();
                }
                return a;
        }
        public String next(){return readString();}
        public String readString()
        {
                int c = snext();
                while (isSpaceChar(c))
                {
                        c = snext();
                }
                StringBuilder res = new StringBuilder();
                do
                {
                        res.appendCodePoint(c);
                        c = snext();
                } while (!isSpaceChar(c));
                return res.toString();
        }
        public String nextLine()
        {
                int c = snext();
                StringBuilder res = new StringBuilder();
                do
                {
                        res.appendCodePoint(c);
                        c = snext();
                } while (!isEndOfLine(c));
                if(isEndOfLine(res.charAt(res.length()-1)))
                return res.deleteCharAt(res.length()).toString();
                return res.toString();
        }
        public ArrayList<String> readAll(){
            ArrayList<String>a=new ArrayList<String>();
            try{
                while(true){
                    a.add(nextLine());
                }
            }catch(Exception e){

            }
            return a;
        }
        public boolean hasNext(){
            boolean hasnext=true;
            try{
                hasnext= stream.available()!=0;
            }catch(IOException e){
            }
            return hasnext||curChar<snumChars;
        }
        public boolean isSpaceChar(int c)
        {
                if (filter != null)
                        return filter.isSpaceChar(c);
                return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
        private boolean isEndOfLine(int c)
        {
                return c == '\n' || c == '\r' || c == -1;
        }
        public interface SpaceCharFilter
        {
                public boolean isSpaceChar(int ch);
        }
    }
}


// Problem : E2. Three Blocks Palindrome (hard version)
// Contest : Codeforces Round #634 (Div. 3)
// URL : https://codeforces.com/contest/1335/problem/E2
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

import java.io.*;
import java.util.*;

public class a {

	public static void main(String[] args) throws Exception {
		FastReader scan = new FastReader();
		PrintWriter out = new PrintWriter(System.out);
		//PrintWriter out = new PrintWriter("file.out");
		Task solver = new Task();
		int t = scan.nextInt();
		//int t = 1;
		for(int i = 1; i <= t; i++) solver.solve(i, scan, out);
		out.close();
	}

	static class Task {
		static final int inf = Integer.MAX_VALUE;
		public void solve(int testNumber, FastReader sc, PrintWriter pw) {
			//CHECK FOR QUICKSORT TLE
			//***********************//
			//CHECK FOR INT OVERFLOW
			//***********************//
			int n = sc.nextInt();
			int[][] pref = new int[201][n];
			int[] arr = new int[n];
			ArrayList<Integer>[] fr = new ArrayList[201];
			for(int i=0;i<=200;i++){
				fr[i]=new ArrayList<Integer>();
			}
			for(int i=0;i<n;i++){
				arr[i]=sc.nextInt();
				pref[arr[i]][i]++;
				fr[arr[i]].add(i);
			}
			for(int i = 0;i <= 200; i++){
				for(int j = 1;j < n; j++){
					pref[i][j] += pref[i][j-1];
				}
			}
			//pw.println(Arrays.toString(pref[2]));
			int max = 1;
			for(int i=0;i<=200;i++){
				int len = fr[i].size();
				for(int j=0, k=len-1; j < k; j++,k--){
					int ind1 = fr[i].get(j);
					int ind2 = fr[i].get(k);
					int submx = 0;
					for(int h = 0; h <=200; h++){
						submx = Math.max(submx, pref[h][ind2-1]-pref[h][ind1]);
					}
					max = Math.max((j+1)*2+submx,max);
				}
			}
			pw.println(max);
		}
	}
	static long binpow(long a, long b, long m) {
		a %= m;
		long res = 1;
		while (b > 0) {
			if ((b & 1) == 1)
				res = res * a % m;
			a = a * a % m;
			b >>= 1;
		}
		return res;
	}
	static void sort(int[] x){
		shuffle(x);
		Arrays.sort(x);
	}
	static void sort(long[] x){
		shuffle(x);
		Arrays.sort(x);
	}
	static class tup implements Comparable<tup>{
		int a, b;
		tup(int a,int b){
			this.a=a;
			this.b=b;
		}
		@Override
		public int compareTo(tup o){
			return Integer.compare(o.b,b);
		}
	}
	static void shuffle(int[] a) {
		Random get = new Random();
		for (int i = 0; i < a.length; i++) {
			int r = get.nextInt(a.length);
			int temp = a[i];
			a[i] = a[r];
			a[r] = temp;
		}
	}

	static void shuffle(long[] a) {
		Random get = new Random();
		for (int i = 0; i < a.length; i++) {
			int r = get.nextInt(a.length);
			long temp = a[i];
			a[i] = a[r];
			a[r] = temp;
		}
	}

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		public FastReader(String s) throws FileNotFoundException {
			br = new BufferedReader(new FileReader(new File(s)));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}

}


// Problem : E2. Three Blocks Palindrome (hard version)
// Contest : Codeforces Round #634 (Div. 3)
// URL : https://codeforces.com/contest/1335/problem/E2
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

import java.io.*;
import java.util.*;

public class a {

	public static void main(String[] args) throws Exception {
		FastReader scan = new FastReader();
		PrintWriter out = new PrintWriter(System.out);
		//PrintWriter out = new PrintWriter("file.out");
		Task solver = new Task();
		int t = scan.nextInt();
		//int t = 1;
		for(int i = 1; i <= t; i++) solver.solve(i, scan, out);
		out.close();
	}

	static class Task {
		static final int inf = Integer.MAX_VALUE;
		public void solve(int testNumber, FastReader sc, PrintWriter pw) {
			//CHECK FOR QUICKSORT TLE
			//***********************//
			//CHECK FOR INT OVERFLOW
			//***********************//
			int n = sc.nextInt();
			int[][] pref = new int[201][n];
			int[] arr = new int[n];
			ArrayList<Integer>[] fr = new ArrayList[201];
			for(int i=0;i<=200;i++){
				fr[i]=new ArrayList<Integer>();
			}
			for(int i=0;i<n;i++){
				arr[i]=sc.nextInt();
				pref[arr[i]][i]++;
				fr[arr[i]].add(i);
			}
			for(int i = 0;i < 200; i++){
				for(int j = 1;j < n; j++){
					pref[i][j] += pref[i][j-1];
				}
			}
			//pw.println(Arrays.toString(pref[2]));
			int max = 1;
			for(int i=0;i<=200;i++){
				int len = fr[i].size();
				for(int j=0, k=len-1; j < k; j++,k--){
					int ind1 = fr[i].get(j);
					int ind2 = fr[i].get(k);
					int submx = 0;
					for(int h = 0; h <=200; h++){
						submx = Math.max(submx, pref[h][ind2-1]-pref[h][ind1]);
					}
					max = Math.max((j+1)*2+submx,max);
				}
			}
			pw.println(max);
		}
	}
	static long binpow(long a, long b, long m) {
		a %= m;
		long res = 1;
		while (b > 0) {
			if ((b & 1) == 1)
				res = res * a % m;
			a = a * a % m;
			b >>= 1;
		}
		return res;
	}
	static void sort(int[] x){
		shuffle(x);
		Arrays.sort(x);
	}
	static void sort(long[] x){
		shuffle(x);
		Arrays.sort(x);
	}
	static class tup implements Comparable<tup>{
		int a, b;
		tup(int a,int b){
			this.a=a;
			this.b=b;
		}
		@Override
		public int compareTo(tup o){
			return Integer.compare(o.b,b);
		}
	}
	static void shuffle(int[] a) {
		Random get = new Random();
		for (int i = 0; i < a.length; i++) {
			int r = get.nextInt(a.length);
			int temp = a[i];
			a[i] = a[r];
			a[r] = temp;
		}
	}

	static void shuffle(long[] a) {
		Random get = new Random();
		for (int i = 0; i < a.length; i++) {
			int r = get.nextInt(a.length);
			long temp = a[i];
			a[i] = a[r];
			a[r] = temp;
		}
	}

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		public FastReader(String s) throws FileNotFoundException {
			br = new BufferedReader(new FileReader(new File(s)));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}

}


import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(in.readLine());
        for (int te = 0; te < t; te++) {
            int n = Integer.parseInt(in.readLine());
            int[] a = new int[n+1];
            StringTokenizer st = new StringTokenizer(in.readLine());
            for (int i = 1; i <= n; i++) {
                a[i] = Integer.parseInt(st.nextToken());
            }
            int[][] pSum = new int[201][n+1];
            ArrayList<Integer>[] pos = new ArrayList[201];
            for (int i = 0; i <= 200; i++) {
                pos[i] = new ArrayList<>();
            }
            for (int i = 1; i <= n; i++) {
                for (int j = 0; j <= 200; j++) {
                    pSum[j][i] += pSum[j][i-1];
                }
                pSum[a[i]][i]++;
                pos[a[i]].add(i);
            }
            int res = 1;
            for (int c1 = 0; c1 <= 200; c1++) {
                for (int i = 0; i < pos[c1].size()/2; i++) {
                    int leftLeft = 1;
                    int leftRight = pos[c1].get(i);
                    int rightLeft = pos[c1].get(pos[c1].size()-1-i);
                    int rightRight = n;

                    int middleLeft = leftRight + 1;
                    int middleRight = rightLeft - 1;

                    int outside = 0;
                    int inside = 0;

                    int leftSum = pSum[c1][leftRight] - pSum[c1][leftLeft - 1];
                    int rightSum = pSum[c1][rightRight] - pSum[c1][rightLeft - 1];

                    int outsum = Math.min(leftSum, rightSum);
                    outside = outsum*2;

                    for (int j = 0; j <= 200; j++) {
                        if (middleLeft <= middleRight) {
                            int midsum = pSum[j][middleRight] - pSum[j][middleLeft - 1];
                            inside = Math.max(midsum, inside);
                        }
                    }
                    res = Math.max(res, outside + inside);
                }
            }
            System.out.println(res);
        }
    }

}
/*

 */


import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(in.readLine());
        for (int te = 0; te < t; te++) {
            int n = Integer.parseInt(in.readLine());
            int[] a = new int[n+1];
            StringTokenizer st = new StringTokenizer(in.readLine());
            for (int i = 1; i <= n; i++) {
                a[i] = Integer.parseInt(st.nextToken());
            }
            int[][] pSum = new int[201][n+1];
            for (int i = 1; i <= n; i++) {
                for (int j = 0; j <= 200; j++) {
                    pSum[j][i] += pSum[j][i-1];
                }
                pSum[a[i]][i]++;
            }
            int mid = n/2;
            int res = 0;
            for (int i = 0; i <= mid; i++) {
                //System.out.println(i + "th iteration");
                int leftLeft = 1;
                int leftRight = i;
                int rightLeft = n+1-i;
                int rightRight = n;

                int middleLeft = leftRight+1;
                int middleRight = rightLeft-1;

                int outside = 0;
                int inside = 0;

                //System.out.println("RangeLeft: [" + leftLeft + "," + leftRight + "]");
                //System.out.println("RangeRight: [" + rightLeft + "," + rightRight + "]");
                //System.out.println("RangeMid: [" + middleLeft + "," + middleRight + "]");
                for (int j = 0; j <= 200; j++) {
                    int leftSum = pSum[j][leftRight] - pSum[j][leftLeft-1];
                    int rightSum = pSum[j][rightRight] - pSum[j][rightLeft-1];
                    int outsum = Math.min(leftSum, rightSum);
                    outside = Math.max(outside, outsum*2);
                    if (middleLeft <= middleRight) {
                        int midsum = pSum[j][middleRight] - pSum[j][middleLeft-1];
                        inside = Math.max(midsum, inside);
                    }
                }
                res = Math.max(res, outside+inside);
            }
            System.out.println(res);
        }
    }

}
/*

 */


import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(in.readLine());
        for (int te = 0; te < t; te++) {
            int n = Integer.parseInt(in.readLine());
            int[] a = new int[n+1];
            StringTokenizer st = new StringTokenizer(in.readLine());
            for (int i = 1; i <= n; i++) {
                a[i] = Integer.parseInt(st.nextToken());
            }
            int[][] pSum = new int[201][n+1];
            ArrayList<Integer>[] pos = new ArrayList[201];
            for (int i = 0; i <= 200; i++) {
                pos[i] = new ArrayList<>();
            }
            for (int i = 1; i <= n; i++) {
                for (int j = 0; j <= 200; j++) {
                    pSum[j][i] += pSum[j][i-1];
                }
                pSum[a[i]][i]++;
                pos[a[i]].add(i);
            }
            int res = 1;
            for (int c1 = 0; c1 <= 200; c1++) {
                for (int i = 0; i < pos[c1].size()/2; i++) {
                    //System.out.println(i + "th iteration");
                    int leftLeft = 1;
                    int leftRight = pos[c1].get(i);
                    int rightLeft = pos[c1].get(pos[c1].size()-1-i);
                    int rightRight = n;

                    int middleLeft = leftRight + 1;
                    int middleRight = rightLeft - 1;

                    int outside = 0;
                    int inside = 0;

                    int leftSum = pSum[c1][leftRight] - pSum[c1][leftLeft - 1];
                    int rightSum = pSum[c1][rightRight] - pSum[c1][rightLeft - 1];

                    int outsum = Math.min(leftSum, rightSum);
                    outside = outsum*2;

                    for (int j = 0; j <= 200; j++) {
                        if (middleLeft <= middleRight) {
                            int midsum = pSum[j][middleRight] - pSum[j][middleLeft - 1];
                            inside = Math.max(midsum, inside);
                        }
                    }
                    res = Math.max(res, outside + inside);
                }
            }
            System.out.println(res);
        }
    }

}
/*
1
6
1 5 6 3 2 3

 */


import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.List;
 import java.util.*;
public  class realfast implements Runnable
  {
    private static final int INF = (int) 1e9;
    long in= (long)Math.pow(10,9)+7;
    long fac[]= new long[3000];
    public  void solve() throws IOException
    {

     int t = readInt();

     for(int f =0;f<t;f++)
     {
        int n = readInt();
        int count[][]= new int[n+1][201];
        ArrayList<Integer> arr[]= new ArrayList[201];
        int max=1;
        for(int i =0;i<=200;i++)
            arr[i]= new ArrayList<>();
        for(int i =1;i<=n;i++)
        {
            int a= readInt();
            for(int j=1;j<=200;j++)
            {
                count[i][j]=count[i-1][j];
            }
            count[i][a]++;
            arr[a].add(i);
        }

        for(int i=1;i<=200;i++)
        {
            for(int j =0;j<arr[i].size()/2;j++)
            {
                int left = arr[i].get(j);
                int right =arr[i].get(arr[i].size()-j-1);
                int val = 2*(j+1);
                max=Math.max(val,max);
                for(int k=1;k<=200;k++)
                {
                   max = Math.max(val+ count[right-1][k]- count[left][k],max);
                }
            }
        }

        out.println(max);

     }

    }

    public long pow(long n , long p,long m)
    {
         if(p==0)
            return 1;
        long val = pow(n,p/2,m);;
        val= (val*val)%m;
        if(p%2==0)
            return val;
        else
            return (val*n)%m;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public static void main(String[] args) {
        new Thread(null, new realfast(), "", 128 * (1L << 20)).start();
    }

    private static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
    private BufferedReader reader;
    private StringTokenizer tokenizer;
    private PrintWriter out;

    @Override
    public void run() {
        try {
            if (ONLINE_JUDGE || !new File("input.txt").exists()) {
                reader = new BufferedReader(new InputStreamReader(System.in));
                out = new PrintWriter(System.out);
            } else {
                reader = new BufferedReader(new FileReader("input.txt"));
                out = new PrintWriter("output.txt");
            }
            solve();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                // nothing
            }
            out.close();
        }
    }

    private String readString() throws IOException {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }

    @SuppressWarnings("unused")
    private int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    @SuppressWarnings("unused")
    private long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    @SuppressWarnings("unused")
    private double readDouble() throws IOException {
        return Double.parseDouble(readString());
    }
}
class edge implements Comparable<edge>{
    int val ;
    int color;

    edge(int u, int v)
    {
       this.val=u;
       this.color=v;
    }
    public int compareTo(edge e)
    {
        return this.val-e.val;
    }
}


// package com.company;
import java.util.*;
import java.lang.*;
import java.io.*;
//****Use Integer Wrapper Class for Arrays.sort()****
public class CJ5 {
    public static void main(String[] Args){
        FastReader scan=new FastReader();
        int t=scan.nextInt();
        while(t-->0){
            int n=scan.nextInt();
            int[] arr=new int[n+1];
            Map<Integer,ArrayList<Integer>> m=new HashMap<>();
            for (int i = 0; i <n ; i++) {
                arr[i+1]=scan.nextInt();
                m.putIfAbsent(arr[i+1],new ArrayList<>());
                m.get(arr[i+1]).add(i+1);
            }
            int[][] f=new int[n+1][27];
            f[1][arr[1]]=1;
            for(int i=2;i<=n;i++){
                for(int j=1;j<=26;j++){
                    f[i][j]=f[i-1][j];
                }
                f[i][arr[i]]++;
            }
            int ans=0;
            for(Integer i:m.keySet()){
                ArrayList<Integer> in=m.get(i);
                ans=Math.max(ans,in.size());
                int s=in.size();
                s/=2;
                for(int j=0;j<s;j++){
                    int li=in.get(j);
                    int ri=in.get(in.size()-1-j);
                    int temp=2*(j+1);
                    int[] mid=new int[27];
                    int midm=0;
                    for(int k=1;k<27;k++){
                        mid[k]=f[ri-1][k]-f[li][k];
                        midm=Math.max(midm,mid[k]);
                    }
                    temp+=midm;
                    ans=Math.max(temp,ans);

                }
            }
            System.out.println(ans);
        }
    }
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

}


// package com.company;
import java.util.*;
import java.lang.*;
import java.io.*;
//****Use Integer Wrapper Class for Arrays.sort()****
public class CJ5 {
    public static void main(String[] Args){
        FastReader scan=new FastReader();
        int t=scan.nextInt();
        StringBuilder print=new StringBuilder();
        while(t-->0){
            int n=scan.nextInt();
            int[] arr=new int[n+1];
            Map<Integer,ArrayList<Integer>> m=new HashMap<>();
            for (int i = 0; i <n ; i++) {
                arr[i+1]=scan.nextInt();
                m.putIfAbsent(arr[i+1],new ArrayList<>());
                m.get(arr[i+1]).add(i+1);
            }
            int[][] f=new int[n+1][27];
            f[1][arr[1]]=1;
            for(int i=2;i<=n;i++){
                for(int j=1;j<=26;j++){
                    f[i][j]=f[i-1][j];
                }
                f[i][arr[i]]++;
            }
            int ans=0;
            for(Integer i:m.keySet()){
                ArrayList<Integer> in=m.get(i);
                ans=Math.max(ans,in.size());
                int l=0;
                int r=in.size()-1;
                while(l<r){
                    int li=in.get(l);
                    int ri=in.get(r)-1;
                    int temp=2*(l+1);
                    int[] mid=new int[27];
                    int midm=0;
                    for(int k=1;k<27;k++){
                        mid[k]=f[ri][k]-f[li][k];
                        midm=Math.max(midm,mid[k]);
                    }
                    temp+=midm;
                    ans=Math.max(temp,ans);
                    l++;
                    r--;
                }
            }
            print.append(ans+"\n");
        }
        System.out.println(print);
    }
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

}


//  package com.company;
import java.util.*;
import java.lang.*;
import java.io.*;
//****Use Integer Wrapper Class for Arrays.sort()****
public class CJ5 {
    public static void main(String[] Args){
        FastReader scan=new FastReader();
        int t=scan.nextInt();
        while(t-->0){
            int n=scan.nextInt();
            int[] arr=new int[n+1];
            Map<Integer,ArrayList<Integer>> m=new HashMap<>();
            for (int i = 0; i <n ; i++) {
                arr[i+1]=scan.nextInt();
                m.putIfAbsent(arr[i+1],new ArrayList<>());
                m.get(arr[i+1]).add(i+1);
            }
            int[][] f=new int[n+1][201];
            f[1][arr[1]]=1;
            for(int i=2;i<=n;i++){
                for(int j=1;j<=200;j++){
                    f[i][j]=f[i-1][j];
                }
                f[i][arr[i]]++;
            }
            int ans=0;
            for(Integer i:m.keySet()){
                ArrayList<Integer> in=m.get(i);
                ans=Math.max(ans,in.size());
                int s=in.size();
                s/=2;
                for(int j=0;j<s;j++){
                    int li=in.get(j);
                    int ri=in.get(in.size()-1-j);
                    int temp=2*(j+1);
                    int[] mid=new int[201];
                    int midm=0;
                    for(int k=1;k<201;k++){
                        mid[k]=f[ri-1][k]-f[li][k];
                        midm=Math.max(midm,mid[k]);
                    }
                    temp+=midm;
                    ans=Math.max(temp,ans);

                }
            }
            System.out.println(ans);
        }
    }
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

}


import java.io.*;
import java.io.IOException;

import java.util.*;
//import javafx.util.Pair;
//import java.util.concurrent.LinkedBlockingDeque;

//import sun.net.www.content.audio.wav;

   public  class Codeforces {

        public static long mod = (long)Math.pow(10, 9)+7 ;
        public static double epsilon=0.00000000008854;//value of epsilon
        public static InputReader sc = new InputReader(System.in);
        public static PrintWriter pw = new PrintWriter(System.out);

        public static long gcd(long a,long b){
            while(b>0){
                long t=b;
                b=a%b;
                a=t;
            }
            return a;
        }
        public static long fun(int a1[],TreeSet<Integer> tr2,TreeSet<Integer> tr3){
            int n1=a1.length;
            long max=Long.MAX_VALUE;
                for(int i=0;i<n1;i++){
                    Integer mi2=tr2.floor(a1[i]);
                    Integer ma2=tr2.ceiling(a1[i]);
                    long b2;
                    if(mi2==null)
                    b2=ma2;
                    else if(ma2==null){
                        b2=mi2;
                    }
                    else {
                        if(ma2-a1[i]>a1[i]-mi2){
                            b2=mi2;
                        }
                        else b2=ma2;
                    }
                    Integer mi3=tr3.floor(a1[i]);
                    Integer ma3=tr3.ceiling(a1[i]);
                    long b3;
                    if(mi3==null)
                    b3=ma3;
                    else if(ma3==null){
                        b3=mi3;
                    }
                    else {
                        if(ma3-a1[i]>a1[i]-mi3){
                            b3=mi3;
                        }
                        else b3=ma3;
                    }
                    max=Math.min((a1[i]-b2)*(a1[i]-b2)+(a1[i]-b3)*(a1[i]-b3)+(b2-b3)*(b2-b3),max);

                }

            return max;
        }

        public static void main(String[] args) {
            // code starts..
            int q=sc.nextInt();

            for(int f=0;f<q;f++){
                int n=sc.nextInt();
                int a[]=scanArray(n);
                int hp[][]=new int[n][201];
                ArrayList<ArrayList<Integer>> hi=new ArrayList<>();
                ArrayList<ArrayList<Integer>> hm=new ArrayList<>();
                for(int i=0;i<201;i++){
                    hi.add(new ArrayList<>());
                    hm.add(new ArrayList<>());
                }
                int ans=0;
                for(int i=0;i<n;i++){
                    hi.get(a[i]).add(i);
                    hp[i][a[i]]++;
                    if(i>0)
                    for(int j=1;j<201;j++){
                        hp[i][j]+=hp[i-1][j];
                    }
                }

               for(int i=1;i<201;i++){
                   int m=hi.get(i).size();
                   ans=Math.max(m, ans);
                   for(int j=0;j<m/2;j++){
                       int i1=hi.get(i).get(j);
                       int i2=hi.get(i).get(m-j-1);
                    for(int k=1;k<201;k++){
                        if(i==k)
                        continue;
                        ans=Math.max(2*(j+1)+hp[i2][k]-hp[i1][k], ans);
                    }
                   }
               }
                pw.println(ans);
            }
            // Code ends...
            pw.flush();
            pw.close();
        }

        public static Comparator<Integer[]> column(int i){
            return
            new Comparator<Integer[]>() {
                @Override
                public int compare(Integer[] o1, Integer[] o2) {
                    return o1[i].compareTo(o2[i]);//for ascending
                    //return o2[i].compareTo(o1[i]);//for descending
                }
            };
        }
        public static String reverseString(String s){
            StringBuilder input1 = new StringBuilder();
            input1.append(s);
            input1 = input1.reverse();
            return input1.toString();
        }
        public static int[] scanArray(int n){
            int a[]=new int [n];
            for(int i=0;i<n;i++)
            a[i]=sc.nextInt();

            return a;
        }
        public static long[] scanLongArray(int n){
            long a[]=new long [n];
            for(int i=0;i<n;i++)
            a[i]=sc.nextLong();

            return a;
        }
        public static String [] scanStrings(int n){
            String a[]=new String [n];
            for(int i=0;i<n;i++)
            a[i]=sc.nextLine();

            return a;
        }

    }

    class InputReader {

            private final InputStream stream;
            private final byte[] buf = new byte[8192];
            private int curChar, snumChars;
            private SpaceCharFilter filter;

            public InputReader(InputStream stream) {
                this.stream = stream;
            }

            public int snext() {
                if (snumChars == -1)
                    throw new InputMismatchException();
                if (curChar >= snumChars) {
                    curChar = 0;
                    try {
                        snumChars = stream.read(buf);
                    } catch (IOException e) {
                        throw new InputMismatchException();
                    }
                    if (snumChars <= 0)
                        return -1;
                }
                return buf[curChar++];
            }

            public int nextInt() {
                int c = snext();
                while (isSpaceChar(c)) {
                    c = snext();
                }
                int sgn = 1;
                if (c == '-') {
                    sgn = -1;
                    c = snext();
                }
                int res = 0;
                do {
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    res *= 10;
                    res += c - '0';
                    c = snext();
                } while (!isSpaceChar(c));
                return res * sgn;
            }

            public long nextLong() {
                int c = snext();
                while (isSpaceChar(c)) {
                    c = snext();
                }
                int sgn = 1;
                if (c == '-') {
                    sgn = -1;
                    c = snext();
                }
                long res = 0;
                do {
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    res *= 10;
                    res += c - '0';
                    c = snext();
                } while (!isSpaceChar(c));
                return res * sgn;
            }

            public int[] nextIntArray(int n) {
                int a[] = new int[n];
                for (int i = 0; i < n; i++) {
                    a[i] = nextInt();
                }
                return a;
            }

            public String readString() {
                int c = snext();
                while (isSpaceChar(c)) {
                    c = snext();
                }
                StringBuilder res = new StringBuilder();
                do {
                    res.appendCodePoint(c);
                    c = snext();
                } while (!isSpaceChar(c));
                return res.toString();
            }

            public String nextLine() {
                int c = snext();
                while (isSpaceChar(c))
                    c = snext();
                StringBuilder res = new StringBuilder();
                do {
                    res.appendCodePoint(c);
                    c = snext();
                } while (!isEndOfLine(c));
                return res.toString();
            }

            public boolean isSpaceChar(int c) {
                if (filter != null)
                    return filter.isSpaceChar(c);
                return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
            }

            private boolean isEndOfLine(int c) {
                return c == '\n' || c == '\r' || c == -1;
            }

            public interface SpaceCharFilter {
                public boolean isSpaceChar(int ch);
            }
        }


import java.io.*;
import java.io.IOException;

import java.util.*;
//import javafx.util.Pair;
//import java.util.concurrent.LinkedBlockingDeque;

//import sun.net.www.content.audio.wav;

   public  class Codeforces {

        public static long mod = (long)Math.pow(10, 9)+7 ;
        public static double epsilon=0.00000000008854;//value of epsilon
        public static InputReader sc = new InputReader(System.in);
        public static PrintWriter pw = new PrintWriter(System.out);

        public static long gcd(long a,long b){
            while(b>0){
                long t=b;
                b=a%b;
                a=t;
            }
            return a;
        }
        public static long fun(int a1[],TreeSet<Integer> tr2,TreeSet<Integer> tr3){
            int n1=a1.length;
            long max=Long.MAX_VALUE;
                for(int i=0;i<n1;i++){
                    Integer mi2=tr2.floor(a1[i]);
                    Integer ma2=tr2.ceiling(a1[i]);
                    long b2;
                    if(mi2==null)
                    b2=ma2;
                    else if(ma2==null){
                        b2=mi2;
                    }
                    else {
                        if(ma2-a1[i]>a1[i]-mi2){
                            b2=mi2;
                        }
                        else b2=ma2;
                    }
                    Integer mi3=tr3.floor(a1[i]);
                    Integer ma3=tr3.ceiling(a1[i]);
                    long b3;
                    if(mi3==null)
                    b3=ma3;
                    else if(ma3==null){
                        b3=mi3;
                    }
                    else {
                        if(ma3-a1[i]>a1[i]-mi3){
                            b3=mi3;
                        }
                        else b3=ma3;
                    }
                    max=Math.min((a1[i]-b2)*(a1[i]-b2)+(a1[i]-b3)*(a1[i]-b3)+(b2-b3)*(b2-b3),max);

                }

            return max;
        }

        public static void main(String[] args) {
            // code starts..
            int q=sc.nextInt();
            while(q-->0){
                int n=sc.nextInt();
                int a[]=scanArray(n);
                int hp[][]=new int[n][201];
                ArrayList<ArrayList<Integer>> hi=new ArrayList<>();
                ArrayList<ArrayList<Integer>> hm=new ArrayList<>();
                for(int i=0;i<201;i++){
                    hi.add(new ArrayList<>());
                    hm.add(new ArrayList<>());
                }
                int ans=0;
                for(int i=0;i<n;i++){
                    hi.get(a[i]).add(i);
                    hp[i][a[i]]++;
                    if(i>0)
                    for(int j=1;j<201;j++){
                        hp[i][j]+=hp[i-1][j];
                    }
                    int size=hi.get(a[i]).size();
                    int mid=0;
                    if(size>=2){
                        int i1=hi.get(a[i]).get(size-2);
                        int i2=i;
                        for(int j=1;j<201;j++){
                            if(j!=a[i])
                            mid=Math.max(hp[i2][j]-hp[i1][j], mid);
                        }
                        hm.get(a[i]).add(mid);
                    }
                }
                for(int i=1;i<201;i++){
                    ans=Math.max(hm.get(i).size()+1, ans);
                    for(int j=0;j<hm.get(i).size();j++){
                        int mid=hm.get(i).get(j);
                        ans=Math.max(2*Math.min(j+1, hm.get(i).size()-j)+mid, ans);
                    }
                }
                pw.println(ans);
            }
            // Code ends...
            pw.flush();
            pw.close();
        }

        public static Comparator<Integer[]> column(int i){
            return
            new Comparator<Integer[]>() {
                @Override
                public int compare(Integer[] o1, Integer[] o2) {
                    return o1[i].compareTo(o2[i]);//for ascending
                    //return o2[i].compareTo(o1[i]);//for descending
                }
            };
        }
        public static String reverseString(String s){
            StringBuilder input1 = new StringBuilder();
            input1.append(s);
            input1 = input1.reverse();
            return input1.toString();
        }
        public static int[] scanArray(int n){
            int a[]=new int [n];
            for(int i=0;i<n;i++)
            a[i]=sc.nextInt();

            return a;
        }
        public static long[] scanLongArray(int n){
            long a[]=new long [n];
            for(int i=0;i<n;i++)
            a[i]=sc.nextLong();

            return a;
        }
        public static String [] scanStrings(int n){
            String a[]=new String [n];
            for(int i=0;i<n;i++)
            a[i]=sc.nextLine();

            return a;
        }

    }

    class InputReader {

            private final InputStream stream;
            private final byte[] buf = new byte[8192];
            private int curChar, snumChars;
            private SpaceCharFilter filter;

            public InputReader(InputStream stream) {
                this.stream = stream;
            }

            public int snext() {
                if (snumChars == -1)
                    throw new InputMismatchException();
                if (curChar >= snumChars) {
                    curChar = 0;
                    try {
                        snumChars = stream.read(buf);
                    } catch (IOException e) {
                        throw new InputMismatchException();
                    }
                    if (snumChars <= 0)
                        return -1;
                }
                return buf[curChar++];
            }

            public int nextInt() {
                int c = snext();
                while (isSpaceChar(c)) {
                    c = snext();
                }
                int sgn = 1;
                if (c == '-') {
                    sgn = -1;
                    c = snext();
                }
                int res = 0;
                do {
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    res *= 10;
                    res += c - '0';
                    c = snext();
                } while (!isSpaceChar(c));
                return res * sgn;
            }

            public long nextLong() {
                int c = snext();
                while (isSpaceChar(c)) {
                    c = snext();
                }
                int sgn = 1;
                if (c == '-') {
                    sgn = -1;
                    c = snext();
                }
                long res = 0;
                do {
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    res *= 10;
                    res += c - '0';
                    c = snext();
                } while (!isSpaceChar(c));
                return res * sgn;
            }

            public int[] nextIntArray(int n) {
                int a[] = new int[n];
                for (int i = 0; i < n; i++) {
                    a[i] = nextInt();
                }
                return a;
            }

            public String readString() {
                int c = snext();
                while (isSpaceChar(c)) {
                    c = snext();
                }
                StringBuilder res = new StringBuilder();
                do {
                    res.appendCodePoint(c);
                    c = snext();
                } while (!isSpaceChar(c));
                return res.toString();
            }

            public String nextLine() {
                int c = snext();
                while (isSpaceChar(c))
                    c = snext();
                StringBuilder res = new StringBuilder();
                do {
                    res.appendCodePoint(c);
                    c = snext();
                } while (!isEndOfLine(c));
                return res.toString();
            }

            public boolean isSpaceChar(int c) {
                if (filter != null)
                    return filter.isSpaceChar(c);
                return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
            }

            private boolean isEndOfLine(int c) {
                return c == '\n' || c == '\r' || c == -1;
            }

            public interface SpaceCharFilter {
                public boolean isSpaceChar(int ch);
            }
        }


import java.io.*;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main
{
   static long mod=(long)(1e+9 + 7);
   //static long mod=(long)998244353;

   static int[] sieve;
   static ArrayList<Integer> primes;
   static PrintWriter out;
   static fast s;
   static StringBuilder fans;

   public static void solve()
   {
   	int n=s.nextInt();
   	int a[]=new int[n];
   	for(int i=0;i<n;i++) a[i]=s.nextInt();

   	int ans=1;

   	int left[]=new int[201];
   	int right[]=new int[201];

   	Arrays.fill(left, Integer.MAX_VALUE);
   	Arrays.fill(right, -1);

   	for(int i=0;i<n;i++) left[a[i]]=Math.min(left[a[i]], i);
   	for(int i=0;i<n;i++) right[a[i]]=Math.max(right[a[i]], i);

   	int f[][]=new int[n][201];
   	f[0][a[0]]++;

   	for(int i=1;i<n;i++)
   		{
   			for(int k=1;k<=200;k++) f[i][k]=f[i-1][k];
   			f[i][a[i]]++;
   		}

   	for(int k=1;k<=200;k++)
   		{
   			int l=left[k];
   			int r=right[k];

   			if(l==r || l==Integer.MAX_VALUE) continue;
   			if(r==l+1) {ans=Math.max(ans, 2);continue;}

   			int i=l;
   			int j=r;
   			int cnt=2;

   			while(i<j)
   				{
   					if(a[i]==a[j] && a[i]==k)
   						{
		   					int cur=cnt;
		   					int maxi=0;

		   					for(int p=1;p<=200;p++)
		   						{
		   							int pp=f[j-1][p]-f[i][p];
		   							maxi=Math.max(maxi, pp);
		   						}
		   					ans=Math.max(ans, cur+maxi);
		   					cnt+=2;
		   					i++;
		   					j--;
   						}
   					else
   						{
   							if(a[i]!=k) i++;
   							if(a[j]!=k) j--;
   						}
   				}
   		}
   	fans.append(ans+"\n");
   }

   public static void main(String[] args) throws java.lang.Exception
   {
	     s = new fast();
	     out=new PrintWriter(System.out);
	     fans=new StringBuilder("");

	     int t=s.nextInt();

	     while(t>0)
	    	 {
	    		 solve();
	    		 t--;
	    	 }

	    out.println(fans);
	    out.close();
   }

static class fast {

	   private InputStream i;
		private byte[]  buf = new byte[1024];
		private int curChar;

		private int numChars;

		//Return floor log2n
		public static long log2(long bits) // returns 0 for bits=0
			{
			    int log = 0;
			    if( ( bits & 0xffff0000 ) != 0 ) { bits >>>= 16; log = 16; }
			    if( bits >= 256 ) { bits >>>= 8; log += 8; }
			    if( bits >= 16  ) { bits >>>= 4; log += 4; }
			    if( bits >= 4   ) { bits >>>= 2; log += 2; }
			    return log + ( bits >>> 1 );
			}

		public static boolean next_permutation(int a[])
		   {
			   int i=0,j=0;int index=-1;
			   int n=a.length;

			   for(i=0;i<n-1;i++)
				   if(a[i]<a[i+1]) index=i;

			   if(index==-1) return false;
			   i=index;

			   for(j=i+1;j<n && a[i]<a[j];j++);

			   int temp=a[i];
			   a[i]=a[j-1];
			   a[j-1]=temp;

			   for(int p=i+1,q=n-1;p<q;p++,q--)
			   {
				   temp=a[p];
				   a[p]=a[q];
				   a[q]=temp;
			   }

			   return true;
		   }

		public static void division(char ch[],int divisor)
		   {
			   int div=Character.getNumericValue(ch[0]); int mul=10;int remainder=0;
			   StringBuilder quotient=new StringBuilder("");
			   for(int i=1;i<ch.length;i++)
			   {
				   div=div*mul+Character.getNumericValue(ch[i]);
				   if(div<divisor) {quotient.append("0");continue;}
				   quotient.append(div/divisor);
				   div=div%divisor;mul=10;

			   }
			   remainder=div;
			   while(quotient.charAt(0)=='0')quotient.deleteCharAt(0);
			   System.out.println(quotient+" "+remainder);
		   }

		public static void sieve(int size)
		{
			sieve=new int[size+1];

			primes=new ArrayList<Integer>();

			sieve[1]=1;

			for(int i=2;i*i<=size;i++)
			{
				if(sieve[i]==0)
				 {
					for(int j=i*i;j<size;j+=i) {sieve[j]=1;}
				 }
			}

			for(int i=2;i<=size;i++)
			{
				if(sieve[i]==0) primes.add(i);
			}
		}

		public static long pow(long n, long b, long MOD)
		   {
		       long x=1;long y=n;
		       while(b > 0)
		       {
		           if(b%2 == 1)
		           {
		               x=x*y;
		               if(x>MOD) x=x%(MOD);
		           }
		           y = y*y;
		           if(y>MOD) y=y%(MOD);
		           b >>= 1;
		       }
		       return x;
		   }

		public static long mod_inv(long n,long mod)
		{
			return pow(n,mod-2,mod);
		}

		//Returns index of highest number less than or equal to key
		 public static int upper(long[] a,int length,long key)
		 {
			    int low = 0;
		        int high = length-1;
		        int ans=-1;

		        while (low <= high) {
		            int mid = (low + high) / 2;
		            if (key >= a[mid]) {
		                ans=mid;
		            	low = mid+1;
		            } else if(a[mid]>key){
		                high = mid - 1;
		            }
		        }
		        return ans;
		}

		//Returns index of least number greater than or equal to key
		 public static int lower(long[] a,int length,long key)
		 {
			    int low = 0;
		        int high = length-1;
		        int ans=-1;

		        while (low <= high) {
		            int mid = (low + high) / 2;
		            if (key<=a[mid]) {
		                ans=mid;
		            	high = mid-1;
		            }
		            else{
		            		low=mid+1;
		            	}
		        }
		        return ans;
		}

		public long gcd(long r,long ans)
		{
			if(r==0) return ans;
			return gcd(ans%r,r);
		}
		public fast() {
			this(System.in);
		}
		public fast(InputStream is) {
			i = is;
		}
		public int read() {
			if (numChars == -1)
				throw new InputMismatchException();
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = i.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}
		public String nextLine() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndOfLine(c));
			return res.toString();
		}
		public String nextString() {
     		int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}
		public long nextLong() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}
		public int nextInt() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}
		public boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}
		public boolean isEndOfLine(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

	}
}