#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <cmath>
#include <iomanip>
#include <time.h>
#define dibs reserve
#define OVER9000 1234567890123456789LL
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):x)
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge
typedef long long cat;
#ifdef DONLINE_JUDGE
// palindromic tree is better than splay tree!
#define lld I64d
#endif
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(10);
int T;
cin >> T;
while(T--) {
int N;
cin >> N;
vector<int> M(N);
vector< vector<int> > A(N);
for(int i =0; i < N; i++) {
cin >> M[i];
A[i].resize(M[i]);
for(int j =0; j < M[i]; j++) cin >> A[i][j];
}
vector< vector<cat> > S(N);
for(int i =0; i < N; i++) S[i].resize(M[i],0);
for(int i =1; i < N; i++) {
cat max1 =-OVER9000, max2 =-OVER9000;
for(int j =0; j < M[i-1]; j++) {
max1 =max(max1,S[i-1][(j+1)%M[i-1]]-1LL*i*A[i-1][j]);
max2 =max(max2,S[i-1][(j+1)%M[i-1]]+1LL*i*A[i-1][j]);
}
for(int j =0; j < M[i]; j++)
S[i][j] =max(S[i][j],max(max1+1LL*i*A[i][j],max2-1LL*i*A[i][j]));
}
cat ans =0;
for(int i =0; i < M[N-1]; i++) ans =max(ans,S[N-1][i]);
cout << ans << "\n";
}
}
// look at my code
// my code is amazing
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Ci8vIGlvc3RyZWFtIGlzIHRvbyBtYWluc3RyZWFtCiNpbmNsdWRlIDxjc3RkaW8+Ci8vIGJpdGNoIHBsZWFzZQojaW5jbHVkZS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