#include #define rep(i,n) for(int i = 0; i < (n); ++i) #define srep(i,s,t) for (int i = s; i < t; ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) using namespace std; typedef long long int ll; typedef pair P; #define yn {puts("YES");}else{puts("NO");} #define MAX_N 200005 int isKadomatsu(int x, int y, int z){ int res = 0; if(y < x && y < z && x != z) res = 1; if(y > x && y > z && x != z) res = 1; return res; } int main() { int T; cin >> T; rep(_,T){ int n; cin >> n; ll b[n*2]; rep(i,n){ cin >> b[i]; b[i+n] = b[i]; } ll ans = 0; rep(loop, 3){ ll a[n]; rep(i,n){ a[i] = b[i+loop]; } ll dp[n] = {}; srep(i,2,n){ dp[i] = dp[i-1]; if(isKadomatsu(a[i-2],a[i-1],a[i])){ if(i == 2) dp[i] = a[i-2]; else dp[i] = max(dp[i], dp[i-3] + a[i-2]); } } ans = max(ans, dp[n-1]); } cout << ans << endl; } return 0; }