#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define r(i, n) for(int i=0;i<n;i++)

int main() {
    multimap<string, int> MAP;
    multimap<string, int>::iterator b, e;
    int num_query;
    cin >> num_query;
    int command, x;
    for (int loop = 0; loop < num_query; loop++) {
        string tmp;
        cin >> command;
        if (command == 0) {
            cin >> tmp >> x;
            MAP.insert(make_pair(tmp,x));
        } else if (command == 1) {
            cin >> tmp;
            multimap <string,int>::iterator i;
            //for (i = MAP.equal_range(tmp).first;i!=MAP.equal_range(tmp).second;i++)cout<<(*i).second<<endl;
            for (i = MAP.lower_bound(tmp);i!=MAP.upper_bound(tmp);i++)cout<<(*i).second<<endl;
        } else if (command == 2) {
            cin >> tmp;
            MAP.erase(tmp);
        } else {
            string l, r;
            cin >> l >> r;
            b=MAP.lower_bound(l);
            e=MAP.upper_bound(r);
            for(;b!=e;b++) {
                cout << b->first << " " << b->second << endl;
            }
        }
    }
    return 0;
}
