#include <iostream>
#include <map>
using namespace std;
map<int, int, greater<int>> mp; //次数, 系数

int main(){
    int n, m;
    cin>>n;
    for(int i = 0; i < n; i ++){
        int a, b;
        cin>>a>>b;
        mp[b] = a;
    }
    cin>>m;
    for(int i = 0; i < m; i ++){
        int a, b;
        cin>>a>>b;
        if(mp.count(b) == 0) mp[b] = a;
        else mp[b] += a;
    }
    for(auto i : mp){
        if(i.second != 0) cout<<i.second<<' '<<i.first<<' ';
    }

}