fn is_kadomatsu(a: &Vec) -> bool { a[0] != a[1] && a[1] != a[2] && a[2] != a[0] && (*a.iter().max().unwrap() == a[1] || *a.iter().min().unwrap() == a[1]) } fn solve() -> isize { 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 = 0isize; for start in 0..3 { let mut dp = vec![vec![0isize; 2]; n+1]; for i in 1+start..=n+start-2 { let aval = a[(i-1) % n]; dp[i][0] = dp[i-1][0]; dp[i][1] = dp[i-1][0]; dp[i][0] = dp[i][0].max(if i<3 { 0 } else { dp[i-3][1] }); let vals = vec![aval, a[i % n], a[(i+1) % n]]; dp[i][1] = dp[i][1].max(if i<3 { 0 } else { dp[i-3][1] }) + if is_kadomatsu(&vals) { aval } else { 0 }; } result = result.max(*dp[n+start-2].iter().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); } }