#pragma GCC optimize("O2") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define int ll #define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1) #define INT128_MIN (-INT128_MAX - 1) #define clock chrono::steady_clock::now().time_since_epoch().count() #ifdef DEBUG #define dbg(x) cout << (#x) << " = " << x << '\n' #else #define dbg(x) #endif namespace R = std::ranges; namespace V = std::views; using namespace std; using ll = long long; using ull = unsigned long long; using ldb = long double; using pii = pair; using pll = pair; //#define double ldb template ostream& operator<<(ostream& os, const pair pr) { return os << pr.first << ' ' << pr.second; } template ostream& operator<<(ostream& os, const array &arr) { for(const T &X : arr) os << X << ' '; return os; } template ostream& operator<<(ostream& os, const vector &vec) { for(const T &X : vec) os << X << ' '; return os; } template ostream& operator<<(ostream& os, const set &s) { for(const T &x : s) os << x << ' '; return os; } using lld = int64_t; class DSU { vector v; public: DSU(int n): v(n) { iota(v.begin(), v.end(), 0); } int query(int u) { return v[u] == u ? u : v[u] = query(v[u]); } void merge(int x, int y) { v[query(x)] = query(y); } }; template struct Point { T x, y; Point(T x_ = 0, T y_ = 0): x(x_), y(y_) {} Point operator-(const Point &rhs) const { return Point(x - rhs.x, y - rhs.y); } }; typedef Point P; vector> manhattanMST(vector

ps) { vector id(ps.size()); iota(id.begin(), id.end(), 0); vector> edges; for (int k = 0; k < 4; ++k) { sort(id.begin(), id.end(), [&](int i, int j) { return (ps[i] - ps[j]).x < (ps[j] - ps[i]).y; }); map sweep; for (int i : id) { for (auto it = sweep.lower_bound(-ps[i].y); it != sweep.end(); sweep.erase(it++)) { int j = it->second; P d = ps[i] - ps[j]; if (d.y > d.x) break; edges.emplace_back(d.y + d.x, i, j); } sweep[-ps[i].y] = i; } for (P &p : ps) if (k & 1) p.x = -p.x; else swap(p.x, p.y); } return edges; } vector MST(int n, const vector> &e) { vector idx(e.size()); iota(idx.begin(), idx.end(), 0); sort(idx.begin(), idx.end(), [&](int i, int j){ return get<0>(e[i]) < get<0>(e[j]); }); vector r; DSU dsu(n); for (int o: idx) { const auto &[w, i, j] = e[o]; if (dsu.query(i) == dsu.query(j)) continue; r.push_back(o); dsu.merge(i, j); } return r; } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while(t--) { int n, l; cin >> n >> l; vector

ps(n); for(auto &[x, y] : ps) cin >> x >> y; auto e = manhattanMST(ps); auto r = MST(n, e); vector> g(n); for(auto i : r) { auto [_, u, v] = e[i]; g[u].emplace_back(v); g[v].emplace_back(u); } vector

ans; auto dfs = [&](int v, int p, auto self) -> void { ans.emplace_back(ps[v]); for(int x : g[v]) { if (x == p) continue; self(x, v, self); ans.emplace_back(ps[v]); } }; dfs(0, -1, dfs); cout << ssize(ans) << '\n'; for(auto [x, y] : ans) cout << x << ' ' << y << '\n'; } return 0; }