#include using namespace std; //* ATCODER #include using namespace atcoder; typedef modint998244353 mint; //*/ /* BOOST MULTIPRECISION #include using namespace boost::multiprecision; //*/ typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) template bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template T max(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]); return ret; } template T min(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]); return ret; } template T sum(vector &a){ T ret = 0; for (int i=0; i<(int)a.size(); i++) ret += a[i]; return ret; } // https://judge.yosupo.jp/submission/117923 struct UnionFind { vector par; vector siz; vector edg; UnionFind(int N) : par(N), siz(N), edg(N) { for(int i = 0; i < N; ++i){ par[i] = i; siz[i] = 1; edg[i] = 0; } } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry){ edg[rx]++; return; } par[rx] = ry; siz[ry] += siz[rx]; edg[ry] += edg[rx] + 1; } bool same(int x, int y){ int rx = root(x); int ry = root(y); return rx == ry; } long long size(int x){ return siz[root(x)]; } long long edge(int x){ return edg[root(x)]; } }; template vector> manhattanMST(vector x, vector y){ int n = x.size(); vector> edge; edge.reserve(4 * n); vector idx(n); iota(idx.begin(), idx.end(), 0); for(int s = 0; s < 2; ++s){ for(int t = 0; t < 2; ++t){ sort(idx.begin(), idx.end(), [&](const int i, const int j) { return x[i] + y[i] < x[j] + y[j]; }); map> map; for(const int i : idx){ for(auto iter = map.lower_bound(y[i]); iter != map.end(); iter = map.erase(iter)){ const int j = iter->second; const T dx = x[i] - x[j]; const T dy = y[i] - y[j]; if(dy > dx) break; edge.emplace_back(dx + dy, i, j); } map[y[i]] = i; } swap(x, y); } for(int i = 0; i < n; ++i) { x[i] *= -1; } } sort(edge.begin(), edge.end()); UnionFind dsu(n); vector> used; used.reserve(n - 1); for(const auto& [c, i, j] : edge){ if(!dsu.same(i, j)){ used.emplace_back(i, j); dsu.unite(i, j); } } return used; } // ------- void solve(){ int n; ll l; cin >> n >> l; vector x(n), y(n); rep(i,0,n){ cin >> x[i] >> y[i]; } vector> mst = manhattanMST(x, y); vector ikeru(n, vector(0)); for (auto [i, j]: mst){ ikeru[i].push_back(j); ikeru[j].push_back(i); } vector ans; auto dfs = [&](auto self, int i, int p) -> void { ans.push_back(i); for (int j: ikeru[i]){ if (j == p) continue; self(self, j, i); ans.push_back(i); } }; dfs(dfs,0,-1); cout << (int)ans.size() << '\n'; rep(i,0,(int)ans.size()){ cout << x[ans[i]] << ' ' << y[ans[i]] << '\n'; } } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while(t--) solve(); }