#include <bits/extc++.h>
using namespace std;
int f[1002][202][202][3]{};int a[1002][3]{};
int main()
{
////cin.tie(0)->sync_with_stdio(0);cout.tie(0);
int t;cin >> t;
while(t--) {
__builtin_memset(f,0,sizeof f);
int n;cin >> n;
int as{};
for(int i = 1;i <= n; ++i)cin >> a[i][0] >> a[i][1] >> a[i][2];
// for(int i=1;i <= n;++i)for(int x=0;x<=200;++x)for(int y=0;y<=200;++y)for(int k=0;k<3;++k) {
// int nx=x?x+1:0, ny=y?y+1:0;
// f[i][nx][ny][k]=max(f[i][nx][ny][k],f[i-1][x][y][k]-nx-ny+a[i][k]);
// f[i][ny][1][(k+1)%3]=max(f[i][ny][1][(k+1)%3],f[i-1][x][y][k]-1-ny+a[i][(k+1)%3]);
// f[i][1][nx][(k+1)%3]=max(f[i][1][nx][(k+1)%3],f[i-1][x][y][k]-1-nx+a[i][(k+2)%3]);
// }
int mm = min(194,n);
for(int i = 1;i <= n; ++i) {
for(int j = 0;j < mm; ++j)for(int k = 0;k < mm;++k) {
for(int l = 0;l < 3; ++l) {
int nj = j?j+1:0, nk = k?k+1:0;
f[i][nj][nk][l] = max(f[i][nj][nk][l],f[i-1][j][k][l] + a[i][l] - nj - nk);
f[i][nk][1][(l+1)%3] = max(f[i][nk][1][(l+1)%3],f[i-1][j][k][l] + a[i][(l+1)%3] - 1 - nk);
f[i][1][nj][(l+2)%3] = max(f[i][1][nj][(l+2)%3],f[i-1][j][k][l] + a[i][(l+2)%3] - 1 - nj);
}
}
}
for(int j = 0;j <= m; ++j)for(int k = 0;k <= m;++k)for(int l = 0;l < 3;++l) {
// cout<<f[n][j][k][l]<<"\t";
as = max(as,f[n][j][k][l]);
}
cout<<as<<"\n";
}
}