#include <bits/stdc++.h>
using namespace std;
map<string, int> zip_name;
map<int, int> zip_version;
map<int, string> unzip_name;
map<int, int> unzip_version;
map<pair<int, int>, vector<pair<int, int> > > g;
const int maxn = 1001;
int was[maxn];
vector<pair<int, int> > ans;
void bfs(pair<int, int> vert) {
  priority_queue<pair<int, pair<int, int> > > q;
  q.push({0, {vert.second, vert.first}});
  while (not q.empty()) {
    auto tmp = q.top();
    q.pop();
    auto v = tmp.second;
    int depth = -tmp.first;
    if (was[v.second]) continue;
    ans.push_back({v.second, v.first});
    was[v.second] = 1;
    for (auto x : g[{v.second, v.first}]) {
      if (!was[x.first]) {
        pair<int, int> newx = {x.second, x.first};
        q.push({-depth - 1, newx});
      }
    }
  }
}
map<pair<string, int>, vector<pair<string, int> > > input;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  pair<string, int> start_proj;
  while (n--) {
    string proj;
    int version;
    cin >> proj;
    cin >> version;
    if (start_proj.first == "") {
      start_proj = make_pair(proj, version);
    }
    input[make_pair(proj, version)];
    zip_name[proj];
    zip_version[version];
    auto vert = input.find(make_pair(proj, version));
    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
      string dep_proj;
      int dep_version;
      cin >> dep_proj;
      cin >> dep_version;
      zip_name[dep_proj];
      zip_version[dep_version];
      vert->second.push_back(make_pair(dep_proj, dep_version));
    }
  }
  int cnt = 1;
  for (auto& x : zip_name) {
    x.second = cnt++;
    unzip_name[x.second] = x.first;
  }
  cnt = 1;
  for (auto& x : zip_version) {
    x.second = cnt++;
    unzip_version[x.second] = x.first;
  }
  for (auto x : input) {
    pair<int, int> tmp =
        make_pair(zip_name[x.first.first], zip_version[x.first.second]);
    g[tmp];
    auto vert = g.find(tmp);
    for (auto y : x.second) {
      vert->second.push_back(
          make_pair(zip_name[y.first], zip_version[y.second]));
    }
    sort(vert->second.begin(), vert->second.end(), greater<pair<int, int> >());
  }
  bfs(make_pair(zip_name[start_proj.first], zip_version[start_proj.second]));
  cout << ans.size() - 1 << endl;
  sort(ans.begin() + 1, ans.end());
  for (int i = 1; i < ans.size(); i++) {
    cout << unzip_name[ans[i].first] << ' ' << unzip_version[ans[i].second]
         << endl;
  }
}
