You are given an array aa consisting of nn integers.

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

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

Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array aa of length nn is the palindrome if ai=an−i−1ai=an−i−1 for all ii from 11 to nn. For example, arrays [1234][1234], [1,2,1][1,2,1], [1,3,2,2,3,1][1,3,2,2,3,1] and [10,100,10][10,100,10] are palindromes, but arrays [1,2][1,2] and [1,2,3,1][1,2,3,1] are not.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1≤t≤1001≤t≤100) — the number of test cases.

Next 2t2t lines describe test cases. The first line of the test case contains one integer nn (3≤n≤50003≤n≤5000) — the length of aa. The second line of the test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n), where aiai is the ii-th element of aa.

It is guaranteed that the sum of nn over all test cases does not exceed 50005000 (∑n≤5000∑n≤5000).

Output

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

Example

Input
5
3
1 2 1
5
1 2 2 3 2
3
1 1 2
4
1 2 2 1
10
1 1 2 2 3 3 4 4 5 5
Output
YES
YES
NO
YES
NO

Note

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

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

In the third test case of the example, the array aa has no subsequences of length at least 33 which are palindromes.

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

In the fifth test case of the example, the array aa has no subsequences of length at least 33 which are palindromes.

这个题的意思是能不能在一组数中找到一个回文数(只要有三个数就行,而且在数组中的位置不一定要挨着)

这道题再简化一步的想法是能否在一个数组中找到两个相等的数,并且第一关数和第二个数之间至少隔一个位置

题目要求数组的大小上界为5000,一定要在主函数外定义这个数组,因为这个数组太大了,当时比赛的时候一直找不到哪里错了,下来之后才知道因该在全局定义

#include<iostream>
using namespace std;
const long long maxn=+;
int a[maxn];
int main(){
int t,i,j,flag,k,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i=;i<n;i++){
scanf("%d",&a[i]);
}
flag=;
for(i=;i<n-;i++){ //第一个数最多遍历到倒第三个位置
if(i&==) continue; //从第0个位置开始如果第一个数的位置是2的倍数,那么这个位置就和第二个数的上一个位置重合,直接跳过
for(k=i+;k<n;k++){
if(a[k]==a[i]){
cout<<"YES"<<endl;
flag=;
break;
}
}
if(flag==) break;
}
if(i==n-) cout<<"NO"<<endl;
}
}

最新文章

  1. oracle调用JAVA类的方法
  2. Java特性之多态父类与子类之间的调用
  3. IIS兼容模式设置
  4. JDSideMenu实现(整块)侧滑功能,主视图会和状态栏(StatusBar)会一起滑动。
  5. GitHub Desktop for Win 安装不上
  6. 原 Debian设置开机自动启动与关闭
  7. DOM中的node与element的区别
  8. 什么是Jsp
  9. 轻量级别的Cache和反向代理软件---Varnish
  10. Web office apps 安装部署
  11. vscode前端常用插件推荐,搭建JQuery、Vue等开发环境
  12. Interpreting NotifyCollectionChangedEventArgs zz
  13. 五、RemoteViews
  14. YAML 知识点
  15. 10 SpringBoot全面接管SpringMVC
  16. Replicated Ship 本地 kubernetes 环境试用
  17. [UE4]非常实用的插值Lerp
  18. 数链剖分(树的统计Count )
  19. R语言学习——列表
  20. Tomcat中的Web.xml和servlet.xml的学习

热门文章

  1. 复合文字(Compound Literals)
  2. 你相信吗:新药可以让X战警变成现实
  3. iPhone8、Note8、Mate10硬上面部识别:是潮流还是无奈
  4. Simpo
  5. Eclipse如何打开Android工程
  6. C++与引用1
  7. Intellij IDEA 干货分享
  8. BLAKE及BLAKE2算法详解
  9. py装饰器,生成器,迭代器
  10. 前端每日实战:20# 视频演示如何用纯 CSS 为母亲节创作一颗像素画风格的爱心