#include <bits/stdc++.h>
using namespace std;
//#include <boost/multiprecision/cpp_int.hpp>
//using multiInt = boost::multiprecision::cpp_int;

using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename Q_type>
using smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;

const int MOD_TYPE = 1;
const ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);
const int INF = (int)1e9;
const ll LINF = (ll)4e18;
const double PI = acos(-1.0);

#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl
#define possible(n) cout << ((n) ? "possible" : "impossible") << endl
#define Yay(n) cout << ((n) ? "Yay!" : ":(") << endl
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x) cerr << #x << ":" << x << endl;

vector<int> Dx = {0, 0, -1, 1, -1, 1, -1, 1, 0};
vector<int> Dy = {1, -1, 0, 0, -1, -1, 1, 1, 0};

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(50) << setiosflags(ios::fixed);
  int q;
  cin >> q;
  multimap<string, int> mp;
  rep(qi, q)
  {
    int type;
    cin >> type;
    if (type == 0)
    {
      string key;
      int x;
      cin >> key >> x;
      mp.emplace(key, x);
    }
    else if (type == 1)
    {
      string key;
      cin >> key;
      auto p = mp.equal_range(key);
      for (auto itr = p.first; itr != p.second; itr++)
      {
        cout << itr->second << endl;
      }
    }
    else if (type == 2)
    {
      string key;
      cin >> key;
      mp.erase(key);
    }
    else
    {
      string l, r;
      cin >> l >> r;
      auto itr = mp.lower_bound(l);
      while (itr != mp.end() && itr->first <= r)
      {
        cout << itr->first << " " << itr->second << endl;
        itr++;
      }
    }
  }
  return 0;
}

