inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define fr first #define sc second
#define N 100007
int testcase;
pair<int, int> a[N];
inlinevoidwork(){ printf("Case %d: ", ++testcase); int n = rd(), m = rd(); for (int i = 1; i <= n; ++i) a[i].fr = rd(); for (int i = 1; i <= n; ++i) a[i].sc = rd(); sort(a + 1, a + 1 + n); for (int i = 1; i <= n; ++i) { if (a[i].sc > m) {printf("%d\n", i - 1); return;} m -= a[i].sc; } printf("%d\n", n); }
intmain(){ for (int t = rd(); t; --t) work(); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define fr first #define sc second #define pb push_back #define mp make_pair #define pii pair<int, int>
#define N 200007
int testcase, col[N], a[N], b[N];
vector<int> e[N], s[3];
booldfs(int u){ s[col[u]].pb(u); for (auto v : e[u]) if (!col[v]) { col[v] = 3 - col[u]; if (dfs(v)) returntrue; } elseif (col[u] == col[v]) returntrue; returnfalse; }
int l1[N], r1[N], l2[N], r2[N], tot;
priority_queue<pii, vector<pii>, greater<pii>> q;
inlinevoidwork(){ tot = 0; while(!q.empty()) q.pop(); printf("Case %d: ", ++testcase); int n = rd(), m = rd(); for (int i = 1; i <= n; ++i) {e[i].clear(); col[i] = 0;} for (int i = 1; i <= m; ++i) { int a = rd(), b = rd(); e[a].pb(b); e[b].pb(a); } for (int i = 1; i <= n; ++i) {a[i] = rd(); b[i] = rd();} int nwr = 0; for (int i = 1; i <= n; ++i) if (!col[i]) { s[1].clear(); s[2].clear(); col[i] = 1; if (dfs(i)) {puts("IMPOSSIBLE"); return;} ++tot; l1[tot] = l2[tot] = 2e9; r1[tot] = r2[tot] = 0; for (auto u : s[1]) { l1[tot] = min(l1[tot], a[u]); l2[tot] = min(l2[tot], b[u]); r1[tot] = max(r1[tot], a[u]); r2[tot] = max(r2[tot], b[u]); } for (auto u : s[2]) { l1[tot] = min(l1[tot], b[u]); l2[tot] = min(l2[tot], a[u]); r1[tot] = max(r1[tot], b[u]); r2[tot] = max(r2[tot], a[u]); } if (r1[tot] > r2[tot]) { swap(r1[tot], r2[tot]); swap(l1[tot], l2[tot]); } nwr = max(nwr, r1[tot]); q.push(mp(l1[tot], tot)); } int ans = 2e9; while (!q.empty()) { auto [nwl, p] = q.top(); q.pop(); ans = min(ans, nwr - nwl); if (l2[p] == nwl) break; q.push(mp(l2[p], p)); nwr = max(nwr, r2[p]); } printf("%d\n", ans); }
intmain(){ for (int t = rd(); t; --t) work(); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define fr first #define sc second #define pb push_back #define all(x) x.begin(), (x).end()
#define N 100007
int testcase, x[N], y[N], cntx[N], cnty[N];
vector<int> X, Y;
inlinevoidwork(){ printf("Case %d: ", ++testcase); X.clear(); Y.clear(); int n = rd(); for (int i = 1; i <= n; ++i) { cntx[i] = cnty[i] = 0; x[i] = rd(); X.pb(x[i]); y[i] = rd(); Y.pb(y[i]); } sort(all(X)); X.erase(unique(all(X)), X.end()); sort(all(Y)); Y.erase(unique(all(Y)), Y.end()); auto getx = [&](int w) {return lower_bound(all(X), w) - X.begin() + 1;}; auto gety = [&](int w) {return lower_bound(all(Y), w) - Y.begin() + 1;}; int mxx = 0, mxy = 0; for (int i = 1; i <= n; ++i) { x[i] = getx(x[i]); y[i] = gety(y[i]); ++cntx[x[i]]; mxx = max(mxx, cntx[x[i]]); ++cnty[y[i]]; mxy = max(mxy, cnty[y[i]]); } int cntmxx = 0, cntmxy = 0, cntnxx = 0, cntnxy = 0; for (int i = 1; i <= n; ++i) { if (cntx[i] == mxx) ++cntmxx; elseif (cntx[i] == mxx - 1) ++cntnxx; if (cnty[i] == mxy) ++cntmxy; elseif (cnty[i] == mxy - 1) ++cntnxy; } int tot = 0, ans = mxx + mxy, totnx = 0; for (int i = 1; i <= n; ++i) { if (cntx[x[i]] + cnty[y[i]] == ans) ++tot; elseif (cntx[x[i]] + cnty[y[i]] == ans - 1) ++totnx; } bool fl = true; if (tot == 1ll * cntmxx * cntmxy) {--ans; fl = false;} if (ans == 1) {puts("1 1"); return;} if (ans == 2) {printf("%d %lld\n", 2, 1ll * n * (n - 1) / 2); return;} if (fl) printf("%d %lld\n", ans, 1ll * cntmxx * cntmxy - tot); elseprintf("%d %lld\n", ans, tot + 1ll * cntmxx * cntnxy + 1ll * cntmxy * cntnxx - totnx); }
intmain(){ for (int t = rd(); t; --t) work(); return0; }