#include <bits/stdc++.h>
using namespace std;
int readInt() {
  int x;
  cin >> x;
  return x;
}
string readString() {
  string x;
  cin >> x;
  return x;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n = readInt();
  map<pair<string, int>, vector<pair<string, int>>> dependencies;
  map<string, bool> projects;
  string poly_project;
  int poly_version;
  for (int i = 0; i < n; ++i) {
    string project = readString();
    int version = readInt();
    if (i == 0) {
      poly_project = project;
      poly_version = version;
    }
    dependencies[make_pair(project, version)];
    int deps = readInt();
    for (int j = 0; j < deps; ++j) {
      string parent_project = readString();
      int parent_version = readInt();
      dependencies[make_pair(project, version)].push_back(
          make_pair(parent_project, parent_version));
    }
  }
  projects[poly_project] = true;
  map<string, int> answer;
  vector<pair<string, int>> cur_level;
  cur_level.push_back(make_pair(poly_project, poly_version));
  while (!cur_level.empty()) {
    for (auto project_version : cur_level) {
      if (project_version.first != poly_project) {
        answer[project_version.first] = project_version.second;
      }
    }
    map<string, int> project_best_version;
    for (auto project_version : cur_level) {
      for (auto next_project_version : dependencies[project_version]) {
        if (!projects[next_project_version.first]) {
          project_best_version[next_project_version.first] =
              max(project_best_version[next_project_version.first],
                  next_project_version.second);
        }
      }
    }
    vector<pair<string, int>> next_level;
    for (auto const& it : project_best_version) {
      next_level.push_back(make_pair(it.first, it.second));
      projects[it.first] = true;
    }
    cur_level = next_level;
  }
  cout << answer.size() << "\n";
  for (auto const& it : answer) {
    cout << it.first << " " << it.second << "\n";
  }
  return 0;
}
