#include <bits/stdc++.h>
using namespace std;
int n, m;
pair<string, int> s, a, b;
queue<pair<string, int> > q;
vector<pair<string, int> > v, v1, ans;
map<string, bool> vis;
map<string, pair<int, int> > info;
map<pair<string, int>, vector<pair<string, int> > > G;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a.first >> a.second >> m;
    for (int j = 1; j <= m; j++) {
      cin >> b.first >> b.second;
      G[a].push_back(b);
    }
    if (i == 1) s = a;
  }
  int depth = 0;
  q.push(s);
  vis[s.first] = true;
  info[s.first] = {s.second, 0};
  while (!q.empty()) {
    a = q.front();
    v.clear();
    while (!q.empty() && info[a.first].second == depth) {
      a = q.front();
      v.push_back(a);
      q.pop();
    }
    v1.clear();
    for (int i = 0; i < v.size(); i++) {
      a = v[i];
      for (int j = 0; j < G[a].size(); j++) {
        if (!vis[G[a][j].first]) v1.push_back(G[a][j]);
      }
    }
    sort(v1.begin(), v1.end());
    for (int i = 0; i < v1.size(); i++) {
      a = v1[i];
      while (i + 1 < v1.size() && a.first == v1[i + 1].first) {
        i++;
        a.second = max(a.second, v1[i].second);
      }
      q.push(a);
      ans.push_back(a);
      vis[a.first] = true;
      info[a.first] = {a.second, depth + 1};
    }
    depth++;
  }
  sort(ans.begin(), ans.end());
  cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); i++)
    cout << ans[i].first << " " << ans[i].second << endl;
  return 0;
}
