# include <iostream>
# include <algorithm>
#include <array>
# include <cassert>
#include <cctype>
#include <climits>
#include <numeric>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <tuple>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <bitset>
# include <complex>
# include <chrono>
# include <random>
# include <limits.h>
# include <unordered_map>
# include <unordered_set>
# include <deque>
# include <cstdio>
# include <cstring>
#include <stdio.h>
#include<time.h>
#include <stdlib.h>
#include <cstdint>
#include <cfenv>

//#include <bits/stdc++.h>
using namespace std;
using LL = int;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7;
constexpr long long INF = numeric_limits<LL>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr<<#x<<": "<<x<<'\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll { LL first, second, third; };
struct Fll { LL first, second, third, fourd; };
typedef pair<LL, Tll> Ptll;
#define rep(i,rept) for(LL i=0;i<rept;i++)
#define Mfor(i,mf) for(LL i=mf-1;i>=0;i--)
LL h, w, n, m, k, t, s, q, last, cnt, sum, ans, dp[10000][4], a[2000000], b[2000000];
string str, ss;
bool f[1100][1100];
char c;
int di[4][2] = { { 0,1 },{ 1,0 },{ 0,-1 },{ -1,0 } };
struct Edge { LL to, cost; };
vector<Edge>vec[200000];
vector<LL>v;
map<string, vector<LL>>ma;
set<LL>st;
void YN(bool f) {
	if (f)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
}
void yn(bool f) {
	if (f)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
}
int main() {
	cin >> n;
	ma["あ"].push_back(0);
	rep(i, n) {
		LL x,y,z,w;
		string s1, s2;
		cin >> x;
		if (x == 0) {
			cin >> s1;
			cin >> y;
			ma[s1].push_back(y);
		}
		if (x == 1) {
			cin >> s1;
			if (ma[s1].size()) {
				rep(j, ma[s1].size()) {
					cout << ma[s1][j];
					cout << endl;
				}
			}
			
		}
		if (x == 2) {
			cin >> s1;
			ma[s1].clear();
		}
		if (x == 3) {
			cin >> s1 >> s2;
			s2.push_back(0);
			auto it1 = ma.lower_bound(s1), it2 = ma.upper_bound(s2);
			for (auto itr = it1; itr != it2; itr++) {
				if (itr->second.size() > 0) {
					rep(j, itr->second.size()) {
						cout << itr->first << " " << itr->second[j] << endl;
					}

				}
			}
		}
	}
	return 0;
}
