#include <bits/stdc++.h>
struct project {
  std::string name;
  int32_t version;
};
std::istream& operator>>(std::istream& is, project& p) {
  return is >> p.name >> p.version;
}
using graph = std::map<std::string, std::map<int32_t, std::vector<project>>>;
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  size_t n;
  std::cin >> n;
  graph gr;
  project start;
  for (size_t i = 0; i < n; ++i) {
    project base;
    std::cin >> base;
    auto& deps = gr[base.name][base.version];
    size_t k;
    std::cin >> k;
    deps.resize(k);
    for (size_t j = 0; j < k; ++j) {
      std::cin >> deps[j];
    }
    if (i == 0) {
      start = base;
    }
  }
  std::map<std::string, int32_t> result;
  result[start.name] = start.version;
  std::queue<project> q;
  q.push(start);
  while (!q.empty()) {
    std::map<std::string, int32_t> locals;
    while (!q.empty()) {
      project cur = q.front();
      q.pop();
      const auto it = gr.find(cur.name);
      if (it == gr.end()) {
        continue;
      }
      const auto it2 = it->second.find(cur.version);
      if (it2 == it->second.end()) {
        continue;
      }
      for (const auto& p : it2->second) {
        auto& v = locals[p.name];
        v = std::max(v, p.version);
      }
    }
    for (const auto& [k, v] : locals) {
      if (result.insert({k, v}).second) {
        q.push({k, v});
      }
    }
  }
  result.erase(start.name);
  std::cout << result.size() << '\n';
  for (const auto& [k, v] : result) {
    std::cout << k << ' ' << v << '\n';
  }
}
