#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{
bestans = max(bestans, running_sum + firstround);
if(bestans == running_sum + firstround){
min_deletions = min(min_deletions, cur_deletions);
}
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++;
}
}
}
bestans = max(bestans, running_sum + firstround);
if(bestans == running_sum + firstround){
min_deletions = min(min_deletions, cur_deletions);
}
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+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgZmFzdGlvIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7Y2luLnRpZSgwKTtj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gICAgICB9CiAgICAgICAgZWxzZXsKICAgICAgICAgICAgYmVzdGFucyA9IG1heChiZXN0YW5zLCBydW5uaW5nX3N1bSArIGZpcnN0cm91bmQpOwogICAgICAgICAgICBpZihiZXN0YW5zID09IHJ1bm5pbmdfc3VtICsgZmlyc3Ryb3VuZCl7CiAgICAgICAgICAgICAgICBtaW5fZGVsZXRpb25zID0gbWluKG1pbl9kZWxldGlvbnMsIGN1cl9kZWxldGlvbnMpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGN1cl9kZWxldGlvbnMrKzsKICAgICAgICAgICAgZmlyc3Ryb3VuZCAtPSBlW2ldOwogICAgICAgICAgICB3aGlsZSghZ29vZC5lbXB0eSgpICYmIGdvb2QudG9wKCkuZmlyc3QgPiBmaXJzdHJvdW5kKXsKICAgICAgICAgICAgICAgIGZpcnN0cm91bmQgLT0gZVtnb29kLnRvcCgpLnNlY29uZF07CiAgICAgICAgICAgICAgICBydW5uaW5nX3N1bSAtPSBlW2dvb2QudG9wKCkuc2Vjb25kXTsKICAgICAgICAgICAgICAgIGdvb2QucG9wKCk7CiAgICAgICAgICAgICAgICBjdXJfZGVsZXRpb25zKys7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICBiZXN0YW5zID0gbWF4KGJlc3RhbnMsIHJ1bm5pbmdfc3VtICsgZmlyc3Ryb3VuZCk7CiAgICBpZihiZXN0YW5zID09IHJ1bm5pbmdfc3VtICsgZmlyc3Ryb3VuZCl7CiAgICAgICAgbWluX2RlbGV0aW9ucyA9IG1pbihtaW5fZGVsZXRpb25zLCBjdXJfZGVsZXRpb25zKTsKICAgIH0gICAgICAgIAogICAgaWYoZ29vZC5zaXplKCkgPiAxKXsKICAgICAgICByZXR1cm4ge24gLSBnb29kLnNpemUoKSwtMX07CiAgICB9CiAgICByZXR1cm4ge21pbl9kZWxldGlvbnMsYmVzdGFuc307Cn0KaW50IG1haW4oaW50IGFyZ2MsIGNoYXIgY29uc3QgKmFyZ3ZbX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