using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); } static void Solve() { var t = NN; var ans = new long[t]; for (var u = 0; u < t; ++u) { var n = NN; var a = NList; ans[u] = Kado(n, a); } WriteLine(string.Join("\n", ans)); } static long Kado(int n, int[] a) { var list = a.ToList(); var ans = Kado(n, list); list.Add(list[0]); list.RemoveAt(0); ans = Math.Max(ans, Kado(n, list)); list.Add(list[0]); list.RemoveAt(0); ans = Math.Max(ans, Kado(n, list)); return ans; } static long Kado(int n, List a) { var dp = new long[a.Count - 2]; for (var i = 0; i + 2 < a.Count; ++i) { if (a[i] != a[i + 2] && ((a[i] > a[i + 1] && a[i + 1] < a[i + 2]) || (a[i] < a[i + 1] && a[i + 1] > a[i + 2]))) dp[i] = a[i]; } for (var i = 0; i < dp.Length; ++i) { if (i - 3 >= 0) dp[i] += dp[i - 3]; if (i > 0) dp[i] = Math.Max(dp[i], dp[i - 1]); } return dp[^1]; } }