#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
const int MAXN = 1e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 1e9;
signed main() {
  int n;
  map<pair<string, int>, vector<pair<string, int>>> g;
  unordered_set<string> used;
  vector<pair<string, int>> ans;
  set<tuple<int, int, string>> q;
  cin >> n;
  for (int it = 0; it < n; it++) {
    string s, s1;
    int v, c, v1;
    cin >> s >> v >> c;
    if (it == 0) {
      q.insert(make_tuple(0, -v, s));
    }
    while (c--) {
      cin >> s1 >> v1;
      g[make_pair(s, v)].push_back(make_pair(s1, v1));
    }
  }
  while (q.size()) {
    int d = get<0>(*q.begin());
    pair<string, int> cur = make_pair(get<2>(*q.begin()), -get<1>(*q.begin()));
    q.erase(q.begin());
    if (used.find(cur.first) != used.end()) continue;
    if (d) ans.push_back(cur);
    used.insert(cur.first);
    for (pair<string, int> to : g[cur]) {
      q.insert(make_tuple(d + 1, -to.second, to.first));
    }
  }
  sort(ans.begin(), ans.end());
  cout << ans.size() << endl;
  for (auto pr : ans) cout << pr.first << ' ' << pr.second << endl;
  return 0;
}
