include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m;
int x[11], y[11], k[11], t[11];
struct Equa2 {
int x, c, m;
Equa2(int x, int c, int m): x(x), c(c), m(m) {}
};
ll ans;
vector
struct Equa {
int x, c, m;
Equa(int x, int c, int m): x(x), c(c), m(m) {}
bool operator<(const Equa & e)const {
return x<e.x;
}
};
int m[11],c[11];
int gcd(int a,int b){
if(!b)return a;
else return gcd(b,a%b);
}
void exgcd(int a,int b,int x,int y){
if(!b)
x=1,y=0;
else
exgcd(b,a%b,y,x),y-=a/b*x;
}
int inv(int a,int b){
int x=0,y=0;
exgcd(a,b,x,y);
x=(x%b+b)%b;
if(!x)x+=b;
return x;
}
int EXCRT() {
if(n==1)
return c[1];
//x<=1?
for(int i = 2; i <= n; ++i) {
int m1 = m[i], m2 = m[i];
int c1 = c[i], c2 = c[i];
int t = gcd(m1, m2);
if((c2 - c1) % t != 0)
return -1;
m[i] = m1 * m2 / t;
c[i] = inv(m1 / t, m2 / t) * ((c2 - c1) / t) % (m2 / t) * m1 + c1;
c[i] = (c[i] % m[i] + m[i]) % m[i];
}
return c[n];
}
void calc() {
int curxe = 0, curye = 0;
sort(vx.begin(),vx.end());
sort(vy.begin(),vy.end());
int Lx=0,Ly=0;
int Rx=vx[0].x,Ry=vy[0].x;
for(int i=0;i<n;++i){
m[i+1]=vx[0].m;
c[i+1]=(vx[0].c+vx[0].x)%vx[0].m;
}
int resx=EXCRT();
for(int i=0;i<n;++i){
m[i+1]=vy[0].m;
c[i+1]=(vy[0].c+vy[0].x)%vy[0].m;
}
int resy=EXCRT();
//res
//???
int calcx=R[x]
}
void dfs(int id) {
if(id > n) {
calc();
return;
}
for(int xti = 0; xti <= t[id]; ++xti) {
vx.push_back(Equa2(x[id], xti, k[id]));
vy.push_back(Equa2(y[id], t[id] - xti, k[id]));
dfs(id + 1);
vx.pop_back();
vy.pop_back();
}
}
int main() {
ifdef local
freopen("a.txt", "r", stdin);
endif // loccal
int t;
scanf("%d", &t);
while(t--) {
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i) {
scanf("%d%d%d%d", &x[i], &y[i], &k[i].&t[i]);
}
ans = 0;
dfs(1);
}
return 0;
}