#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
void yes() {
  cout << "Yes"
       << "\n";
}
void no() {
  cout << "No"
       << "\n";
}
struct Sync_stdio {
  Sync_stdio() {
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
  }
} _sync_stdio;
struct FAIL {
  FAIL() {
    cout << "CHANGE!!!"
         << "\n";
  }
};
struct Info {
  string name;
  int version;
  int operator<(const Info& other) const {
    return make_tuple(name, version) < make_tuple(other.name, other.version);
  }
};
map<Info, int> project_to_num;
vector<Info> num_to_project;
vector<int> bad;
int get_index(const Info& info) {
  if (project_to_num.find(info) == project_to_num.end()) {
    num_to_project.push_back(info);
    project_to_num[info] = int(num_to_project.size()) - 1;
    return int(num_to_project.size()) - 1;
  }
  return project_to_num[info];
}
vector<vector<int>> g;
vector<int> used;
void read_project() {
  Info info;
  cin >> info.name >> info.version;
  int u = get_index(info);
  int dep;
  cin >> dep;
  for (int i = (0); i < (dep); ++i) {
    cin >> info.name >> info.version;
    int v = get_index(info);
    g[u].push_back(v);
  }
}
struct Z {
  Info info;
  int index;
  int operator<(const Z& other) const {
    return make_tuple(info, index) < make_tuple(other.info, other.index);
  }
};
void drop(int index) { bad[index] = 1; }
void include(const Z& elem, vector<Z>& q) {
  q.push_back(elem);
  int index = elem.index;
  used[index] = 1;
  int n = g.size();
  for (int i = (0); i < (n); ++i) {
    if (i == index) {
      continue;
    }
    if (num_to_project[i].name == num_to_project[index].name) {
      drop(i);
    }
  }
}
void bfs(int x) {
  int n = g.size();
  used.assign(n, 0);
  vector<Z> q;
  include({num_to_project[x], x}, q);
  while (q.size()) {
    vector<Z> new_q;
    for (const auto& info : q) {
      for (int v : g[info.index]) {
        if (used[v] || bad[v]) {
          continue;
        }
        new_q.push_back({num_to_project[v], v});
      }
    }
    q.resize(0);
    sort(new_q.begin(), new_q.end());
    for (int i = (0); i < (int(new_q.size()) - 1); ++i) {
      if (new_q[i].info.name == new_q[i + 1].info.name) {
        drop(new_q[i].index);
      } else {
        include(new_q[i], q);
      }
    }
    if (new_q.size()) {
      include(new_q.back(), q);
    }
  }
}
int main() {
  int n;
  cin >> n;
  g.resize(n);
  bad.resize(n);
  for (int i = (0); i < (n); ++i) {
    read_project();
  }
  bfs(0);
  vector<Info> ans;
  for (int i = (1); i < (n); ++i) {
    if (used[i]) {
      ans.push_back(num_to_project[i]);
    }
  }
  sort(ans.begin(), ans.end());
  cout << ans.size() << "\n";
  for (const auto& info : ans) {
    cout << info.name << " " << info.version << "\n";
  }
}
