#include <bits/stdc++.h>
using namespace std;
struct Pack {
  string name;
  int ver;
  Pack(const string name, int ver = -1) : name(name), ver(ver) {}
  bool operator<(const Pack &x) const {
    return ver != x.ver ? ver < x.ver : name < x.name;
  }
};
map<string, pair<int, int>> taken;
map<Pack, vector<Pack>> deps;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, dep_n, v;
  string dummy;
  Pack root("", -1);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> dummy >> v;
    Pack pack(dummy, v);
    cin >> dep_n;
    for (size_t j = 0; j < dep_n; j++) {
      cin.ignore();
      cin >> dummy >> v;
      Pack dep(dummy, v);
      deps[pack].push_back(dep);
    }
    if (i == 0) root = Pack(pack.name, pack.ver);
    if (i < n - 1) {
      cin.ignore();
      getline(cin, dummy);
    }
  }
  queue<pair<Pack, int>> q;
  q.push({root, 0});
  taken[root.name] = {root.ver, 0};
  while (!q.empty()) {
    pair<Pack, int> p_l = q.front();
    q.pop();
    string pack_n = p_l.first.name;
    int pack_v = p_l.first.ver;
    int pack_l = p_l.second;
    if (taken.find(pack_n) != taken.end() && taken[pack_n].first != pack_v)
      continue;
    for (Pack &dep : deps[p_l.first]) {
      if (taken.find(dep.name) == taken.end() ||
          (taken[dep.name].second == pack_l + 1 &&
           taken[dep.name].first < dep.ver)) {
        taken[dep.name] = {dep.ver, pack_l + 1};
        q.push({dep, pack_l + 1});
      }
    }
  }
  auto it = taken.find(root.name);
  taken.erase(it);
  cout << taken.size() << '\n';
  for (auto kv : taken) {
    cout << kv.first << ' ' << kv.second.first << '\n';
  }
  return 0;
}
