#include <bits/stdc++.h>
using namespace std;
constexpr size_t inf = numeric_limits<size_t>::max();
using uint = unsigned int;
struct Proj {
  string name;
  int ver;
};
bool operator<(const Proj &a, const Proj &b) {
  if (a.ver == b.ver)
    return a.name < b.name;
  else
    return a.ver < b.ver;
}
bool versionCmp(const Proj &a, const Proj &b) { return a.ver > b.ver; }
bool nameCmp(const Proj &a, const Proj &b) { return a.name < b.name; }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  map<Proj, vector<Proj>> projectDeps;
  Proj root;
  for (int i = 0; i < n; ++i) {
    Proj p;
    cin >> p.name >> p.ver;
    if (i == 0) {
      root = p;
    }
    int ndeps;
    cin >> ndeps;
    vector<Proj> deps;
    deps.reserve(ndeps);
    for (int j = 0; j < ndeps; ++j) {
      Proj d;
      cin >> d.name >> d.ver;
      deps.push_back(d);
    }
    projectDeps[p] = move(deps);
  }
  map<string, int> usedVer;
  vector<Proj> curr;
  map<string, int> next;
  curr.push_back(root);
  set<Proj> visited;
  usedVer[root.name] = root.ver;
  while (!curr.empty()) {
    next.clear();
    for (Proj &c : curr) {
      for (Proj &d : projectDeps[c]) {
        if (usedVer.find(d.name) != usedVer.end()) {
          continue;
        }
        auto it = next.find(d.name);
        if (it == next.end()) {
          next[d.name] = d.ver;
        } else if (it->second < d.ver) {
          it->second = d.ver;
        }
      }
    }
    curr.clear();
    curr.reserve(next.size());
    for (auto it = next.begin(); it != next.end(); ++it) {
      Proj p{it->first, it->second};
      curr.push_back(p);
      usedVer[p.name] = p.ver;
    }
  }
  usedVer.erase(root.name);
  cout << usedVer.size() << '\n';
  for (auto it = usedVer.begin(); it != usedVer.end(); ++it) {
    cout << it->first << " " << it->second << '\n';
  }
}
