#include<iostream>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
 
int q;
 
int main(){
    multimap<string, int> mma;
    cin >> q;
    for(int i = 0; i < q; i++){
        char query;
        cin >> query;
        if(query == '0'){
            string key;
            int x;
            cin >> key >> x;
            mma.insert(make_pair(key, x));
        }else if(query == '1'){
            string key;
            cin >> key;
            multimap<string, int>::iterator itr;
            pair<multimap<string, int>::iterator, multimap<string, int>::iterator> itr1 = mma.equal_range(key);
            for(itr = itr1.first; itr != itr1.second; itr++){
                cout << itr -> second << endl;
            }
        }else if(query == '2'){
            string key;
            cin >> key;
            pair<multimap<string, int>::iterator, multimap<string, int>::iterator> itr1 = mma.equal_range(key);
            mma.erase(itr1.first, itr1.second);
        }else if(query == '3'){
            string L, R;
            cin >> L >> R;
            multimap<string, int>::iterator itr, itr1, itr2;
            itr1 = mma.lower_bound(L);
            itr2 = mma.upper_bound(R);
            for(itr = itr1; itr != itr2; itr++){
                cout << itr -> first << ' ' << itr -> second << endl;
            }
        }
    }
    return 0;
}
