#include <bits/stdc++.h>
int main() {
  size_t projectNum;
  std::vector<std::pair<std::string, size_t>> projects;
  std::map<std::pair<std::string, size_t>, size_t> indexes;
  std::map<std::string, int> cur_versions, cur_dist;
  std::vector<std::vector<size_t>> dependencies;
  std::vector<std::vector<std::pair<std::string, size_t>>> dependencies_raw;
  std::cin >> projectNum;
  dependencies.resize(projectNum);
  dependencies_raw.resize(projectNum);
  for (size_t i = 0; i < projectNum; ++i) {
    std::string name;
    size_t version;
    std::cin >> name >> version;
    projects.push_back({name, version});
    indexes[{name, version}] = projects.size() - 1;
    cur_dist[name] = 100000000;
    cur_versions[name] = -1;
    size_t dependencies_num;
    std::cin >> dependencies_num;
    for (size_t j = 0; j < dependencies_num; ++j) {
      std::string child_name;
      size_t child_version;
      std::cin >> child_name >> child_version;
      dependencies_raw[projects.size() - 1].push_back(
          {child_name, child_version});
    }
  }
  for (size_t i = 0; i < dependencies_raw.size(); ++i) {
    for (auto dependency : dependencies_raw[i]) {
      dependencies[i].push_back(indexes[dependency]);
    }
  }
  std::queue<size_t> q;
  q.push(0);
  cur_versions[projects[0].first] = projects[0].second;
  while (q.size() > 0) {
    std::map<std::string, size_t> added;
    while (q.size() > 0) {
      size_t cur = q.front();
      q.pop();
      for (size_t dep_idx : dependencies[cur]) {
        std::pair<std::string, size_t> dep_project = projects[dep_idx];
        if (cur_versions[dep_project.first] < 0) {
          if (added.find(dep_project.first) != added.end()) {
            added[dep_project.first] =
                std::max(added[dep_project.first], dep_project.second);
          } else {
            added[dep_project.first] = dep_project.second;
          }
        }
      }
    }
    for (auto it = added.begin(); it != added.end(); ++it) {
      cur_versions[it->first] = it->second;
      q.push(indexes[{it->first, it->second}]);
    }
  }
  std::vector<std::pair<std::string, size_t>> ans;
  for (auto it = cur_versions.begin(); it != cur_versions.end(); ++it) {
    if (it->second >= 0 && it->first != projects[0].first) {
      ans.push_back(std::make_pair(it->first, it->second));
    }
  }
  std::sort(ans.begin(), ans.end());
  std::cout << ans.size() << '\n';
  for (size_t i = 0; i < ans.size(); ++i) {
    std::cout << ans[i].first << ' ' << ans[i].second << '\n';
  }
}
