#include <bits/stdc++.h>
using namespace std;
string int_to_string(int a) {
  vector<char> ch;
  while (a) {
    ch.push_back((a % 10) + 48);
    a /= 10;
  }
  string s = "";
  for (int i = ch.size() - 1; i >= 0; --i) s += ch[i];
  return s;
}
struct point {
  string name;
  int version;
  int distance = (int)1e9;
  vector<string> neighbours;
  string name_version;
};
unordered_map<string, int> point_index;
queue<int> que;
vector<point> all;
unordered_map<string, int> best_version_for_name;
vector<string> result_names;
void make(point& x, int distance) {
  if (x.distance > distance + 1) {
    x.distance = distance + 1;
  }
  if (best_version_for_name[x.name] == 0) {
    result_names.push_back(x.name);
    best_version_for_name[x.name] = x.version;
    que.push(point_index[x.name_version]);
  } else if (best_version_for_name[x.name] < x.version) {
    string current =
        x.name + ' ' + int_to_string(best_version_for_name[x.name]);
    if (all[point_index[current]].distance >= x.distance) {
      best_version_for_name[x.name] = x.version;
      que.push(point_index[x.name_version]);
    }
  }
}
void push_gray(point& x) {
  for (int i = 0; i < x.neighbours.size(); ++i) {
    int pos = point_index[x.neighbours[i]];
    make(all[pos], x.distance);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  point x;
  all.assign(n + 5, x);
  for (int i = 1; i <= n; ++i) {
    cin >> all[i].name >> all[i].version;
    all[i].name_version = all[i].name + ' ' + int_to_string(all[i].version);
    point_index[all[i].name_version] = i;
    int k;
    cin >> k;
    for (int j = 0; j < k; ++j) {
      cin >> x.name >> x.version;
      x.name = x.name + ' ' + int_to_string(x.version);
      all[i].neighbours.push_back(x.name);
    }
  }
  all[1].distance = 0;
  que.push(1);
  best_version_for_name[all[1].name] = all[1].version;
  while (!que.empty()) {
    if (best_version_for_name[all[que.front()].name] ==
        all[que.front()].version) {
      push_gray(all[que.front()]);
    }
    que.pop();
  }
  sort(result_names.begin(), result_names.end());
  cout << result_names.size() << '\n';
  for (int i = 0; i < result_names.size(); ++i) {
    cout << result_names[i] << " " << best_version_for_name[result_names[i]]
         << '\n';
  }
  return 0;
}
