#include using namespace std; #define rep(i,n) for(ll i = 0; i < ll(n); i++) using ll = long long; using P = pair; bool kadomatsu(ll a, ll b, ll c) { if (a != b && b != c && c != a && (max({a, b, c}) == b || min({a, b, c}) == b)) return true; return false; } void solve() { ll n; cin >> n; vector a(n); rep(i, n) { cin >> a[i]; } rep(i, 3) { vector b; rep(j, n) { b.push_back(a[(i + j) % n]); } vector dp(n + 1); rep(i, n - 2) { if (kadomatsu(b[i], b[i + 1], b[i + 2])) { dp[i + 3] = max(dp[i + 3], dp[i] + b[i]); } } cout << dp[n] << endl; } } int main() { ll t; cin >> t; while (t--) { solve(); } return 0; }