#include <bits/stdc++.h>
using namespace std;
struct project {
  string name;
  int v;
};
int n;
project *a;
map<string, int> ans;
vector<project> *e;
unordered_map<string, int> pos;
int *d;
string _to_string(project x) { return x.name + to_string(x.v); }
void bfs() {
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    if (ans.count(a[v].name) && ans.at(a[v].name) != a[v].v) continue;
    for (project u : e[v]) {
      int ind = pos[_to_string(u)];
      if (d[ind] > d[v] + 1) {
        d[ind] = d[v] + 1;
        if (ans.count(u.name)) {
          int old = pos[_to_string({u.name, ans.at(u.name)})];
          if (d[ind] < d[old] || d[ind] == d[old] && u.v > a[old].v) {
            ans.at(u.name) = u.v;
          }
        } else {
          ans.insert({u.name, u.v});
        }
        q.push(ind);
      }
    }
  }
}
int main() {
  std::ios::sync_with_stdio(false);
  cin >> n;
  a = new project[n];
  d = new int[n];
  e = new vector<project>[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i].name >> a[i].v;
    int m;
    cin >> m;
    d[i] = INT_MAX;
    for (int j = 0; j < m; ++j) {
      string name;
      int v;
      cin >> name >> v;
      e[i].push_back({name, v});
    }
    pos.insert({_to_string(a[i]), i});
  }
  d[0] = 0;
  ans.insert({a[0].name, a[0].v});
  bfs();
  cout << ans.size() - 1 << endl;
  for (auto w : ans) {
    if (w.first != a[0].name) cout << w.first << " " << w.second << endl;
  }
  return 0;
}
