pub mod input { pub trait ReadIntHelper: PartialOrd + Copy { fn min_value() -> Self; fn from_u32(u: u32) -> Self; fn checked_mul(&self, other: u32) -> Option; fn checked_sub(&self, other: u32) -> Option; fn checked_add(&self, other: u32) -> Option; } macro_rules! impl_read_int_helper { ($($t:ident)*) => ($(impl ReadIntHelper for $t { #[inline] fn min_value() -> Self { std::$t::MIN } #[inline] fn from_u32(u: u32) -> Self { u as Self } #[inline] fn checked_mul(&self, other: u32) -> Option { Self::checked_mul(*self, other as Self) } #[inline] fn checked_sub(&self, other: u32) -> Option { Self::checked_sub(*self, other as Self) } #[inline] fn checked_add(&self, other: u32) -> Option { Self::checked_add(*self, other as Self) } })*) } impl_read_int_helper! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize } #[inline] pub fn read_int(r: &mut R) -> T { let is_signed_ty = T::from_u32(0) > T::min_value(); let mut is_positive = true; let mut is_empty = true; let mut is_only_sign = true; let mut result = T::from_u32(0); loop { let buf = match r.fill_buf() { Ok(buf) => buf, Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue, Err(e) => panic!("{}", e), }; let (done, used, mut buf) = match buf.iter().position(u8::is_ascii_whitespace) { Some(i) => (i > 0 || !is_empty, i + 1, &buf[..i]), None => (buf.is_empty(), buf.len(), buf), }; if is_empty && buf.len() > 0 { is_empty = false; buf = match buf[0] { b'+' => &buf[1..], b'-' if is_signed_ty => { is_positive = false; &buf[1..] } _ => buf, }; } if buf.len() > 0 { is_only_sign = false; } if is_positive { for &c in buf { let x = (c as char).to_digit(10).expect("InvalidDigit"); result = result.checked_mul(10).expect("PosOverflow"); result = result.checked_add(x).expect("PosOverflow"); } } else { for &c in buf { let x = (c as char).to_digit(10).expect("InvalidDigit"); result = result.checked_mul(10).expect("NegOverflow"); result = result.checked_sub(x).expect("NegOverflow"); } } r.consume(used); if done { if is_empty { panic!("Empty"); } if is_only_sign { panic!("InvalidDigit"); } return result; } } } #[inline] pub fn read_bytes(r: &mut R) -> Vec { let mut result = Vec::new(); loop { let buf = match r.fill_buf() { Ok(buf) => buf, Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue, Err(e) => panic!("{}", e), }; let (done, used, buf) = match buf.iter().position(u8::is_ascii_whitespace) { Some(i) => (i > 0 || !result.is_empty(), i + 1, &buf[..i]), None => (buf.is_empty(), buf.len(), buf), }; result.extend_from_slice(buf); r.consume(used); if done { return result; } } } #[macro_export] macro_rules! read { ($r:expr, [$t:tt; $n:expr]) => ((0..$n).map(|_| read!($r, $t)).collect::>()); ($r:expr, [$t:tt]) => (read!($r, [$t; read!($r, usize)])); ($r:expr, ($($t:tt),*)) => (($(read!($r, $t)),*)); ($r:expr, Usize1) => (read!($r, usize) - 1); ($r:expr, String) => (String::from_utf8(read!($r, Bytes)).unwrap()); ($r:expr, Bytes) => (input::read_bytes($r)); ($r:expr, $t:ty) => (input::read_int::<_, $t>($r)); } #[macro_export] macro_rules! input { ($r:expr, $($($v:ident)* : $t:tt),* $(,)?) => { $(let $($v)* = read!($r, $t);)* }; } } fn run(reader: &mut R, writer: &mut W) { 't: for _ in 0..read!(reader, usize) { input! { reader, n: usize, mut a: [usize; 2 * n], } let x: Vec<_> = (1..=n).cycle().take(2 * n).collect(); macro_rules! done { ($s:tt) => { writeln!(writer, "{}", $s).ok(); continue 't; }; } if a == x { done!("Yes"); } for i in 1..=n { if let Some(l) = a.iter().position(|a| *a == i) { let r = a.iter().rposition(|a| *a == i).unwrap(); a[l..=r].reverse(); if a == x { done!("Yes"); } a[l..=r].reverse(); } else { done!("No"); } } done!("No"); } } fn main() { let (stdin, stdout) = (std::io::stdin(), std::io::stdout()); let ref mut reader = std::io::BufReader::new(stdin.lock()); let ref mut writer = std::io::BufWriter::new(stdout.lock()); run(reader, writer); }