fn is_kadomatsu(a: &Vec) -> bool { a[0] != a[1] && a[1] != a[2] && a[2] != a[0] && (*a.iter().min().unwrap() == a[1] || *a.iter().max().unwrap() == a[1]) } fn solve() -> usize { let mut n = String::new(); std::io::stdin().read_line(&mut n).ok(); let n: usize = n.trim().parse().unwrap(); let mut a = String::new(); std::io::stdin().read_line(&mut a).ok(); let a: Vec = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let mut result = 0usize; for start in 0..3 { let mut dp = vec![vec![0usize; 2]; n+1]; for i in start..n+start-2 { let aval = a[i]; let target = vec![aval, a[(i+1)%n], a[(i+2)%n]]; let kadoval = if is_kadomatsu(&target) { aval } else { 0 }; dp[i+1][0] = dp[i][0]; dp[i+1][1] = dp[i][0] + kadoval; if i >= 2 { dp[i+1][0] = dp[i+1][0].max(dp[i-2][1]); dp[i+1][1] = dp[i+1][1].max(dp[i-2][1] + kadoval) } } result = result.max(dp.iter().map(|v| *v.iter().max().unwrap()).max().unwrap()); } result } fn main() { let mut t = String::new(); std::io::stdin().read_line(&mut t).ok(); let t: usize = t.trim().parse().unwrap(); let mut result = Vec::with_capacity(t); for _ in 0..t { result.push(solve()); } for &v in result.iter() { println!("{}", v); } }