#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define speed ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define pb push_back
#define S second
#define F first
#define pii pair<int,int>
#define pll pair<ll,ll>
void solve(){
    ll n, m;
    cin>>n;
    vector<pair<string, ll>> vec(n);
    for(int i=0;i<n;i++){
        cin>>vec[i].first>>vec[i].second;
    }
cin>>m;
        vector<pair <ll, pair< string, ll>>> vec2(m);
            vector<pair<string, ll>> ans;
    for(int i=0; i<m;i++){
            cin>>vec2[i].F>>vec2[i].S.F>>vec2[i].S.S;}

        if(m==0){
        ll cnt=0;
        for( auto now: vec){cout<<now.first<<" "<<cnt<<endl;  cnt+=now.S;}
         return;}

    reverse(vec.begin(), vec.end());
//    for( auto now: vec){cout<<now.first<<" "<<now.second;}

    ll time=0;
    m--;
    reverse(vec2.begin(), vec2.end());
    while(vec.size()!=0){

        while(vec2[m].F<=time && vec2.size()!=0){
            vec.pb(vec2[m].S);
            vec2.pop_back();
        m--;
        }
        ans.pb({vec[vec.size()-1].F, time});
        time+=vec[vec.size()-1].S;
        vec.pop_back();
    }
    while(vec2.size()!=0&&m>=0){
            time=max(time, vec2[m].F);
        ans.pb({vec2[m].S.F, time});
       time+=vec2[m].S.S;
        vec2.pop_back();
        m--;
    }
     for( auto now: ans){cout<<now.first<<" "<<now.second<<"\n";}
}
int main(){
    speed;
    int tt=1;
    //cin>>tt;
    while(tt--){
        solve();
    }
    return 0;
}
