/* Problem URL: https://codeforces.com/gym/100952/problem/J */ #include #include #include using namespace std; using namespace __gnu_pbds; template > using ordered_set = tree; #define V vector #define rmin(a, b) a = min(a, b) #define rmax(a, b) a = max(a, b) #define rep(i, lim) for (int i = 0; i < (lim); i++) #define nrep(i, s, lim) for (int i = s; i < (lim); i++) #define repv(i, v) for (auto &i : (v)) #define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } #define sortv(v) sort(v.begin(), v.end()) #define all(v) (v).begin(), (v).end() using vi = vector; using vvi = vector; using vvvi = vector; using vvvvi = vector; using ll = long long; using vl = vector; using vvl = vector; using vvvl = vector; using vvvvl = vector; template auto operator<<(ostream &os, const vector &vec)->ostream& { os << vec[0]; for (size_t i = 1; i < vec.size(); i++) { os << ' ' << vec[i]; } os << '\n'; return os; } template auto operator>>(istream &is, vector &vec)->istream& { for (auto &i : vec) { is >> i; } return is; } template auto operator<<(ostream &os, const vector> &vec)->ostream& { for (auto &i : vec) { os << i[0]; for (size_t j = 1; j < i.size(); j++) { os << ' ' << i[j]; } os << '\n'; } return os; } template auto operator>>(istream &is, vector> &vec)->istream& { for (auto &i : vec) { for (auto &j : i) { is >> j; } } return is; } const int oo = INT32_MAX >> 1; const ll OO = INT64_MAX >> 1; using ld = long double; struct pt { ld x, y; pt() = default; pt(ld x, ld y): x(x), y(y) {} friend istream &operator >> (istream &is, pt &a) { is >> a.x >> a.y; return is; } ld operator ^ (pt b) { return x * b.y - y * b.x; } pt operator - (pt b) { return pt(x - b.x, y - b.y); } }; #define eq(x, y) (abs(x - y) < EPS) ll sarea2(pt a, pt b, pt c) { return ((b - a) ^ (c - b)); } int ccw(pt a, pt b, pt c) { ld r = sarea2(a, b, c); return (r > 0) - (r < 0); } struct line { pt a; pt b; line() = default; line(pt a, pt b): a(a), b(b) {} }; bool lineinter(line a, line b) { return ccw(a.a, a.b, b.a) != ccw(a.a, a.b, b.b) && ccw(b.a, b.b, a.a) != ccw(b.a, b.b, a.b); } pt linept(line a, line b) { ld l1[] = {a.b.y - a.a.y, a.a.x - a.b.x, a.a ^ a.b}; ld l2[] = {b.b.y - b.a.y, b.a.x - b.b.x, b.a ^ b.b}; ld det = l1[0] * l2[1] - l1[1] * l2[0]; return pt((l2[1] * l1[2] - l1[1] * l2[2]) / det, (l2[0] * l1[2] - l1[0] * l2[2]) / det); } void pre() { } #define TEST 1 void solve() { int n, m; cin >> n >> m; V a(n); V b(m); cin >> a >> b; V act[2]; int now = 0; rep(i, n) { pt p1 = a[i]; pt p2 = a[(i + 1) % n]; auto inter = [&](line a) { rep(j, m) { pt p3 = b[j]; pt p4 = b[(j + 1) % m]; if (!lineinter(a, line(p3, p4))) { continue; } act[0].emplace_back(linept(a, line(p3, p4))); act[1].emplace_back(linept(a, line(p3, p4))); return true; } return false; }; if (inter(line(p1, p2))) { now ^= 1; continue; } act[now].emplace_back(p1); } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); pre(); int t; (TEST && cin >> t) || (t = 1); while (t--) { solve(); } }