#include <bits/stdc++.h>

using namespace std;

// void get_from_key(multimap<string, int> m, string key) {
//   auto p = m.equal_range(key);
//   for (auto itr = p.first; itr != p.second; itr++) {
//     cout << itr->second << endl;
//   }
// }

int main() {
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  multimap<string, int> m;

  int cmd, val;
  string lhd, rhd;
  string key;

  for (int i = 0; i < n; i++) {
    cin >> cmd;

    switch (cmd) {
      case 0:
        cin >> key >> val;
        // m[key] = val;
        m.insert(make_pair(key, val));
        break;

      case 1: {
        cin >> key;
        // get_from_key(m, key);
        multimap<string, int>::iterator itr = m.lower_bound(key);
        multimap<string, int>::iterator end = m.upper_bound(key);

        for (; itr != end; itr++) {
          cout << itr->second << endl;
        }

        break;
      }

      case 2:
        cin >> key;
        m.erase(key);
        break;

      case 3: {
        cin >> lhd >> rhd;

        multimap<string, int>::iterator end = m.upper_bound(rhd);
        for (auto itr = m.lower_bound(lhd); itr != end; itr++) {
          cout << itr->first << " " << itr->second << endl;
        }
        break;
      }
    }
  }
}


