#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <iomanip>
#define FOR(i,a,b) for(int i=(a),_b=(b); i <= _b; ++i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(x) { cout << #x << " = " << x << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
using namespace std;
const long double EPS = 1e-6;
struct Point {
long double x, y;
Point(long double x = 0, long double y = 0) : x(x), y(y) {}
Point operator - (Point a) { return Point(x-a.x, y-a.y); }
long double sqlen() { return x*x + y*y; }
long double len() { return sqrt(sqlen()); }
void print() { cout << x << ' ' << y << endl; }
} points[111], O(0, 0);
int ccw(Point a, Point b, Point c) {
b = b - a; c = c - a;
long double t = b.x*c.y - b.y*c.x;
if (fabs(t) < EPS) return 0;
else if (t < 0) return -1;
else return 1;
}
bool operator < (const Point &a, const Point &b) {
int t = ccw(O, a, b);
if (t == 0) return (const_cast<Point &>(a)).sqlen() < (const_cast<Point &>(b)).sqlen();
return t > 0;
}
long double area(Point a, Point b, Point c) {
b = b - a; c = c - a;
long double t = b.x*c.y - b.y*c.x;
return fabs(t) / 2.0;
}
struct Rect {
Point A, B, C, D;
} a[111];
int n;
long double X, Y;
int nPoint;
struct Line { long double a, b, c; }; // a way to represent a line
void pointsToLine(Point p1, Point p2, Line &l) {
if (fabs(p1.x - p2.x) < EPS) { // vertical line is fine
l.a = 1.0; l.b = 0.0; l.c = -p1.x; // default values
} else {
l.a = -(long double)(p1.y - p2.y) / (p1.x - p2.x);
l.b = 1.0; // IMPORTANT: we fix the value of b to 1.0
l.c = -(long double)(l.a * p1.x) - p1.y;
}
}
bool areParallel(Line l1, Line l2) { // check coefficients a & b
return (fabs(l1.a-l2.a) < EPS) && (fabs(l1.b-l2.b) < EPS);
}
bool areIntersect(Line l1, Line l2, Point &p) {
if (areParallel(l1, l2)) return false; // no intersection
// solve system of 2 linear algebraic equations with 2 unknowns
p.x = (l2.b * l1.c - l1.b * l2.c) / (l2.a * l1.b - l1.a * l2.b);
// special case: test for vertical line to avoid division by zero
if (fabs(l1.b) > EPS) p.y = -(l1.a * p.x + l1.c);
else p.y = -(l2.a * p.x + l2.c);
return true;
}
bool between(Point A, Point B, Point P) {
return min(A.x, B.x) - EPS <= P.x && P.x <= max(A.x, B.x) + EPS
&& min(A.y, B.y) - EPS <= P.y && P.y <= max(A.y, B.y) + EPS;
}
void go(Point A, Point B, Line lA, Line lB, Point &X, Point &Y) {
if ((A.x > EPS || A.y > EPS) && (B.x > EPS || B.y > EPS)) {
Line l; pointsToLine(A, B, l);
Point P, Q;
if (areIntersect(l, lA, P) && areIntersect(l, lB, Q)) {
if (between(A, B, P) && between(A, B, Q)) {
if (P.len() < X.len()) X = P;
if (Q.len() < Y.len()) Y = Q;
}
}
}
}
long double calc() {
nPoint = 0;
FOR(i,1,n) {
points[++nPoint] = a[i].A;
points[++nPoint] = a[i].B;
points[++nPoint] = a[i].C;
points[++nPoint] = a[i].D;
}
sort(points+1, points+nPoint+1);
long double res = 0.0;
FOR(i,1,nPoint-1) {
Point A = points[i], B = points[i+1];
// cout << "Khe: " << A.x << ' ' << A.y << ' ' << B.x << ' ' << B.y << endl;
Line lA, lB;
pointsToLine(O, A, lA);
pointsToLine(O, B, lB);
Point X(1e5, 1e5), Y(1e5, 1e5);
FOR(r,1,n) {
Line l;
go(a[r].A, a[r].B, lA, lB, X, Y);
go(a[r].B, a[r].C, lA, lB, X, Y);
go(a[r].C, a[r].D, lA, lB, X, Y);
go(a[r].D, a[r].A, lA, lB, X, Y);
}
res += area(O, X, Y);
// DEBUG(res);
}
// cout << endl;
return res;
}
void xoay(Point &P, int turn) {
if (turn == 0) {
P.x = X - P.x;
} else {
P.y = Y - P.y;
}
}
void reverse(int turn) {
FOR(i,1,n) {
xoay(a[i].A, turn);
xoay(a[i].B, turn);
xoay(a[i].C, turn);
xoay(a[i].D, turn);
}
}
int main(int argc, char const *argv[]) {
int ntest; cin >> ntest;
cout << (fixed) << setprecision(6);
while (ntest--) {
cin >> X >> Y >> n;
FOR(i,1,n) {
long double x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
if (x1 > x2) swap(x1, x2);
if (y1 > y2) swap(y1, y2);
a[i].A = Point(x1, y1);
a[i].B = Point(x1, y2);
a[i].C = Point(x2, y2);
a[i].D = Point(x2, y1);
}
++n;
a[n].A = Point(0, 0);
a[n].B = Point(0, Y);
a[n].C = Point(X, Y);
a[n].D = Point(X, 0);
long double res3 = calc();
reverse(0);
long double res4 = calc();
reverse(1);
long double res2 = calc();
reverse(0);
long double res1 = calc();
cout << res1 << ' ' << res2 << endl << res3 << ' ' << res4 << endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8Y3N0cmluZz4KI2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDxjbWF0aD4KI2luY2x1ZGUgPHNldD4KI2luY2x1ZGUgPG1hcD4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGlvbWFuaXA+CgojZGVmaW5lIEZPUihpLGEsYikgZm9yKGludCBpPShhKSxfYj0oYik7IGkgPD0gX2I7ICsraSkKI2RlZmluZSBSRVAoaSxhKSBmb3IoaW50IGk9MCxfYT0oYSk7IGkgPCBfYTsgKytpKQoKI2RlZmluZSBERUJVRyh4KSB7IGNvdXQgPDwgI3ggPDwgIiA9ICIgPDwgeCA8PCBlbmRsOyB9CiNkZWZpbmUgUFIoYSxuKSB7IGNvdXQgPDwgI2EgPDwgIiA9ICI7IEZPUihfLDEsbikgY291dCA8PCBhW19dIDw8ICcgJzsgY291dCA8PCBlbmRsOyB9CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpjb25zdCBsb25nIGRvdWJsZSBFUFMgPSAxZS02OwoKc3RydWN0IFB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sIEIsIFEpKSB7CiAgICAgICAgICAgICAgICBpZiAoUC5sZW4oKSA8IFgubGVuKCkpIFggPSBQOwogICAgICAgICAgICAgICAgaWYgKFEubGVuKCkgPCBZLmxlbigpKSBZID0gUTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KfQoKbG9uZyBkb3VibGUgY2FsYygpIHsKICAgIG5Qb2ludCA9IDA7CiAgICBGT1IoaSwxLG4pIHsKICAgICAgICBwb2ludHNbKytuUG9pbnRdID0gYVtpXS5BOwogICAgICAgIHBvaW50c1srK25Qb2ludF0gPSBhW2ldLkI7CiAgICAgICAgcG9pbnRzWysrblBvaW50XSA9IGFbaV0uQzsKICAgICAgICBwb2ludHNbKytuUG9pbnRdID0gYVtpXS5EOwogICAgfQogICAgc29ydChwb2ludHMrMSwgcG9pbnRzK25Qb2ludCsxKTsKICAgIGxvbmcgZG91YmxlIHJlcyA9IDAuMDsKICAgIEZPUihpLDEsblBvaW50LTEpIHsKICAgICAgICBQb2ludCBBID0gcG9pbnRzW2ldLCBCID0gcG9pbnRzW2krMV07CgogICAgICAgIC8vIGNvdXQgPDwgIktoZTogIiA8PCBBLnggPDwgJyAnIDw8IEEueSA8PCAnICcgPDwgQi54IDw8ICcgJyA8PCBCLnkgPDwgZW5kbDsKICAgICAgICBMaW5lIGxBLCBsQjsKICAgICAgICBwb2ludHNUb0xpbmUoTywgQSwgbEEpOwogICAgICAgIHBvaW50c1RvTGluZShPLCBCLCBsQik7CgogICAgICAgIFBvaW50IFgoMWU1LCAxZTUpLCBZKDFlNSwgMWU1KTsKCiAgICAgICAgRk9SKHIsMSxuKSB7CiAgICAgICAgICAgIExpbmUgbDsKICAgICAgICAgICAgZ28oYVtyXS5BLCBhW3JdLkIsIGxBLCBsQiwgWCwgWSk7CiAgICAgICAgICAgIGdvKGFbcl0uQiwgYVtyXS5DLCBsQSwgbEIsIFgsIFkpOwogICAgICAgICAgICBnbyhhW3JdLkMsIGFbcl0uRCwgbEEsIGxCLCBYLCBZKTsKICAgICAgICAgICAgZ28oYVtyXS5ELCBhW3JdLkEsIGxBLCBsQiwgWCwgWSk7CiAgICAgICAgfQoKICAgICAgICByZXMgKz0gYXJlYShPLCBYLCBZKTsKICAgICAgICAvLyBERUJVRyhyZXMpOwogICAgfQogICAgLy8gY291dCA8PCBlbmRsOwogICAgcmV0dXJuIHJlczsKfQoKdm9pZCB4b2F5KFBvaW50ICZQLCBpbnQgdHVybikgewogICAgaWYgKHR1cm4gPT0gMCkgewogICAgICAgIFAueCA9IFggLSBQLng7CiAgICB9IGVsc2UgewogICAgICAgIFAueSA9IFkgLSBQLnk7CiAgICB9Cn0KCnZvaWQgcmV2ZXJzZShpbnQgdHVybikgewogICAgRk9SKGksMSxuKSB7CiAgICAgICAgeG9heShhW2ldLkEsIHR1cm4pOwogICAgICAgIHhvYXkoYVtpXS5CLCB0dXJuKTsKICAgICAgICB4b2F5KGFbaV0uQywgdHVybik7CiAgICAgICAgeG9heShhW2ldLkQsIHR1cm4pOwogICAgfQp9CgppbnQgbWFpbihpbnQgYXJnYywgY2hhciBjb25zdCAqYXJndltdKSB7CiAgICBpbnQgbnRlc3Q7IGNpbiA+PiBudGVzdDsKICAgIGNvdXQgPDwgKGZpeGVkKSA8PCBzZXRwcmVjaXNpb24oNik7CiAgICB3aGlsZSAobnRlc3QtLSkgewogICAgICAgIGNpbiA+PiBYID4+IFkgPj4gbjsKICAgICAgICBGT1IoaSwxLG4pIHsKICAgICAgICAgICAgbG9uZyBkb3VibGUgeDEsIHkxLCB4MiwgeTI7CiAgICAgICAgICAgIGNpbiA+PiB4MSA+PiB5MSA+PiB4MiA+PiB5MjsKCiAgICAgICAgICAgIGlmICh4MSA+IHgyKSBzd2FwKHgxLCB4Mik7CiAgICAgICAgICAgIGlmICh5MSA+IHkyKSBzd2FwKHkxLCB5Mik7CgogICAgICAgICAgICBhW2ldLkEgPSBQb2ludCh4MSwgeTEpOwogICAgICAgICAgICBhW2ldLkIgPSBQb2ludCh4MSwgeTIpOwogICAgICAgICAgICBhW2ldLkMgPSBQb2ludCh4MiwgeTIpOwogICAgICAgICAgICBhW2ldLkQgPSBQb2ludCh4MiwgeTEpOwogICAgICAgIH0KICAgICAgICArK247CiAgICAgICAgYVtuXS5BID0gUG9pbnQoMCwgMCk7CiAgICAgICAgYVtuXS5CID0gUG9pbnQoMCwgWSk7CiAgICAgICAgYVtuXS5DID0gUG9pbnQoWCwgWSk7CiAgICAgICAgYVtuXS5EID0gUG9pbnQoWCwgMCk7CgogICAgICAgIGxvbmcgZG91YmxlIHJlczMgPSBjYWxjKCk7CgogICAgICAgIHJldmVyc2UoMCk7CiAgICAgICAgbG9uZyBkb3VibGUgcmVzNCA9IGNhbGMoKTsKCiAgICAgICAgcmV2ZXJzZSgxKTsKICAgICAgICBsb25nIGRvdWJsZSByZXMyID0gY2FsYygpOwoKICAgICAgICByZXZlcnNlKDApOwogICAgICAgIGxvbmcgZG91YmxlIHJlczEgPSBjYWxjKCk7CgogICAgICAgIGNvdXQgPDwgcmVzMSA8PCAnICcgPDwgcmVzMiA8PCBlbmRsIDw8IHJlczMgPDwgJyAnIDw8IHJlczQgPDwgZW5kbDsKICAgIH0KICAgIHJldHVybiAwOwp9