#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<set>
using namespace std;
int x,now;
struct data {int id,t;};
struct res {int id,t;} ans[300005];
int t1[300005],t2[300005],rank[300005];
priority_queue<data,vector<data> >q;
bool operator<(data a,data b)
{
if (rank[a.id]==rank[b.id]) return t1[a.id]>t1[b.id];
return rank[a.id]<rank[b.id];
}
bool operator<(res a,res b)
{
return a.t<b.t;
}
int main()
{
while (scanf("%d",&x)!=EOF)
{
scanf("%d%d%d",&t1[x],&t2[x],&rank[x]);
ans[x].id=x;
}
t1[x+1]=1000000007;
for (int i=1;i<=x+1;i++)
{
int t=t1[i]-now;
while (!q.empty()&&q.top().t<=t)
{
now+=q.top().t;
ans[q.top().id].t=now;
t-=q.top().t;
q.pop();
}
if (i==x+1) break;
if (!q.empty()&&t)
{
data x=q.top();
q.pop();
x.t-=t;
q.push(x);
}
now=t1[i];
q.push((data){i,t2[i]});
}
sort(ans+1,ans+x+1);
for (int i=1;i<=x;i++)
printf("%d %d\n",ans[i].id,ans[i].t);
return 0;
}