#include <bits/stdc++.h>
int main() {
  int nr_projects;
  std::cin >> nr_projects;
  std::map<std::pair<std::string, int>,
           std::vector<std::pair<std::string, int> > >
      lda;
  std::vector<std::pair<std::string, int> > queue;
  std::vector<int> distance;
  std::string foreign;
  for (int i = 0; i < nr_projects; ++i) {
    std::string current_project;
    std::cin >> current_project;
    int project_version;
    std::cin >> project_version;
    int nr_dependencies;
    std::cin >> nr_dependencies;
    std::pair<std::string, int> key(current_project, project_version);
    lda.insert(std::pair<std::pair<std::string, int>,
                         std::vector<std::pair<std::string, int> > >(
        key, std::vector<std::pair<std::string, int> >()));
    if (i == 0) {
      queue.push_back(key);
      distance.push_back(0);
      foreign = current_project;
    }
    for (int j = 0; j < nr_dependencies; ++j) {
      std::string current_dependency;
      std::cin >> current_dependency;
      int dependency_version;
      std::cin >> dependency_version;
      lda[key].push_back({current_dependency, dependency_version});
    }
  }
  std::map<std::string, std::pair<int, int> > response;
  int i = 0;
  while (i < (int)queue.size()) {
    std::map<std::string, std::pair<int, int> > temporary_response;
    int j = i;
    while (j < (int)queue.size() && distance[i] == distance[j]) {
      std::string candidate_name = queue[j].first;
      int candidate_version = queue[j].second;
      if (temporary_response.count(candidate_name) == 0 ||
          temporary_response[candidate_name].second < candidate_version) {
        temporary_response[candidate_name] =
            std::pair<int, int>(distance[i], candidate_version);
      }
      ++j;
    }
    std::map<std::string, std::pair<int, int> >::iterator it;
    for (it = temporary_response.begin(); it != temporary_response.end();
         ++it) {
      if (response.count(it->first) == 0) {
        response.insert(std::pair<std::string, std::pair<int, int> >(
            it->first,
            std::pair<int, int>(it->second.first, it->second.second)));
        std::vector<std::pair<std::string, int> > dependencies =
            lda[std::pair<std::string, int>(it->first, it->second.second)];
        for (int k = 0; k < (int)dependencies.size(); ++k) {
          queue.push_back(dependencies[k]);
          distance.push_back(distance[i] + 1);
        }
      }
    }
    i = j;
  }
  response.erase(foreign);
  std::cout << response.size() << "\n";
  std::map<std::string, std::pair<int, int> >::iterator it;
  for (it = response.begin(); it != response.end(); ++it) {
    std::cout << it->first << " " << it->second.second << "\n";
  }
}
