#include <bits/stdc++.h>
using namespace std;
struct prj {
  string name;
  int num;
  friend bool operator<(const prj& lhs, const prj& rhs) {
    if (lhs.name == rhs.name) return lhs.num > rhs.num;
    return lhs.name < rhs.name;
  }
};
vector<vector<int> > gr;
vector<vector<int> > grreverse;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) gr.push_back(vector<int>());
  for (int i = 0; i < n; i++) grreverse.push_back(vector<int>());
  map<prj, int> arr;
  set<prj> all;
  vector<prj> Vert(n);
  for (int i = 0; i < n; i++) {
    prj Now;
    cin >> Now.name >> Now.num;
    int Vnow;
    if (arr[Now] == 0) {
      all.insert(Now);
      arr[Now] = (int)all.size() - 1;
      Vert[(int)all.size() - 1] = Now;
    }
    Vnow = arr[Now];
    Vert[Vnow] = Now;
    int m;
    cin >> m;
    for (int j = 0; j < m; j++) {
      prj New;
      cin >> New.name >> New.num;
      if (arr[New] == 0) {
        all.insert(New);
        arr[New] = (int)all.size() - 1;
        Vert[(int)all.size() - 1] = New;
      }
      gr[Vnow].push_back(arr[New]);
      grreverse[arr[New]].push_back(Vnow);
    }
  }
  vector<prj> ans;
  set<int> allans;
  allans.insert(0);
  vector<int> level;
  level.push_back(0);
  set<int> pts;
  map<string, int> vers;
  set<string> allin;
  allin.insert(Vert[0].name);
  vers[Vert[0].name] = Vert[0].num;
  while (level.size() > 0) {
    pts.clear();
    for (int i = 0; i < level.size(); i++) {
      for (int j = 0; j < gr[level[i]].size(); j++) {
        if (allin.find(Vert[gr[level[i]][j]].name) == allin.end())
          if (vers[Vert[gr[level[i]][j]].name] < Vert[gr[level[i]][j]].num) {
            prj X;
            X.name = Vert[gr[level[i]][j]].name;
            X.num = vers[Vert[gr[level[i]][j]].name];
            if (vers[Vert[gr[level[i]][j]].name] != 0) pts.erase(arr[X]);
            pts.insert(gr[level[i]][j]);
            vers[Vert[gr[level[i]][j]].name] = Vert[gr[level[i]][j]].num;
          }
      }
    }
    level.clear();
    for (auto X : pts) {
      allin.insert(Vert[X].name);
      level.push_back(X);
      allans.insert(X);
    }
  }
  allans.erase(0);
  for (auto X : allans) {
    ans.push_back(Vert[X]);
  }
  sort(ans.begin(), ans.end());
  cout << (int)ans.size() << endl;
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i].name << ' ' << ans[i].num << endl;
  }
  return 0;
}
