#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct package {
  int dist;
  int version;
  string name;
};
bool operator<(const package& a, const package& b) {
  if (a.dist != b.dist) return a.dist < b.dist;
  if (a.version != b.version) return a.version > b.version;
  return a.name < b.name;
}
map<string, int> ans;
map<pair<string, int>, vector<pair<string, int>>> E;
set<package> S;
int main() {
  int n;
  cin >> n;
  pair<string, int> proj;
  for (int i = 0; i < n; i++) {
    string name;
    int version;
    cin >> name >> version;
    int k;
    cin >> k;
    vector<pair<string, int>> t;
    for (int j = 0; j < k; j++) {
      string s;
      cin >> s;
      int v;
      cin >> v;
      t.push_back({s, v});
    }
    E[make_pair(name, version)] = t;
    if (i == 0) {
      S.insert({0, version, name});
      proj = {name, version};
    }
  }
  while (!S.empty()) {
    package p = *S.begin();
    S.erase(S.begin());
    if (ans.count(p.name)) continue;
    ans[p.name] = p.version;
    auto& v = E[make_pair(p.name, p.version)];
    for (const auto& el : v) {
      S.insert({p.dist + 1, el.second, el.first});
    }
  }
  ans.erase(proj.first);
  cout << ans.size() << "\n";
  for (auto el : ans) cout << el.first << " " << el.second << "\n";
  return 0;
}
