#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
pair<cat,cat> dist(int u, int v, vector<cat> &S) {
if(u > v) swap(u,v);
return make_pair(S[v]-S[u], S[S.size()-1]-S[v]+S[u]);
}
cat minp(pair<cat,cat> p) {
return min(p.ff,p.ss);
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(10);
int T;
cin >> T;
while(T--) {
int N,Q;
cin >> N >> Q;
vector<int> A(N);
vector< vector<cat> > cycw(N);
for(int i =0; i < N; i++) {
cin >> A[i];
cycw[i].resize(A[i]);
for(int j =0; j < A[i]; j++) cin >> cycw[i][j];
}
vector<cat> w(N), v1(N), v2(N);
for(int i =0; i < N; i++) {
cin >> v1[i] >> v2[(i+1)%N] >> w[i];
v1[i]--, v2[(i+1)%N]--;
}
// prefix sums in outer cycle
vector<cat> W(N+1,0);
for(int i =0; i < N; i++) W[i+1] =W[i]+w[i];
// prefix sums in inner cycles
vector< vector<cat> > cycW(N);
for(int i =0; i < N; i++) {
cycW[i].resize(A[i]+1,0);
for(int j =0; j < A[i]; j++) cycW[i][j+1] =cycW[i][j]+cycw[i][j];
}
// compute X[i] + prefix sums
vector<cat> X(N), S(N+1,0);
for(int i =0; i < N; i++) X[i] =minp(dist(v1[i],v2[i],cycW[i]));
for(int i =0; i < N; i++) S[i+1] =S[i]+X[i];
// queries
for(int q =0; q < Q; q++) {
int u1,c1,u2,c2;
cin >> u1 >> c1 >> u2 >> c2;
u1--, c1--, u2--, c2--;
if(c1 > c2) {
swap(c1,c2);
swap(u1,u2);
}
pair<cat,cat> ans =dist(c1,c2,W); // dist. in outer cycle
// add dist. from u1, u2 in inner cycles
ans.ff +=minp(dist(u1,v1[c1],cycW[c1])) + minp(dist(u2,v2[c2],cycW[c2]));
ans.ss +=minp(dist(u1,v2[c1],cycW[c1])) + minp(dist(u2,v1[c2],cycW[c2]));
// add dist. v1--v2 in inner cycles
ans.ff +=S[c2]-S[c1+1];
ans.ss +=S[N]-S[c2+1]+S[c1];
cout << minp(ans) << "\n";
}
}
}
// look at my code
// my code is amazing
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Ci8vIGlvc3RyZWFtIGlzIHRvbyBtYWluc3RyZWFtCiNpbmNsdWRlIDxjc3RkaW8+Ci8vIGJpdGNoIHBsZWFzZQojaW5jbHVkZSA8aW9zdHJlYW0+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDxjc3RkbGliPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8c2V0PgojaW5jbHVkZSA8bWFwPgojaW5jbHVkZSA8cXVldWU+CiNpbmNsdWRlIDxzdGFjaz4KI2luY2x1ZGUgPGxpc3Q+CiNpbmNsdWRlIDxjbWF0aD4KI2luY2x1ZGUgPGlvbWFuaXA+CiNpbmNsdWRlIDx0aW1lLmg+CiNkZWZpbmUgZGlicyByZXNlcnZlCiNkZWZpbmUgT1ZFUjkwMDAgMTIzNDU2Nzg5MDEyMzQ1Njc4OUxMCiNkZWZpbmUgQUxMX1RIRShDQUtFLExJRSkgZm9yKGF1dG8gTElFID1DQUtFLmJlZ2luKCk7IExJRSAhPSBDQUtFLmVuZCgpOyBMSUUrKykKI2RlZmluZSB0aXNpYyA0NwojZGVmaW5lIHNvY2xvc2UgMWUtOAoj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