#include <bits/stdc++.h>
using namespace std;
struct Name {
  string str;
  int version;
  string compound() {
    char buf[10];
    snprintf(buf, 9, "%d", version);
    return str + string(buf);
  }
  Name(const string& s, int v) : str(s), version(v) {}
  Name() : version(0) {}
};
struct Project {
  Name name;
  vector<Name> depends;
  Project(const string& s, int v) : name(s, v) {}
  Project() {}
};
int main() {
  int n;
  cin >> n;
  unordered_map<string, Project> globalMap;
  Project mainProject;
  for (int i = 0; i < n; ++i) {
    string s;
    int num;
    int count;
    cin >> s >> num >> count;
    Project pj(s, num);
    pj.depends.reserve(count);
    for (int j = 0; j < count; ++j) {
      cin >> s >> num;
      pj.depends.emplace_back(Name(s, num));
    }
    globalMap[pj.name.compound()] = pj;
    if (i == 0) {
      mainProject = pj;
    }
  }
  multimap<int, string> bufferMaps[2];
  int from = 0;
  int to = 1;
  bufferMaps[0].insert(
      make_pair(mainProject.name.version, mainProject.name.compound()));
  map<string, int> answerMap;
  while (!bufferMaps[from].empty()) {
    bufferMaps[to].clear();
    for (auto riter = bufferMaps[from].rbegin();
         riter != bufferMaps[from].rend(); ++riter) {
      auto& elem = globalMap[riter->second];
      if (answerMap.find(elem.name.str) == answerMap.end()) {
        answerMap[elem.name.str] = elem.name.version;
        for (auto& child : elem.depends) {
          bufferMaps[to].insert(make_pair(child.version, child.compound()));
        }
      }
    }
    from = 1 - from;
    to = 1 - to;
  }
  cout << answerMap.size() - 1 << endl;
  for (auto& elem : answerMap) {
    Name name(elem.first, elem.second);
    if (name.compound() != mainProject.name.compound())
      cout << name.str << " " << name.version << endl;
  }
  return 0;
}
