//To debug : g++ -g file.cpp -o code
//to flush output : fflush(stdout) or cout.flush()
//cout<<setprecision(p)<<fixed<<var
//use 1LL<<i to for 64 bit shifting , (ll)2 because by default 2 is ll
//take care of precedence rule of operators
//do not forget to change the sizes of arrays and value of contants and other things after debugging
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,n) for(i=a;i<n;++i)
#define irep(i,n,a) for(i=n;i>a;--i)
#define mod 1000000007
#define pb push_back
#define big 9223372036854775807
#define big1 LONG_MAX
#define big2 ll_MAX
#define big3 1000000000
#define sma1 LONG_MIN
#define sma2 ll_MIN
#define sma3 -1000000000
#define mp make_pair
#define dub double
#define ivec vector<ll>
#define lvec vector<long long>
#define cvec vector<char>
#define svec vector<string>
#define mt make_tuple
#define MOD 998244353
#define ld long double
#define pi acos(-1.0)
#define SZ(x) (ll)(x.size())
//comment the below if not required
/*
#define ss second.second
#define ff first.first
#define f first
#define s second
#define sf second.first
#define fs first.second
*/
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
//cout<<"Case #"<<c<<": "<<ans<<"\n" ;
int main()
{
IOS;
int t,C=0,n,i,j,pt,ft,r,c,cur,rt;
ll tot,val,ans;
cin>>t;
while(t--)
{
++C;
cout<<"Case #"<<C<<": ";
cin>>n;
pair<int,int> p[n+1];
tot = 0;
r = 0;
multiset<pair<int,int>> tft ; //fotget time (ft) and toy number
rep(i,1,n+1)
{
cin>>pt>>ft;
p[i]={pt,ft};
tot += (ll)pt ; //total playing time
tft.insert({-ft,i});
}
ans = tot;
bool m[n+1]={0}; //to mark if toy has been removed
rt = 0;
val = 0;
for(i=1;i<=n;++i)
{
if(!m[i])
{
//if this toy not removed , tot-pt
if(p[i].second>(tot-(ll)p[i].first))
{
//remove this toy and all other toys
//whose ft > tot-p[i].first-p[other toy playing time]
tot -= (ll)p[i].first;
vector<int> rem;
m[i]=1;
rem.pb(i);
++rt;
for(auto u : tft)
{
int ind = u.second;
if(!m[ind])
{
int temp = -u.first;
m[ind]=1;
if(temp>(tot-(ll)p[ind].first))
{
tot -= (ll)p[ind].first;
rem.pb(ind);
++rt;
if(ind<i)
{
val -= (ll)p[ind].first;
}
}
else
break;
}
}
for(auto ind : rem)
{
auto v = tft.find({-p[ind].second,ind});
tft.erase(v);
}
}
else
{
val += (ll)p[i].first;
if((tot+val)>ans)
{
ans = tot + val;
r = rt;
}
}
}
}
if(SZ(tft))
cout<<rt<<" "<<"INDEFINITELY\n";
else
cout<<r<<" "<<ans<<"\n";
}
return 0;
}
Ly9UbyBkZWJ1ZyA6ICBnKysgLWcgZmlsZS5jcHAgLW8gY29kZQovL3RvIGZsdXNoIG91dHB1dCA6IGZmbHVzaChzdGRvdXQpIG9yIGNvdXQuZmx1c2goKQovL2NvdXQ8PHNldHByZWNpc2lvbihwKTw8Zml4ZWQ8PHZhcgovL3VzZSAxTEw8PGkgdG8gZm9yIDY0IGJpdCBzaGlmdGluZyAsIChsbCkyIGJlY2F1c2UgYnkgZGVmYXVsdCAyIGlzIGxsCi8vdGFrZSBjYXJlIG9mIHByZWNlZGVuY2UgcnVsZSBvZiBvcGVyYXRvcnMgCi8vZG8gbm90IGZvcmdldCB0byBjaGFuZ2UgdGhlIHNpemVzIG9mIGFycmF5cyBhbmQgdmFsdWUgb2YgY29udGFudHMgYW5kIG90aGVyIHRoaW5ncyBhZnRlciBkZWJ1Z2dpbmcgIAogCiNpbmNsdWRlPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgcmVwKGksYSxuKSBmb3IoaT1hO2k8bjsrK2kpCiNkZWZpbmUgaXJlcChpLG4sYSkgZm9yKGk9bjtpPmE7LS1pKQojZGVmaW5lIG1vZCAxMDAwMDAwMDA3CiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgYmlnIDkyMjMzNzIwMzY4NTQ3NzU4MDcKI2RlZmluZSBiaWcxIExPTkdfTU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gICArK0M7CiAgICAgICAgY291dDw8IkNhc2UgIyI8PEM8PCI6ICI7CgogICAgICAgIGNpbj4+bjsKCiAgICAgICAgcGFpcjxpbnQsaW50PiBwW24rMV07CgogICAgICAgIHRvdCA9IDA7CiAgICAgICAgciA9IDA7CgogICAgICAgIG11bHRpc2V0PHBhaXI8aW50LGludD4+IHRmdCA7IC8vZm90Z2V0IHRpbWUgKGZ0KSBhbmQgdG95IG51bWJlcgoKICAgICAgICByZXAoaSwxLG4rMSkKICAgICAgICB7CiAgICAgICAgICAgIGNpbj4+cHQ+PmZ0OwogICAgICAgICAgICBwW2ldPXtwdCxmdH07CiAgICAgICAgICAgIHRvdCArPSAobGwpcHQgOyAvL3RvdGFsIHBsYXlpbmcgdGltZQogICAgICAgICAgICB0ZnQuaW5zZXJ0KHstZnQsaX0pOwogICAgICAgIH0KCiAgICAgICAgYW5zID0gdG90OwoKICAgICAgICBib29sIG1bbisxXT17MH07IC8vdG8gbWFyayBpZiB0b3kgaGFzIGJlZW4gcmVtb3ZlZAoKICAgICAgICBydCA9IDA7CiAgICAgICAgdmFsID0gMDsKCiAgICAgICAgZm9yKGk9MTtpPD1uOysraSkKICAgICAgICB7CgoKICAgICAgICAgICAgaWYoIW1baV0pCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIC8vaWYgdGhpcyB0b3kgbm90IHJlbW92ZWQgLCB0b3QtcHQKCiAgICAgICAgICAgICAgICBpZihwW2ldLnNlY29uZD4odG90LShsbClwW2ldLmZpcnN0KSkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAvL3JlbW92ZSB0aGlzIHRveSBhbmQgYWxsIG90aGVyIHRveXMKICAgICAgICAgICAgICAgICAgICAvL3dob3NlIGZ0ID4gdG90LXBbaV0uZmlyc3QtcFtvdGhlciB0b3kgcGxheWluZyB0aW1lXQoKICAgICAgICAgICAgICAgICAgICB0b3QgLT0gKGxsKXBbaV0uZmlyc3Q7CgogICAgICAgICAgICAgICAgICAgIHZlY3RvcjxpbnQ+IHJlbTsKCiAgICAgICAgICAgICAgICAgICAgbVtpXT0xOwogICAgICAgICAgICAgICAgICAgIHJlbS5wYihpKTsKICAgICAgICAgICAgICAgICAgICArK3J0OwoKCiAgICAgICAgICAgICAgICAgICAgZm9yKGF1dG8gdSA6IHRmdCkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIGludCBpbmQgPSB1LnNlY29uZDsKCiAgICAgICAgICAgICAgICAgICAgICAgIGlmKCFtW2luZF0pCiAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGludCB0ZW1wID0gLXUuZmlyc3Q7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBtW2luZF09MTsKCiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZih0ZW1wPih0b3QtKGxsKXBbaW5kXS5maXJzdCkpCiAgICAgICAgICAgICAgICAgICAgICAgICAgICB7ICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgdG90IC09IChsbClwW2luZF0uZmlyc3Q7CgogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHJlbS5wYihpbmQpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICsrcnQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYoaW5kPGkpCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB2YWwgLT0gKGxsKXBbaW5kXS5maXJzdDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICAgICAgICAgIGZvcihhdXRvIGluZCA6IHJlbSkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIGF1dG8gdiA9IHRmdC5maW5kKHstcFtpbmRdLnNlY29uZCxpbmR9KTsKICAgICAgICAgICAgICAgICAgICAgICAgdGZ0LmVyYXNlKHYpOwogICAgICAgICAgICAgICAgICAgIH0KCgogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIHZhbCArPSAobGwpcFtpXS5maXJzdDsKCiAgICAgICAgICAgICAgICAgICAgaWYoKHRvdCt2YWwpPmFucykKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIGFucyA9IHRvdCArIHZhbDsKICAgICAgICAgICAgICAgICAgICAgICAgciA9IHJ0OwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KCgogICAgICAgICAgICB9CiAgICAgICAgICAgIAoKICAgICAgICB9CgoKICAgICAgICBpZihTWih0ZnQpKQogICAgICAgICAgICAgICAgY291dDw8cnQ8PCIgIjw8IklOREVGSU5JVEVMWVxuIjsKICAgICAgICBlbHNlCiAgICAgICAgICAgIGNvdXQ8PHI8PCIgIjw8YW5zPDwiXG4iOwoKCgogICAgfQoKCgogICAgcmV0dXJuIDA7Cn0=