#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  map<pair<string, int>, vector<pair<string, int>>> a;
  map<string, pair<int, int>> used;
  map<pair<string, int>, int> dist;
  pair<string, int> first;
  string name, cname;
  int v, cv, k;
  for (int i = 0; i < n; i++) {
    cin >> name >> v;
    if (i == 0) {
      first = make_pair(name, v);
    }
    cin >> k;
    for (int j = 0; j < k; j++) {
      cin >> cname >> cv;
      a[make_pair(name, v)].push_back(make_pair(cname, cv));
    }
  }
  used[first.first] = make_pair(0, first.second);
  dist[first] = 0;
  queue<pair<string, int>> q;
  pair<string, int> cur;
  q.push(first);
  while (!q.empty()) {
    cur = q.front();
    q.pop();
    if (cur.second == used[cur.first].second) {
      for (pair<string, int> p : a[cur]) {
        if (used.count(p.first) == 0 ||
            (used[p.first].first == dist[cur] + 1 &&
             used[p.first].second < p.second) ||
            used[p.first].first > dist[cur] + 1) {
          dist[p] = dist[cur] + 1;
          used[p.first] = make_pair(dist[p], p.second);
          q.push(p);
        }
      }
    }
  }
  used.erase(first.first);
  cout << used.size() << endl;
  for (auto& el : used) {
    cout << el.first << " " << el.second.second << endl;
  }
  return 0;
}
