#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<pair<string, int>> names(n);
  vector<vector<pair<string, int>>> dependencies(n);
  set<pair<string, int>> all_names;
  map<pair<string, int>, int> ind;
  map<pair<string, int>, int> ind_graph;
  for (int i = 0; i < n; ++i) {
    cin >> names[i].first >> names[i].second;
    all_names.insert(names[i]);
    ind_graph[names[i]] = i;
    int k;
    cin >> k;
    dependencies[i].resize(k);
    for (int j = 0; j < k; ++j) {
      cin >> dependencies[i][j].first >> dependencies[i][j].second;
      all_names.insert(dependencies[i][j]);
    }
  }
  int count = 0;
  vector<pair<string, int>> sort_names;
  for (auto &name : all_names) {
    ind[name] = count;
    sort_names.push_back(name);
    ++count;
  }
  vector<int> dist(count, -1);
  dist[ind[names[0]]] = 0;
  queue<pair<string, int>> q;
  q.push(names[0]);
  int last_dist = 0;
  vector<pair<string, int>> ans;
  set<pair<string, int>> access;
  access.insert(names[0]);
  while (!q.empty()) {
    pair<string, int> name = q.front();
    q.pop();
    int d = dist[ind[name]];
    if (d != last_dist) {
      access.clear();
      ++last_dist;
      for (int i = 0; i < count;) {
        int j = i;
        while (j < count && sort_names[j].first == sort_names[i].first) ++j;
        int min_dist = -1;
        for (int k = i; k < j; ++k)
          if (dist[k] != -1) {
            if (min_dist == -1)
              min_dist = dist[k];
            else
              min_dist = min(min_dist, dist[k]);
          }
        if (min_dist == last_dist) {
          for (int k = j - 1; k >= i; --k)
            if (min_dist == dist[k]) {
              ans.push_back(sort_names[k]);
              access.insert(sort_names[k]);
              break;
            }
        }
        i = j;
      }
    }
    if (access.find(name) == access.end()) continue;
    if (ind_graph.find(name) == ind_graph.end()) continue;
    int ii = ind_graph[name];
    for (int i = 0; i < dependencies[ii].size(); ++i) {
      if (dist[ind[dependencies[ii][i]]] == -1) {
        dist[ind[dependencies[ii][i]]] = d + 1;
        q.push(dependencies[ii][i]);
      }
    }
  }
  sort(ans.begin(), ans.end());
  cout << ans.size() << '\n';
  for (int i = 0; i < ans.size(); ++i)
    cout << ans[i].first << ' ' << ans[i].second << '\n';
  return 0;
}
