#include <bits/stdc++.h>
#pragma GCC optimize "-O3"
using namespace std;
const int MAXN = 1200;
const int INF = 1e6;
int n;
string s[MAXN];
int v[MAXN];
vector<pair<string, int>> rq1[MAXN];
vector<int> rq[MAXN];
set<tuple<int, int, int>> ss;
map<pair<string, int>, int> mm;
set<string> in;
vector<pair<string, int>> ans;
int dd[MAXN];
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> s[i] >> v[i];
    int k;
    cin >> k;
    for (int j = 0; j < k; ++j) {
      string a;
      int b;
      cin >> a >> b;
      rq1[i].push_back({a, b});
    }
  }
  for (int i = 0; i < n; ++i) {
    mm[{s[i], v[i]}] = i;
  }
  for (int i = 0; i < n; ++i) {
    for (auto x : rq1[i]) rq[i].emplace_back(mm[{x.first, x.second}]);
  }
  fill(dd, dd + n, INF);
  dd[0] = 0;
  ss.insert({0, -v[0], 0});
  while (!ss.empty()) {
    int x = get<2>(*ss.begin());
    ss.erase(ss.begin());
    if (in.count(s[x])) continue;
    if (x != 0) ans.push_back({s[x], v[x]});
    in.insert(s[x]);
    for (int y : rq[x]) {
      if (dd[y] > dd[x] + 1) {
        ss.erase({dd[y], -v[y], y});
        dd[y] = dd[x] + 1;
        ss.insert({dd[y], -v[y], y});
      }
    }
  }
  sort(ans.begin(), ans.end());
  cout << ans.size() << "\n";
  for (auto x : ans) cout << x.first << " " << x.second << "\n";
  return 0;
}
