#include <iostream>
#include <map>

using namespace std;

int main()
{
  int N, Command, Val;
  string Key, LB, UB; 
  cin >> N;

  multimap<string, int> MultiMap;

  for(int i = 0; i < N; ++i)
  {
    cin >> Command >> Key;

    if(Command == 0)
    {
      cin >> Val;   
      //MultiMap[Key] = Val;
      MultiMap.insert(make_pair(Key, Val));
    }
    else if(Command == 1)
    {
      auto ite1 = MultiMap.lower_bound(Key);
      auto ite2 = MultiMap.upper_bound(Key);

      while(ite1 != ite2)
      {
	cout << ite1->second << endl;
	++ite1;
      }
    }
    else if(Command == 2)
    {
      auto ite1 = MultiMap.lower_bound(Key);
      auto ite2 = MultiMap.upper_bound(Key);
      MultiMap.erase(ite1, ite2); 
    }
    else if(Command == 3)
    {
      LB = Key;
      cin >> UB;

      auto ite1 = MultiMap.lower_bound(LB);
      auto ite2 = MultiMap.upper_bound(UB);

      while(ite1 != ite2)
      {
	cout << ite1->first << " " << ite1->second << endl;
	++ite1;
      }
 

    }


  
  }

  return 0;
}
