#include <bits/stdc++.h>
using namespace std;
map<pair<string, int>, vector<pair<string, int> > > gr;
map<string, int> version;
queue<string> q;
map<string, int> was;
vector<pair<string, int> > ans;
map<string, int> d;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (auto i = 0; i < n; i++) {
    string name;
    int v;
    cin >> name >> v;
    if (i == 0) {
      q.push(name);
      version[name] = v;
      was[name] = 1;
    }
    int cnt;
    cin >> cnt;
    for (auto j = 0; j < cnt; j++) {
      string name2;
      int v2;
      cin >> name2 >> v2;
      gr[{name, v}].push_back({name2, v2});
    }
  }
  int it = 0;
  while (!q.empty()) {
    string cur = q.front();
    q.pop();
    pair<string, int> v = {cur, version[cur]};
    if (it != 0) {
      ans.push_back(v);
    }
    it++;
    for (auto to : gr[v]) {
      if (was[to.first] != 2) {
        if (was[to.first] == 0) {
          was[to.first] = 1;
          q.push(to.first);
          version[to.first] = to.second;
          d[to.first] = d[cur] + 1;
        } else if (d[to.first] == d[cur] + 1) {
          version[to.first] = max(to.second, version[to.first]);
        }
      }
    }
  }
  sort(ans.begin(), ans.end());
  cout << ans.size() << '\n';
  for (auto x : ans) {
    cout << x.first << ' ' << x.second << '\n';
  }
}
