#include <bits/stdc++.h>
struct pairhash {
 public:
  template <typename T, typename U>
  std::size_t operator()(const std::pair<T, U>& x) const {
    return std::hash<T>()(x.first) ^ std::hash<U>()(x.second);
  }
};
using pkg_v = std::pair<std::string, int>;
std::vector<pkg_v> id_to_pkg;
std::unordered_map<pkg_v, int, pairhash> pkg_to_id;
int register_pkg(const pkg_v& v) {
  auto it = pkg_to_id.find(v);
  int id;
  if (it == pkg_to_id.end()) {
    id = (int)id_to_pkg.size();
    pkg_to_id[v] = id;
    id_to_pkg.push_back(v);
  } else {
    id = it->second;
  }
  return id;
}
int main() {
  int n;
  std::cin >> n;
  std::vector<std::vector<int>> g(n);
  for (int i = 0; i < n; ++i) {
    pkg_v pkg;
    std::cin >> pkg.first >> pkg.second;
    int id = register_pkg(pkg);
    int m;
    std::cin >> m;
    for (int j = 0; j < m; ++j) {
      pkg_v dep;
      std::cin >> dep.first >> dep.second;
      int dep_id = register_pkg(dep);
      g[id].push_back(dep_id);
    }
  }
  std::unordered_map<std::string, int> selected_pkg_to_id;
  std::unordered_map<std::string, int> selected_pkg_to_level;
  std::unordered_set<int> to_process = {0};
  std::unordered_set<int> visited;
  int level = 0;
  while (!to_process.empty()) {
    std::unordered_set<int> selected_on_this_row;
    for (const auto& id : to_process) {
      if (visited.find(id) != visited.end()) {
        continue;
      }
      const pkg_v& pkg = id_to_pkg.at(id);
      auto it = selected_pkg_to_id.find(pkg.first);
      auto level_it = selected_pkg_to_level.find(pkg.first);
      if (it == selected_pkg_to_id.end()) {
        selected_pkg_to_id[pkg.first] = id;
        selected_pkg_to_level[pkg.first] = level;
        selected_on_this_row.insert(id);
      } else if (level_it->second == level) {
        int prev_pkg_id = it->second;
        const pkg_v& prev_pkg = id_to_pkg.at(prev_pkg_id);
        if (prev_pkg.second < pkg.second) {
          selected_on_this_row.erase(prev_pkg_id);
          selected_on_this_row.insert(id);
          selected_pkg_to_id[pkg.first] = id;
        }
      }
    }
    std::unordered_set<int> next_to_process;
    for (const auto& s : selected_on_this_row) {
      for (const auto& child : g[s]) {
        next_to_process.insert(child);
      }
    }
    level += 1;
    to_process = next_to_process;
  }
  selected_pkg_to_id.erase(id_to_pkg.at(0).first);
  std::vector<pkg_v> selected_pkgs;
  for (const auto& x : selected_pkg_to_id) {
    int id = x.second;
    pkg_v p = id_to_pkg.at(id);
    selected_pkgs.push_back(p);
  }
  std::sort(
      std::begin(selected_pkgs), std::end(selected_pkgs),
      [](const pkg_v& v1, const pkg_v& v2) { return v1.first < v2.first; });
  std::cout << selected_pkgs.size() << '\n';
  for (const auto& x : selected_pkgs) {
    std::cout << x.first << ' ' << x.second << '\n';
  }
  return 0;
}
