#include<bits/stdc++.h>
using namespace std;
using ll = long long;

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")

int main(){
    ll Q;
    cin >> Q;

    multimap<string, ll> mp;

    while(Q--){
        ll a;
        cin >> a;
        string key;

        if(a==0){
            cin >> key;
            // insert
            ll x; cin >> x;
            mp.insert(make_pair(key, x));
        }
        else if(a==1){
            cin >> key;
            auto it = mp.lower_bound(key);
            auto end = mp.upper_bound(key);
            for(; it!=end; it++){
                p((*it).second);
            }
        }
        else if(a==2){
            cin >> key;
            // delete
            mp.erase(key);
        }
        else{
            // dump
            string L, R;
            cin >> L >> R;

            auto it = mp.lower_bound(L);
            auto end = mp.upper_bound(R);
            for(; it!=end; it++){
                p2((*it).first, (*it).second);
            }
        }
    }

    return 0;
}
