#include #include #define rep(i, j, n) for (int i = j; i < (n); ++i) #define rrep(i, j, n) for (int i = (n)-1; j <= i; --i) #define min(a, b) (((a) < (b)) ? (a) : (b)) #define max(a, b) (((a) > (b)) ? (a) : (b)) const int INF = 0x3f3f3f3f; const long long INFL = 0x3f3f3f3f3f3f3f3fLL; long long n, a[200010], dp[200010]; int main(void) { int t; scanf("%lld", &t); while (t--) { scanf("%lld", &n); rep(i, 0, n) { scanf("%lld", &a[i]); } memcpy(a + n, a, 9); long long ans = 0; rep(o, 0, 3) { memset(dp, 0, n + 5); rep(i, 1, n + 1) dp[i] = -INF; rep(i, 0, n) { dp[i + 1] = max(dp[i + 1], dp[i]); if (i + 3 <= n && (a[i + o] != a[i + 1 + o] && a[i + 1 + o] != a[i + 2 + o] && a[i + 2 + o] != a[i + o]) && ((a[i + o] > a[i + 1 + o] && a[i + 1 + o] < a[i + 2 + o]) || (a[i + o] < a[i + 1 + o] && a[i + 1 + o] > a[i + 2 + o]))) { dp[i + 3] = max(dp[i + 3], dp[i] + a[i + o]); } } ans = max(ans, dp[n]); } printf("%d\n", ans); } }