#include #include #include using namespace std; int f(int a , int b , int c){ if(a == b || b == c || c == a){ return 0; } if(b < a && b < c){ return a; } if(b > a && b > c){ return a; } return 0; } void solve(){ int n; cin >> n; vector a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } for(int i = 0; i < n % 3 + 2; i++){ a.push_back(a[i]); } long long res = 0; long long cost0 = 0 , cost1 = 0 , cost2 = 0; for (int i = 0; i < n; i++) { if(i % 3 == 0)cost0 += f(a[i] , a[i + 1] , a[i + 2]); if(i % 3 == 1)cost1 += f(a[i] , a[i + 1] , a[i + 2]); if(i % 3 == 2)cost2 += f(a[i] , a[i + 1] , a[i + 2]); } res = max({cost0 , cost1 , cost2}); cout << res << endl; } int main() { int t; cin >> t; while(t--){ solve(); } return 0; }