#include <bits/stdc++.h>
using namespace std;
map<string, int> version;
map<string, int> level;
map<short, vector<short> > g;
map<pair<string, int>, short> id;
map<short, pair<string, int> > idr;
string root;
int root_ver;
short sz = 1;
short get_id(pair<string, int> a) {
  short& cur = id[a];
  if (cur == 0) idr[sz] = a, cur = sz++;
  return cur;
}
void bfs() {
  queue<short> q;
  q.push(1);
  while (!q.empty()) {
    short id = q.front();
    q.pop();
    if (version[idr[id].first] != idr[id].second) continue;
    auto& vec = g[id];
    for (short& to : vec) {
      string name = idr[to].first;
      int cur_ver = idr[to].second;
      if (version[name] == 0 ||
          level[name] == level[idr[id].first] + 1 && version[name] < cur_ver)
        version[name] = cur_ver, level[name] = level[idr[id].first] + 1,
        q.push(to);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    string name;
    int ver;
    cin >> name >> ver;
    short cnt;
    cin >> cnt;
    if (i == 0) root = name, root_ver = ver;
    short id = get_id(make_pair(name, ver));
    auto& vec = g[id];
    while (cnt--) {
      string nn;
      int nver;
      cin >> nn >> nver;
      vec.push_back(get_id(make_pair(nn, nver)));
    }
  }
  version[root] = root_ver;
  bfs();
  cout << version.size() - 1 << '\n';
  for (auto& v : version) {
    if (v.first == root) continue;
    cout << v.first << ' ' << v.second << '\n';
  }
  return 0;
}
