#include <bits/stdc++.h>
#define loop(n) for (int ngtkana_is_a_genius = 0; ngtkana_is_a_genius < int(n); ngtkana_is_a_genius++)
#define rep(i, begin, end) for (int i = int(begin); (i) < int(end); i++)
#define all(v) v.begin(), v.end()
#define rand(l, r) std::uniform_int_distribution<>(l, r)(mt)
using lint = long long;
auto mt = std::mt19937_64(std::random_device{}());
auto cmn = [](auto& a, auto b){ if (a > b) {a = b; return true;} return false; };
auto cmx = [](auto& a, auto b){ if (a < b) {a = b; return true;} return false; };
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) { std::cerr << " " << head; debug_impl(tail...); }
#ifndef STOPIT
#define debug(...)\
  do {\
    std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\
    debug_impl(__VA_ARGS__);\
    std::cerr << std::noboolalpha;\
  } while (false)
#else
#define debug(...) {}
#endif

template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr>
std::istream& operator>> (std::istream& is, Container& v)
  { for (auto & x : v) { is >> x; } return is; }

template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr >
std::ostream& operator<< (std::ostream& os, Container const& v) {
 os << "{";
  for (auto it = v.begin(); it != v.end(); it++)
    {os << (it != v.begin() ? "," : "") << *it;}
  return os << "}";
}

template < template < typename ... > class Tuple,  typename... Args, std::size_t ... Inds, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::istream& tuple_input_impl(std::istream& os, Tuple<Args...>& tuple, std::integer_sequence<std::size_t, Inds...>)
  { (void)std::initializer_list<int>{((void)(os >> std::get< Inds >(tuple)), 0)...}; return os; }
template < template < typename ... > class Tuple,  typename... Args, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::istream& operator>> (std::istream& os, Tuple<Args...>& tuple)
  { return tuple_input_impl(os, tuple, std::index_sequence_for<Args...>()); }

template < template < typename ... > class Tuple,  typename... Args, std::size_t ... Inds, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::ostream& tuple_output_impl(std::ostream& os, const Tuple<Args...>& tuple, std::integer_sequence<std::size_t, Inds...>)
  { os << "("; (void)std::initializer_list<int>{((void)(os << (Inds > 0 ? "," : "") << std::get< Inds >(tuple)), 0)...}; return os << ")"; }
template < template < typename ... > class Tuple,  typename... Args, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::ostream& operator<< (std::ostream& os, const Tuple<Args...>& tuple)
 { return tuple_output_impl(os, tuple, std::index_sequence_for<Args...>()); }

unsigned long xor128() {
  static unsigned long x=123456789, y=362436069, z=521288629, w=88675123;
  unsigned long t=(x^(x<<11));
  x=y; y=z; z=w;
  return ( w=(w^(w>>19))^(t^(t>>8)) );
}

template <class T, class U, bool HasUniqueKey>
class Treap {
    std::size_t size_ = 0;
    struct Node {
        T key;
        U value;
        int priority;
        Node *l, *r;
        Node(T key, U value, int priority)
          : key(key), value(value), priority(priority), l(nullptr), r(nullptr) {}
    } *root = nullptr;
    using Tree = Node *;

    void split(Tree t, T key, Tree& l, Tree& r, bool by_upper_bound) {
        if (!t) {
            l = r = nullptr;
        } else if (key < t->key || (key == t->key && !by_upper_bound)) {
            split(t->l, key, l, t->l, by_upper_bound), r = t;
        } else {
            split(t->r, key, t->r, r, by_upper_bound), l = t;
        }
    }

    void split_by_lower_bound(Tree t, T key, Tree& l, Tree& r) {
      split(t,key,l,r,false);
    }
    void split_by_upper_bound(Tree t, T key, Tree& l, Tree& r) {
      split(t,key,l,r,true);
    }
    void split_into_tree_parts(Tree t, T key, Tree& l, Tree& c, Tree& r) {
      Tree tmp;
      split_by_lower_bound(t, key, l, tmp);
      split_by_upper_bound(tmp, key, c, r);
    }

    void merge(Tree& t, Tree l, Tree r) {
      if (!l || !r) {
        t = l ? l : r;
      } else if (l->priority > r->priority) {
        merge(l->r, l->r, r), t = l;
      } else {
        merge(r->l, l, r->l), t = r;
      }
    }
    void merge_three_trees(Tree& t, Tree l, Tree c, Tree r) {
      Tree tmp;
      merge(tmp, c, r);
      merge(t, l, tmp);
    }

    void insert(Tree& t, Tree item) {
      if (!t) {
        t = item;
      } else if (item->priority > t->priority) {
        split_by_lower_bound(t, item->key, item->l, item->r), t = item;
      } else {
        insert(item->key < t->key ? t->l : t->r, item);
      }
    }

    void erase(Tree& t, T key) {
      Tree l,r,c;
      split_into_tree_parts(t, key, l, c, r);
      merge(t, l, r);
      size_ -= nuke(c);
    }

    std::size_t find(Tree& t, T key) {
      Tree l,r,c;
      split_into_tree_parts(t, key, l, c, r);
      std::size_t ans = count(c);
      merge_three_trees(t, l, c, r);
      return ans;
    }

    std::size_t nuke(Tree t) {
      if (!t) return 0;
      std::size_t ret = 1;
      ret += nuke(t->l);
      ret += nuke(t->r);
      delete t;
      return ret;
    }

    std::size_t count(Tree t) {
      if (!t) return 0;
      std::size_t ret = 1;
      ret += count(t->l);
      ret += count(t->r);
      return ret;
    }

    void collect_(std::vector<std::pair<T,U>>& v, Tree t) {
      if (!t) return;
      collect_(v, t->l);
      v.emplace_back(t->key, t->value);
      collect_(v, t->r);
    }

  public:
    std::size_t size() const { return size_; }

    void insert(T key, U value) {
      if (HasUniqueKey && find(key)) return;
      size_++;
      insert(root, new Node(key, value, xor128()));
    }

    void insert_or_replace(T key, U value) {
      erase(key);
      size_++;
      insert(root, new Node(key, value, xor128()));
    }

    void erase(T key) {
      erase(root, key);
    }

    std::size_t find(T key) {
      return find(root, key);
    }

    std::vector<std::pair<T,U>> collect(T x, T y) {
      std::vector<std::pair<T,U>> v;
      Tree l,r,c,tmp;
      split_by_lower_bound(root, x, l, tmp);
      split_by_upper_bound(tmp, y, c, r);
      collect_(v, c);
      merge(tmp, c, r);
      merge(root, l, tmp);
      return v;
    }
    std::vector<std::pair<T,U>> collect() {
      std::vector<std::pair<T,U>> v;
      collect_(v, root);
      return v;
    }
};

template <class T, class U>
using map = Treap<T, U, true>;
template <class T, class U>
using multimap = Treap<T, U, false>;

int main() {
  std::cin.tie(0); std::cin.sync_with_stdio(false);
  map<std::string, int> treap;
  int q; std::cin >> q;
  loop(q) {
    int c; std::cin >> c;
    if (c == 0) {
      std::string s; int x;
      std::cin >> s >> x;
      treap.insert_or_replace(s, x);
    } else if (c == 1) {
      std::string s; std::cin >> s;
      auto v = treap.collect(s,s);
      if (v.empty()) {
        std::cout << 0 << std::endl;
      } else {
        for (auto&& pair : v) {
          std::cout << pair.second << std::endl;
        }
      }
    } else if (c == 2) {
      std::string s; std::cin >> s;
      treap.erase(s);
    } else if (c == 3) {
      std::string l, r; std::cin >> l >> r;
      auto v = treap.collect(l,r);
      for (auto&& x : v) {
        std::cout << x.first << " " << x.second << std::endl;
      }
    }
    // debug(treap.collect());
  }
  return 0;
}


Measured memory_bytes: 29712