fn is_kadomatsu(a: &Vec) -> bool { if a[0] == a[1] || a[1] == a[2] || a[2] == a[0] { return false; } (a[0] < a[1] && a[1] > a[2]) || (a[0] > a[1] && a[1] < a[2]) } 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 ret = 0usize; for start in 0..3 { let mut dp = vec![vec![0usize; 2]; n+3]; for i in 0..n-2 { let kados = vec![a[start+i], a[(start+i+1)%n], a[(start+i+2)%n]]; dp[i+1][0] = dp[i+1][0].max(dp[i][0]).max(dp[i][1]); if is_kadomatsu(&kados) { dp[i+2][1] = dp[i+2][1].max(dp[i][0] + kados[0]); } } ret = ret.max(dp.iter().map(|v| *v.iter().max().unwrap()).max().unwrap()); } ret } 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); } }