#include <bits/stdc++.h>
using namespace std;
map<string, pair<int, int> > ans;
map<pair<string, int>, int> used;
map<pair<string, int>, vector<pair<string, int> > > gr;
int n, m, ver, ver1, ver0, inf = 1000000001, ans1 = 0;
string str, str1, str0;
queue<pair<pair<string, int>, int> > q, q1;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> str >> ver;
    cin >> m;
    ans[str] = make_pair(-1, inf);
    pair<string, int> x = make_pair(str, ver);
    if (i == 0) str0 = str, ver0 = ver;
    for (int j = 0; j < m; j++) {
      cin >> str1 >> ver1;
      gr[x].push_back(make_pair(str1, ver1));
    }
  }
  q.push(make_pair(make_pair(str0, ver0), 0));
  while (!q.empty()) {
    while (!q.empty()) {
      pair<pair<string, int>, int> o = q.front();
      q.pop();
      for (int i = 0; i < gr[o.first].size(); i++)
        if ((ans[gr[o.first][i].first].first == -1 ||
             ans[gr[o.first][i].first].second == o.second + 1) &&
            gr[o.first][i].first != str0) {
          q1.push(make_pair(gr[o.first][i], o.second + 1));
          ans[gr[o.first][i].first] = make_pair(
              max(ans[gr[o.first][i].first].first, gr[o.first][i].second),
              o.second + 1);
        }
    }
    while (!q1.empty()) {
      pair<pair<string, int>, int> o = q1.front();
      q1.pop();
      if (o.first.second == ans[o.first.first].first && used[o.first] == 0) {
        used[o.first] = 1;
        q.push(o);
      }
    }
  }
  for (map<string, pair<int, int> >::iterator i = ans.begin(); i != ans.end();
       i++)
    if ((i->second).first != -1) ans1++;
  cout << ans1 << "\n";
  for (map<string, pair<int, int> >::iterator i = ans.begin(); i != ans.end();
       i++)
    if ((i->second).first != -1)
      cout << (i->first) << " " << ((i->second).first) << "\n";
}
