import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; public class Main { static BufferedReader br; static PrintWriter pw; public static void main(String[] args) throws Exception { br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); pw = new PrintWriter(System.out); for (int i = 0; i < t; i++) { solve(); } br.close(); pw.flush(); } static void solve() throws Exception { int n = Integer.parseInt(br.readLine()); String[] sa = br.readLine().split(" "); int[] a = new int[n + 2]; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(sa[i]); } long[][] dp = new long[n + 3][3]; for (int i = 3; i < n + 3; i++) { for (int j = 0; j < 3; j++) { dp[i][j] = Math.max(dp[i][j], dp[i - 1][j]); dp[i][j] = Math.max(dp[i][j], dp[i - 2][j]); } int[] b = new int[3]; for (int j = 1; j <= 3; j++) { b[j - 1] = a[(i - j) % n]; } if (check(b)) { if (i < 6) { dp[i][i - 3] = a[i - 3]; } else { for (int j = 0; j < 3; j++) { dp[i][j] = Math.max(dp[i][j], dp[i - 3][j] + a[i - 3]); } } } } long ans = 0; for (int i = 0; i < 3; i++) { ans = Math.max(ans, dp[n + i][i]); } pw.println(ans); } static boolean check(int[] a) { if (a[0] != a[2]) { if (a[0] < a[1] && a[1] > a[2]) return true; if (a[0] > a[1] && a[1] < a[2]) return true; } return false; } }