#include <bits/stdc++.h>
using namespace std;
int main() {
  int q, k;
  map<string, vector<int>> m;
  map<string, vector<int>>::iterator u;
  string s;
  vector<int> emp;
  cin >> q;
  while (q--) {
    cin >> k >> s;
    if (k == 0) {
      cin >> k;
      if (m.find(s) == m.end()) m.insert({s, emp});
      m[s].push_back(k);
    } else if (k == 1) {
      u = m.find(s);
      if (u != m.end())
        for (int i : u->second) cout << i << endl;
    } else if (k == 2)
      m.erase(s);
    else {
      u = m.lower_bound(s);
      cin >> s;
      while (u != m.end()) {
        if (u->first >= s) break;
        for (int i : u->second) cout << u->first << ' ' << i << endl;
        u++;
      }
    }
  }
}
