#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  map<pair<string, int>, vector<pair<string, int> > > g;
  map<string, int> u;
  pair<string, int> hv;
  for (int i = 0; i < n; ++i) {
    string str;
    int y;
    cin >> str >> y;
    if (!i) hv = {str, y};
    g.insert(make_pair(make_pair(str, y), vector<pair<string, int> >()));
    u.insert(make_pair(str, 0));
    int m;
    cin >> m;
    for (int j = 0; j < m; ++j) {
      string cstr;
      int cy;
      cin >> cstr >> cy;
      g[{str, y}].push_back(make_pair(cstr, cy));
    }
  }
  int ans = 0;
  queue<pair<string, int> > qu;
  map<string, int> tos;
  qu.push(hv);
  vector<pair<string, int> > vans;
  while (!qu.empty() || !tos.empty()) {
    if (qu.empty()) {
      for (map<string, int>::iterator it = tos.begin(); it != tos.end(); ++it) {
        qu.push(*it);
        u[it->first] = 1;
      }
      tos.clear();
    }
    pair<string, int> v = qu.front();
    qu.pop();
    if (ans) vans.push_back(v);
    ++ans;
    u[v.first] = 1;
    vector<pair<string, int> > cv = g[v];
    for (int i = 0; i < cv.size(); ++i) {
      pair<string, int> to = g[v][i];
      if (u[to.first]) continue;
      string name = to.first;
      int y = to.second;
      if (tos.find(name) == tos.end())
        tos.insert(make_pair(name, y));
      else
        tos[name] = max(tos[name], y);
    }
  }
  sort(vans.begin(), vans.end());
  cout << ans - 1 << '\n';
  for (int i = 0; i < vans.size(); ++i)
    cout << vans[i].first << " " << vans[i].second << '\n';
  return 0;
}
