#include<bits/stdc++.h>
using namespace std;
struct node{
	int s,r,t;
	int comval;
	bool operator < (const node &rhs) const{
		return comval < rhs.comval;
	}
};
priority_queue<node> Q[128];
node pat[102345];
int num,n;
char opt[12];
int main(){
	int T,cas;
	scanf("%d",&T);
	for(cas=1;cas<=T;cas++){
		printf("Case #%d:\n",cas);
		num=0;
		for(int i=0;i<=100;i++){
			Q[i] = priority_queue<node>();
		}
		scanf("%d",&n);	
		int s,r,t;
		node P;
		for(int i=1;i<=n;i++){
			scanf("%s",opt);
			if(opt[0]=='P'){
				scanf("%d%d%d",&t,&s,&r);	
				P.s=s,P.r=r,P.t=t;
				P.comval=s-r*t;
				Q[r].push(P);
				continue;
			}
			if(opt[0]=='A'){
				scanf("%d",&t);
				int maxx=-1,jl=-1,val;
				for(int i=100;i>=0;i--){
					if(Q[i].size()){
						P=Q[i].top();
						val=P.s+P.r*(t-P.t);
						if(val>maxx)
							maxx=val,jl=i;
					}
				}
				Q[jl].pop();
				printf("%d %d\n",maxx,jl);	
			}
		}
	}
	return 0;
}
