#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <string>
#include <functional>
#include <map>

using namespace std;

typedef multimap<string, int>::iterator map_it;

int main()
{
    int q;
    cin >> q;

    multimap<string, int> S;
    int cmd, x;
    string key;
    while (q--)
    {
        cin >> cmd;
        if (cmd == 0)
        {
            cin >> key >> x;
            S.insert(pair<string, int>(key, x));
            // cout << S.size() << endl;
        }
        else if (cmd == 1)
        {
            cin >> key;
            // cout << (S.find(x) != S.end()) << endl;
            // pair<map_it, map_it> pr = S.equal_range(key);
            // map_it it = pr.first;
            // while (it != pr.second)
            // {
            //     cout << it->second << endl;
            //     it++;
            // }

            //法2
            map_it it = S.find(key);
            int i = S.count(key);
            while (i--)
            {
                cout << it->second << endl;
                it++;
            }

            
        }
        else if (cmd == 2)
        {
            cin >> key;
            S.erase(key);
            // remove(S.begin(), S.end(), x);
        }
        else
        {
            string L, R;
            cin >> L >> R;

            map<string ,int>::iterator it1 = S.lower_bound(L), it2 = S.upper_bound(R);
            while (it1 != it2)
            {
                cout << it1->first << ' ' << it1->second << endl;
                it1++;
            }

        }

    }
}
