#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

// #define DEBUG

int main()
{

#ifdef DEBUG
    cout << "DEBUG MODE" << endl;
    ifstream in("input.txt"); //for debug
    cin.rdbuf(in.rdbuf());    //for debug
#endif

    int n, k, x, y;
    string key, l, r;
    cin >> n;
    // map<string, int> workspace;
    multimap<string, int> workspace;

    for (int i = 0; i < n; i++)
    {
        cin >> k;
        if (k == 0)
        {
            cin >> key >> x;
            workspace.insert(make_pair(key, x));
        }
        else if (k == 1)
        {
            cin >> key;
            auto z = workspace.equal_range(key);
            for (auto itr = z.first; itr != z.second; itr++)
                cout << itr->second << "\n";
        }   
        else if (k == 2)
        {
            cin >> key;
            workspace.erase(key);
        }
        else if (k == 3)
        {
            cin >> l >> r;
            for (auto itr = workspace.lower_bound(l); itr != workspace.upper_bound(r); itr++)
            {
                auto itr2 = *itr;
                const string a = itr2.first;
                int b = itr2.second;
                cout << a << " " << b << "\n";
            }
        }
    }
    return 0;
}

// getline(cin,s);
// a = strtok(s, ",");  ","で分解
// for (int i = 0; i < n; i++)
