#include <bits/stdc++.h>
const int N = 1100;
const int INF = (int)1e9 + 7;
bool operator<(std::string a, std::string b) {
  for (int i = 0; i < std::min(a.size(), b.size()); ++i) {
    if (a[i] < b[i]) {
      return true;
    }
    if (a[i] > b[i]) {
      return false;
    }
  }
  return a.size() < b.size();
}
std::vector<int> g[N];
std::map<std::pair<std::string, int>, int> projectToV;
std::map<std::string, std::pair<int, int>> answer;
std::pair<std::string, int> vToProject[N];
int d[N];
void addProject(std::string& name, int version) {
  if (projectToV.find(std::make_pair(name, version)) != projectToV.end()) {
    return;
  }
  projectToV.insert(
      std::make_pair(std::make_pair(name, version), projectToV.size()));
  vToProject[projectToV.size() - 1] = std::make_pair(name, version);
}
void bfs(int s) {
  std::queue<int> q;
  q.push(s);
  for (int i = 0; i < N; ++i) {
    d[i] = INF;
  }
  d[s] = 0;
  answer.insert(std::make_pair(vToProject[s].first, std::make_pair(N, 0)));
  while (!q.empty()) {
    int v = q.front();
    std::string curName = vToProject[v].first;
    int curNum = vToProject[v].second;
    q.pop();
    if (v != s && answer[curName].first != curNum) {
      continue;
    }
    for (auto to : g[v]) {
      if (d[to] > d[v] + 1) {
        q.push(to);
        d[to] = d[v] + 1;
        std::string name = vToProject[to].first;
        int version = vToProject[to].second;
        if (answer.find(name) == answer.end()) {
          answer.insert(std::make_pair(name, std::make_pair(version, d[to])));
        } else if (answer[name].second > d[to]) {
          answer[name] = std::make_pair(version, d[to]);
        } else if (answer[name].second == d[to] &&
                   version > answer[name].first) {
          answer[name] = std::make_pair(version, d[to]);
        }
      }
    }
  }
}
int main(void) {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  std::cout.tie(0);
  int n;
  std::cin >> n;
  for (int i = 0; i < n; ++i) {
    std::string curProject = " ";
    do {
      std::cin >> curProject;
    } while (!isalpha(curProject[0]));
    int curVersion, m;
    std::cin >> curVersion >> m;
    addProject(curProject, curVersion);
    int from = projectToV[std::make_pair(curProject, curVersion)];
    for (int j = 0; j < m; ++j) {
      std::string project;
      int version;
      std::cin >> project >> version;
      addProject(project, version);
      int to = projectToV[std::make_pair(project, version)];
      g[from].push_back(to);
    }
  }
  bfs(0);
  answer.erase(vToProject[0].first);
  std::cout << answer.size();
  for (auto i : answer) {
    std::cout << '\n' << i.first << ' ' << i.second.first;
  }
  return 0;
}
