#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:667177216")
using namespace std;
const long long MOD = 1000000000 + 7;
const long long MAXN = 2000000 + 10;
char isLetter(char a) {
  return (a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z');
}
char tolower(char a) {
  if (a >= 'a' && a <= 'z') {
    return a;
  }
  return a - 'A' + 'a';
}
void normalize(string &a) {
  for (int i = 0; i < a.size(); ++i) {
    if (isLetter(a[i])) {
      a[i] = tolower(a[i]);
    }
  }
  for (int i = 0; i < a.size(); ++i) {
    if (a[i] == '0') {
      a[i] = 'o';
    }
    if (a[i] == 'l' || a[i] == 'i') {
      a[i] = '1';
    }
  }
}
map<pair<string, int>, int> indexes;
map<int, pair<string, int>> rev_index;
int get_index(string &s, int v) {
  if (indexes.count({s, v})) {
    return indexes[{s, v}];
  }
  int sz = indexes.size();
  indexes[{s, v}] = sz;
  rev_index[sz] = {s, v};
  return sz;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie();
  int n;
  cin >> n;
  map<int, int> versions;
  vector<vector<int>> g(n);
  string my_project_name;
  int my_v;
  for (int(i) = 0; (i) < n; (i)++) {
    string name;
    int version;
    cin >> name >> version;
    if (i == 0) {
      my_project_name = name;
      my_v = version;
    }
    int dep;
    cin >> dep;
    for (int(j) = 0; (j) < dep; (j)++) {
      string nm;
      int v;
      cin >> nm >> v;
      g[get_index(name, version)].push_back(get_index(nm, v));
    }
  }
  int v = get_index(my_project_name, my_v);
  queue<int> q;
  q.push(v);
  set<pair<string, int>> ans;
  set<string> ans_v;
  vector<char> u(n);
  u[v] = 1;
  while (q.size() > 0) {
    vector<int> ind;
    while (q.size() > 0) {
      ind.push_back(q.front());
      q.pop();
    }
    map<string, int> bigger;
    for (int i = 0; i < ind.size(); ++i) {
      bigger[rev_index[ind[i]].first] =
          max(bigger[rev_index[ind[i]].first], rev_index[ind[i]].second);
    }
    for (auto it : bigger) {
      if (ans_v.count(it.first)) continue;
      int idx = indexes[it];
      for (int j = 0; j < g[idx].size(); ++j) {
        if (!u[g[idx][j]]) {
          u[g[idx][j]] = 1;
          q.push(g[idx][j]);
        }
      }
      ans.insert(it);
      ans_v.insert(it.first);
    }
  }
  ans.erase({my_project_name, my_v});
  cout << ans.size() << endl;
  for (auto it : ans) {
    cout << it.first << " " << it.second << endl;
  }
  return 0;
}
