#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define wfile freopen("output.txt", "w", stdout)
#define rfile freopen("input.txt", "r", stdin)
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
void enable_io(){
#ifdef LOCAL_PROJECT
wfile;
rfile;
#endif
}
pair<int,int> solve(){
int n;
cin >> n;
std::vector<int> e(n,0);
std::vector<int> r(n,0);
ll firstround = 0;
ll bestans = -1;
for (int i = 0; i < n; ++i)
{
cin >> e[i] >> r[i];
firstround += e[i];
}
ll running_sum = 0;
ll min_deletions = 1e9;
ll cur_deletions = 0;
priority_queue<pair<int,int>> good;
for(int i = 0; i < n; i++){
if(e[i] + r[i] <= firstround){
running_sum += e[i];
good.push({e[i] + r[i],i});
}
else{
if(bestans == running_sum + firstround)
min_deletions = min(min_deletions, cur_deletions);
else if(bestans < running_sum + firstround)
min_deletions = cur_deletions;
bestans = max(bestans, running_sum + firstround);
cur_deletions++;
firstround -= e[i];
while(!good.empty() && good.top().first > firstround){
firstround -= e[good.top().second];
running_sum -= e[good.top().second];
good.pop();
cur_deletions++;
}
}
}
if(bestans == running_sum + firstround)
min_deletions = min(min_deletions, cur_deletions);
else if(bestans < running_sum + firstround)
min_deletions = cur_deletions;
bestans = max(bestans, running_sum + firstround);
if(good.size() > 1){
return {n - good.size(),-1};
}
return {min_deletions,bestans};
}
int main(int argc, char const *argv[])
{
fastio;
enable_io();
int T = 1;
cin >> T;
for(int tests = 1; tests <= T; tests++){
auto ans = solve();
string out = (ans.second == -1)? to_string(ans.first) + " INDEFINITELY": to_string(ans.first) + " " + to_string(ans.second);
cout << "Case #" << tests << ": " << out << endl;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgZmFzd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PSBlW2ldOwogICAgICAgICAgICBnb29kLnB1c2goe2VbaV0gKyByW2ldLGl9KTsKICAgICAgICB9CiAgICAgICAgZWxzZXsKICAgICAgICAgICAgaWYoYmVzdGFucyA9PSBydW5uaW5nX3N1bSArIGZpcnN0cm91bmQpCgkJCSAgICBtaW5fZGVsZXRpb25zID0gbWluKG1pbl9kZWxldGlvbnMsIGN1cl9kZWxldGlvbnMpOwoJCQllbHNlIGlmKGJlc3RhbnMgPCBydW5uaW5nX3N1bSArIGZpcnN0cm91bmQpCgkJCSAgICBtaW5fZGVsZXRpb25zID0gY3VyX2RlbGV0aW9uczsKCQkJYmVzdGFucyA9IG1heChiZXN0YW5zLCBydW5uaW5nX3N1bSArIGZpcnN0cm91bmQpOwogICAgICAgICAgICBjdXJfZGVsZXRpb25zKys7CiAgICAgICAgICAgIGZpcnN0cm91bmQgLT0gZVtpXTsKICAgICAgICAgICAgd2hpbGUoIWdvb2QuZW1wdHkoKSAmJiBnb29kLnRvcCgpLmZpcnN0ID4gZmlyc3Ryb3VuZCl7CiAgICAgICAgICAgICAgICBmaXJzdHJvdW5kIC09IGVbZ29vZC50b3AoKS5zZWNvbmRdOwogICAgICAgICAgICAgICAgcnVubmluZ19zdW0gLT0gZVtnb29kLnRvcCgpLnNlY29uZF07CiAgICAgICAgICAgICAgICBnb29kLnBvcCgpOwogICAgICAgICAgICAgICAgY3VyX2RlbGV0aW9ucysrOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgaWYoYmVzdGFucyA9PSBydW5uaW5nX3N1bSArIGZpcnN0cm91bmQpCgkgICAgbWluX2RlbGV0aW9ucyA9IG1pbihtaW5fZGVsZXRpb25zLCBjdXJfZGVsZXRpb25zKTsKCWVsc2UgaWYoYmVzdGFucyA8IHJ1bm5pbmdfc3VtICsgZmlyc3Ryb3VuZCkKCSAgICBtaW5fZGVsZXRpb25zID0gY3VyX2RlbGV0aW9uczsKCWJlc3RhbnMgPSBtYXgoYmVzdGFucywgcnVubm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