#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  map<pair<string, int>, vector<pair<string, int>>> gr;
  map<string, bool> exist;
  vector<pair<string, int>> q;
  pair<string, int> st;
  for (int i = 0; i < n; ++i) {
    string name;
    int num;
    cin >> name >> num;
    if (!i) {
      exist[name] = 1;
      st = {name, num};
      q.emplace_back(st);
    }
    int k;
    cin >> k;
    for (int j = 0; j < k; ++j) {
      string s;
      int val;
      cin >> s >> val;
      gr[{name, num}].emplace_back(s, val);
    }
  }
  map<pair<string, int>, bool> used;
  while (!q.empty()) {
    map<string, int> ver;
    for (const auto &x : q) {
      ver[x.first] = max(ver[x.first], x.second);
    }
    map<pair<string, int>, bool> next;
    vector<pair<string, int>> nq;
    for (const auto &x : q) {
      if (exist[x.first] && x != st) {
        continue;
      }
      if (ver[x.first] == x.second) {
        used[x] = 1;
        exist[x.first] = 1;
        for (const auto &to : gr[x]) {
          if (!next[to]) {
            next[to] = 1;
            nq.emplace_back(to);
          }
        }
      }
    }
    q = nq;
  }
  vector<pair<string, int>> ans;
  for (auto x : used) {
    if (x.second && x.first != st) {
      ans.emplace_back(x.first);
    }
  }
  cout << ans.size() << "\n";
  for (auto x : ans) {
    cout << x.first << " " << x.second << "\n";
  }
}
