mod my { #[macro_export] macro_rules! scan { ($r:expr, [$t:tt; $n:expr]) => ( (0..$n).map(|_| scan!($r, $t)).collect::>() ); ($r:expr, ($($t:tt),*)) => ( ($(scan!($r, $t)),*) ); ($r:expr, $t:ty) => ({ std::str::from_utf8(&scan($r)).unwrap().parse::<$t>().unwrap() }); } pub mod io { use std::io::{BufRead, ErrorKind}; pub fn scan(r: &mut R) -> Vec { let mut buf = Vec::new(); loop { let (done, used) = { let available = match r.fill_buf() { Ok(n) => n, Err(ref e) if e.kind() == ErrorKind::Interrupted => continue, Err(e) => panic!(e), }; match available.iter().position(u8::is_ascii_whitespace) { Some(i) => { buf.extend_from_slice(&available[..i]); (buf.len() > 0, i + 1) } None => { buf.extend_from_slice(available); (false, available.len()) } } }; r.consume(used); if done || used == 0 { return buf; } } } } } use std::io::{BufRead, Write}; use my::io::scan; fn run(reader: &mut R, writer: &mut W) { for _ in 0..scan!(reader, usize) { let n = scan!(reader, usize); let mut a = scan!(reader, [usize; n]); a.extend_from_slice(&a[..3].to_vec()); let mut ans = 0; let mut dp = vec![vec![0; 3]; n + 3]; for (i, a) in a.windows(3).enumerate() { for j in 0..3 { dp[i + 1][j] = dp[i + 1][j].max(dp[i][j]); } if a[0] == a[2] { continue; } if a[0] > a[1] && a[1] < a[2] || a[0] < a[1] && a[1] > a[2] { for j in 0..3 { if i < j || n + j <= i + 2 { continue; } let next = dp[i][j] + a[0]; dp[i + 3][j] = dp[i + 3][j].max(next); ans = ans.max(next); } } } writeln!(writer, "{}", ans).ok(); } } fn main() { let (stdin, stdout) = (std::io::stdin(), std::io::stdout()); let reader = &mut std::io::BufReader::new(stdin.lock()); let writer = &mut std::io::BufWriter::new(stdout.lock()); run(reader, writer); }