#include "bits/stdc++.h"
using namespace std;
template <class X> void printKickStartTemplate(int testCaseNumber, X value) {
cout << "Case #" << testCaseNumber << ": " << value << endl;
}
const int N = 100 + 2;
int t;
int n;
pair<int, int> data[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> t;
for (int T = 1; T <= t; T++) {
cin >> n;
long long totalSum = 0;
long long currMaxSum = 0;
long long currRem = 0;
long long maxFound = 0;
long long maxCurrRem = 0;
for (int i = 1; i <= n; i++) {
cin >> data[i].first >> data[i].second;
totalSum += data[i].first;
}
currMaxSum = totalSum;
maxFound = totalSum;
priority_queue<pair<long long, long long>, vector<pair<long long, long long> >, greater<pair<long long, long long > > > pq;
long long removedTillNow = 0;
for (int i = 1; i <= n; i++) {
if (totalSum - removedTillNow - data[i].first < data[i].second) {
//needs to remove;
removedTillNow += data[i].first;
currRem ++;
currMaxSum -= data[i].first;
if (currMaxSum > maxFound) {
maxFound = max(maxFound, currMaxSum);
maxCurrRem = currRem;
}
while (!pq.empty()) {
int u = pq.top().first;
int v = pq.top().second;
if (u > totalSum - data[v].first - removedTillNow) {
//isko bhi remove karo
pq.pop();
removedTillNow += u;
currRem++;
currMaxSum -= 2 * u;
if (currMaxSum > maxFound) {
maxFound = max(maxFound, currMaxSum);
maxCurrRem = currRem;
}
} else break;
}
} else {
pq.push({data[i].second, i});
currMaxSum += data[i].first;
if (currMaxSum > maxFound) {
maxFound = max(maxFound, currMaxSum);
maxCurrRem = currRem;
}
}
}
if (pq.size() > 1) {
cout << "Case #" << T << ": " << currRem << " " << "INDEFINITELY" << endl;
} else {
cout << "Case #" << T << ": " << maxCurrRem << " " << maxFound << endl;
}
}
}
I2luY2x1ZGUgImJpdHMvc3RkYysrLmgiCnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0ZW1wbGF0ZSA8Y2xhc3MgWD4gIHZvaWQgcHJpbnRLaWNrU3RhcnRUZW1wbGF0ZShpbnQgdGVzdENhc2VOdW1iZXIsIFggdmFsdWUpIH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rKykgewogICAgICAgICAgICBpZiAodG90YWxTdW0gLSByZW1vdmVkVGlsbE5vdyAtIGRhdGFbaV0uZmlyc3QgPCBkYXRhW2ldLnNlY29uZCkgewogICAgICAgICAgICAgICAgLy9uZWVkcyB0byByZW1vdmU7CgogICAgICAgICAgICAgICAgcmVtb3ZlZFRpbGxOb3cgKz0gZGF0YVtpXS5maXJzdDsKICAgICAgICAgICAgICAgIGN1cnJSZW0gKys7CiAgICAgICAgICAgICAgICBjdXJyTWF4U3VtIC09IGRhdGFbaV0uZmlyc3Q7CgogICAgICAgICAgICAgICAgaWYgKGN1cnJNYXhTdW0gPiBtYXhGb3VuZCkgewogICAgICAgICAgICAgICAgICAgIG1heEZvdW5kID0gbWF4KG1heEZvdW5kLCBjdXJyTWF4U3VtKTsKICAgICAgICAgICAgICAgICAgICBtYXhDdXJyUmVtID0gY3VyclJlbTsKICAgICAgICAgICAgICAgIH0KCiAgICAgICAgICAgICAgICB3aGlsZSAoIXBxLmVtcHR5KCkpIHsKICAgICAgICAgICAgICAgICAgICBpbnQgdSA9IHBxLnRvcCgpLmZpcnN0OwogICAgICAgICAgICAgICAgICAgIGludCB2ID0gcHEudG9wKCkuc2Vjb25kOwogICAgICAgICAgICAgICAgICAgIGlmICh1ID4gdG90YWxTdW0gLSBkYXRhW3ZdLmZpcnN0IC0gcmVtb3ZlZFRpbGxOb3cpIHsKICAgICAgICAgICAgICAgICAgICAgICAgLy9pc2tvIGJoaSByZW1vdmUga2FybwogICAgICAgICAgICAgICAgICAgICAgICBwcS5wb3AoKTsKICAgICAgICAgICAgICAgICAgICAgICAgcmVtb3ZlZFRpbGxOb3cgKz0gdTsKICAgICAgICAgICAgICAgICAgICAgICAgY3VyclJlbSsrOwogICAgICAgICAgICAgICAgICAgICAgICBjdXJyTWF4U3VtIC09IDIgKiB1OwogICAgICAgICAgICAgICAgICAgICAgICBpZiAoY3Vyck1heFN1bSA+IG1heEZvdW5kKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBtYXhGb3VuZCA9IG1heChtYXhGb3VuZCwgY3Vyck1heFN1bSk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBtYXhDdXJyUmVtID0gY3VyclJlbTsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIH0gZWxzZSBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIHBxLnB1c2goe2RhdGFbaV0uc2Vjb25kLCBpfSk7CiAgICAgICAgICAgICAgICBjdXJyTWF4U3VtICs9IGRhdGFbaV0uZmlyc3Q7CiAgICAgICAgICAgICAgICBpZiAoY3Vyck1heFN1bSA+IG1heEZvdW5kKSB7CiAgICAgICAgICAgICAgICAgICAgbWF4Rm91bmQgPSBtYXgobWF4Rm91bmQsIGN1cnJNYXhTdW0pOwogICAgICAgICAgICAgICAgICAgIG1heEN1cnJSZW0gPSBjdXJyUmVtOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChwcS5zaXplKCkgPiAxKSB7CiAgICAgICAgICAgIGNvdXQgPDwgIkNhc2UgIyIgPDwgVCA8PCAiOiAiIDw8IGN1cnJSZW0gPDwgIiAiIDw8ICJJTkRFRklOSVRFTFkiIDw8IGVuZGw7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgY291dCA8PCAiQ2FzZSAjIiA8PCBUIDw8ICI6ICIgPDwgbWF4Q3VyclJlbSA8PCAiICIgPDwgbWF4Rm91bmQgPDwgZW5kbDsKICAgICAgICB9CiAgICB9Cn0=