#include using namespace std; #define int long long const int N = 2e5 + 5; int c, T; int n; struct dat { int l1, r1, l2, r2; } a[N]; int b[N << 1]; vector st[N << 1], ed[N << 1]; signed main() { freopen("lucky.in", "r", stdin); freopen("lucky.out", "w", stdout); ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> c >> T; while (T--) { cin >> n; int pre1 = 0, pre2 = 0, now = 0, suf1 = 0, suf2 = 0; for (int i = 1, l1, r1, l2, r2; i <= n; i++) { cin >> l1 >> r1 >> l2 >> r2; suf1 += l1, suf2 += r1; a[i] = {l1, r1, l2, r2 + 1}; b[i] = l2, b[i + n] = r2 + 1; } sort(b + 1, b + 2 * n + 1); int t = unique(b + 1, b + 2 * n + 1) - (b + 1); for (int i = 1; i <= n; i++) { a[i].l2 = lower_bound(b + 1, b + t + 1, a[i].l2) - b; a[i].r2 = lower_bound(b + 1, b + t + 1, a[i].r2) - b; st[a[i].l2].push_back(i); ed[a[i].r2].push_back(i); } int ans = 0; // cout << 48670672 + 602959541 - 288854721 << endl; for (int i = 1; i < t; i++) { for (int x : ed[i]) { pre1 += a[x].l1; pre2 += a[x].r1; now -= a[x].r1; } for (int x : st[i]) { suf1 -= a[x].l1; suf2 -= a[x].r1; now += a[x].r1; } // cout << pre1 << " " << pre2 << " " << now << " " << suf1 << " " << suf2 << endl; if (!now) continue; int m = (pre1 + now + suf1 + 1) / 2; if (m > pre1 && m <= pre1 + now) { ans += b[i + 1] - b[i]; //cout << now << " " << ans << endl; continue; } else if (m <= pre1) { m = (pre1 + now + suf2 + 1) / 2; if (m > pre1) { ans += b[i + 1] - b[i]; // cout << now << " " << ans << endl; continue; } } else if (m > pre1 + now) { m = (pre2 + now + suf1 + 1) / 2; if (m <= pre2 + now) { ans += b[i + 1] - b[i]; // cout << now << " " << ans << endl; continue; } } } cout << ans << '\n'; for (int i = 1; i <= t; i++) st[i].clear(), ed[i].clear(); } return 0; }