#include #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; typedef long long ll; int main(){ cin.tie(0); ios::sync_with_stdio(0); int T; cin >> T; rep(_,T) { int N; cin >> N; vector A(N); rep(i,N) cin >> A[i]; auto f = [N](vector A) { vector dp(N + 1, 0); rep(i,N) { if(i + 2 < N) { // A[i], A[i + 1], A[i + 2] auto [mi, ma] = minmax({A[i], A[i + 1], A[i + 2]}); if(A[i] != A[i + 1] && A[i + 1] != A[i + 2] && A[i + 2] != A[i] && (mi == A[i + 1] || ma == A[i + 1])) { dp[i + 3] = max(dp[i + 3], dp[i] + A[i]); } } dp[i + 1] = max(dp[i + 1], dp[i]); } return dp[N]; }; int ans = 0; rep(_,3) { ans = max(ans, f(A)); vector nt; nt.push_back(A.back()); rep(i,N-1) nt.push_back(A[i]); swap(A, nt); } cout << ans << "\n"; } }