J. Skills
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e3+5;
ll dp[2][4][205][205],a[1005][4];
void solve(){
ll ans = 0;
memset(dp,0,sizeof(dp));
memset(a,0,sizeof(a));
ll n;cin >> n;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= 3;j++){
cin >> a[i][j];
}
}
for(int i = 0;i < n;i++){
// dp[(i+1)%2][1][0][0] = dp[i%2][1][0][0] + a[(i+1)][1];
// dp[(i+1)%2][2][0][0] = dp[i%2][2][0][0] + a[(i+1)][2];
// dp[(i+1)%2][3][0][0] = dp[i%2][3][0][0] + a[(i+1)][3];
//1
for(int x = 0;x <= i&&x <= 200;x++){ // x 2 y 3
for(int y = 0;y <= i&&y <= 200;y++){
if(x!=0&&y!=0){
dp[(i+1)%2][1][x+1][y+1] = max(dp[(i+1)%2][1][x+1][y+1],a[(i+1)][1] + dp[i%2][1][x][y] - x - y);
dp[(i+1)%2][2][2][y+1] = max(dp[(i+1)%2][2][2][y+1],a[(i+1)][2] + dp[i%2][1][x][y] - 1 - y);
dp[(i+1)%2][3][2][x+1] = max(dp[(i+1)%2][3][2][x+1],a[(i+1)][3] + dp[i%2][1][x][y] - 1 - x);
}else if(x==0&&y!=0){
dp[(i+1)%2][1][x][y+1] = max(dp[(i+1)%2][1][x][y+1],a[(i+1)][1] + dp[i%2][1][x][y] - x - y);
dp[(i+1)%2][2][2][y+1] = max(dp[(i+1)%2][2][2][y+1],a[(i+1)][2] + dp[i%2][1][x][y] - 1 - y);
dp[(i+1)%2][3][2][x] = max(dp[(i+1)%2][3][2][x],a[(i+1)][3] + dp[i%2][1][x][y] - 1 - x);
}else if(x!=0&&y==0){
dp[(i+1)%2][1][x+1][y] = max(dp[(i+1)%2][1][x+1][y],a[(i+1)][1] + dp[i%2][1][x][y] - x - y);
dp[(i+1)%2][2][2][y] = max(dp[(i+1)%2][2][2][y],a[(i+1)][2] + dp[i%2][1][x][y] - 1 - y);
dp[(i+1)%2][3][2][x+1] = max(dp[(i+1)%2][3][2][x+1],a[(i+1)][3] + dp[i%2][1][x][y] - 1 - x);
}else if(x==0&&y==0){
dp[(i+1)%2][1][x][y] = max(dp[(i+1)%2][1][x][y],a[(i+1)][1] + dp[i%2][1][x][y] - x - y);
dp[(i+1)%2][2][2][y] = max(dp[(i+1)%2][2][2][y],a[(i+1)][2] + dp[i%2][1][x][y] - 1 - y);
dp[(i+1)%2][3][2][x] = max(dp[(i+1)%2][3][2][x],a[(i+1)][3] + dp[i%2][1][x][y] - 1 - x);
}
}
}
//2
for(int x = 0;x <= i&&x <= 200;x++){ // x 1 y 3
for(int y = 0;y <= i&&y <= 200;y++){
if(x!=0&&y!=0){
dp[(i+1)%2][2][x+1][y+1] = max(dp[(i+1)%2][2][x+1][y+1],a[(i+1)][2] + dp[i%2][2][x][y] - x - y);
dp[(i+1)%2][1][2][y+1] = max(dp[(i+1)%2][1][2][y+1],a[(i+1)][1] + dp[i%2][2][x][y] - 1 - y);
dp[(i+1)%2][3][x+1][2] = max(dp[(i+1)%2][3][x+1][2],a[(i+1)][3] + dp[i%2][2][x][y] - 1 - x);
}else if(x==0&&y!=0){
dp[(i+1)%2][2][x][y+1] = max(dp[(i+1)%2][2][x][y+1],a[(i+1)][2] + dp[i%2][2][x][y] - x - y);
dp[(i+1)%2][1][2][y+1] = max(dp[(i+1)%2][1][2][y+1],a[(i+1)][1] + dp[i%2][2][x][y] - 1 - y);
dp[(i+1)%2][3][x][2] = max(dp[(i+1)%2][3][x][2],a[(i+1)][3] + dp[i%2][2][x][y] - 1 - x);
}else if(x!=0&&y==0){
dp[(i+1)%2][2][x+1][y] = max(dp[(i+1)%2][2][x+1][y],a[(i+1)][2] + dp[i%2][2][x][y] - x - y);
dp[(i+1)%2][1][2][y] = max(dp[(i+1)%2][1][2][y],a[(i+1)][1] + dp[i%2][2][x][y] - 1 - y);
dp[(i+1)%2][3][x+1][2] = max(dp[(i+1)%2][3][x+1][2],a[(i+1)][3] + dp[i%2][2][x][y] - 1 - x);
}else if(x==0&&y==0){
dp[(i+1)%2][2][x][y] = max(dp[(i+1)%2][2][x][y],a[(i+1)][2] + dp[i%2][2][x][y] - x - y);
dp[(i+1)%2][1][2][y] = max(dp[(i+1)%2][1][2][y],a[(i+1)][1] + dp[i%2][2][x][y] - 1 - y);
dp[(i+1)%2][3][x][2] = max(dp[(i+1)%2][3][x][2],a[(i+1)][3] + dp[i%2][2][x][y] - 1 - x);
}
}
}
//3
for(int x = 0;x <= i&&x <= 200;x++){ // x 1 y 2
for(int y = 0;y <= i&&y <= 200;y++){
if(x!=0&&y!=0){
dp[(i+1)%2][3][x+1][y+1] = max(dp[(i+1)%2][3][x+1][y+1],a[(i+1)][3] + dp[i%2][3][x][y] - x - y);
dp[(i+1)%2][2][x+1][2] = max(dp[(i+1)%2][2][x+1][2],a[(i+1)][2] + dp[i%2][3][x][y] - 1 - x);
dp[(i+1)%2][1][y+1][2] = max(dp[(i+1)%2][1][y+1][2],a[(i+1)][1] + dp[i%2][3][x][y] - 1 - y);
}else if(x==0&&y!=0){
dp[(i+1)%2][3][x][y+1] = max(dp[(i+1)%2][3][x][y+1],a[(i+1)][3] + dp[i%2][3][x][y] - x - y);
dp[(i+1)%2][2][x][2] = max(dp[(i+1)%2][2][x][2],a[i+1][2] + dp[i%2][3][x][y] - 1 - x);
dp[(i+1)%2][1][y+1][2] = max(dp[(i+1)%2][1][y+1][2],a[(i+1)][1] + dp[i%2][3][x][y] - 1 - y);
}else if(x!=0&&y==0){
dp[(i+1)%2][3][x+1][y] = max(dp[(i+1)%2][3][x+1][y],a[(i+1)][3] + dp[i%2][3][x][y] - x - y);
dp[(i+1)%2][2][x+1][2] = max(dp[(i+1)%2][2][x+1][2],a[(i+1)][2] + dp[i%2][3][x][y] - 1 - x);
dp[(i+1)%2][1][y][2] = max(dp[(i+1)%2][1][y][2],a[(i+1)][1] + dp[i%2][3][x][y] - 1 - y);
}else if(x==0&&y==0){
dp[(i+1)%2][3][x][y] = max(dp[(i+1)%2][3][x][y],a[(i+1)][3] + dp[i%2][3][x][y] - x - y);
dp[(i+1)%2][2][x][2] = max(dp[(i+1)%2][2][x][2],a[(i+1)][2] + dp[i%2][3][x][y] - 1 - x);
dp[(i+1)%2][1][y][2] = max(dp[(i+1)%2][1][y][2],a[(i+1)][1] + dp[i%2][3][x][y] - 1 - y);
}
}
}
}
// cout<<dp[n][1][2][3] << "?";
for(int j = 1;j <= 3;j++){
for(int x = 0;x <= 200;x++){
for(int y = 0; y <= 200;y++ ){
ans = max(ans , dp[(n)%2][j][x][y]);
}
}
}
cout << ans << "\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--){
solve();
}
}