#include <iostream>
#include <map>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
#define F first
#define S second
#define PB push_back
int main(){
    int q,k;
    map<string,vector<int>>m;
    map<string,vector<int>>::iterator u;
    string s;
    vector<int> emp;
    cin>>q;
    while(q--){
        cin>>k>>s;
        if(k==0){
            cin>>k;
            if(m.find(s)==m.end())m.insert({s,emp});
            m[s].PB(k);
        }
        else if(k==1){
            u=m.find(s);
            if(u!=m.end())for(int i:u->S)cout<<i<<endl;
        }
        else if(k==2)m.erase(s);
        else{
            u=m.lower_bound(s);
            cin>>s;
            while(u!=m.end()){
                if(u->F>s)break;
                for(int i:u->S)cout<<u->F<<' '<<i<<endl;
                u++;
            }
        }
    }
}
