using static System.Math; using System; public class Hello { public static int[] a, b, c; static void Main() { var q = int.Parse(Console.ReadLine().Trim()); while (q-- > 0) { var n = int.Parse(Console.ReadLine().Trim()); string[] line = Console.ReadLine().Trim().Split(' '); a = new int[n]; b = new int[n]; c = new int[n]; for (int i = 0; i < n; i++) { var t = int.Parse(line[i]); a[i] = t; if (i - 1 >= 0) b[i - 1] = t; if (i - 2 >= 0) c[i - 2] = t; } b[n - 1] = a[0]; c[n - 2] = a[0]; c[n - 1] = a[1]; var ans = Max(Dp(n, a), Dp(n, b)); ans = Max(ans, Dp(n, c)); Console.WriteLine(ans); } } static long Dp(int n, int[] t) { var dp = new long[n]; dp[2] = check(t[0], t[1], t[2]) ? t[0] : 0; for (int i = 3; i < n; i++) { if (check(t[i - 2], t[i - 1], t[i])) dp[i] = Max(dp[i - 1], dp[i - 3] + t[i - 2]); else dp[i] = dp[i - 1]; } return dp[n - 1]; } static bool check(int t0, int t1, int t2) { if (!(t0 != t1 && t1 != t2 && t0 != t2)) return false; if (t1 > t0 && t1 > t2) return true; if (t1 < t0 && t1 < t2) return true; return false; } }