#include <bits/stdc++.h>
using namespace std;
const int N = 3333;
vector<pair<string, int>> projs;
int getId(const string &s, int ver) {
  for (int i = 0; i < (int)projs.size(); ++i) {
    if (ver == projs[i].second && s == projs[i].first) {
      return i;
    }
  }
  projs.emplace_back(s, ver);
  return (int)projs.size() - 1;
}
vector<int> g[N];
int depth[N];
map<string, pair<int, int>> mp;
template <class Key, class Val>
ostream &operator<<(ostream &os, const map<Key, Val> &mp) {
  for (const auto &it : mp) {
    os << it.first << ": " << it.second << "\n";
  }
  return os;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "{" << p.first << "," << p.second << "}";
  return os;
}
void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int ver;
    string s;
    cin >> s >> ver;
    int v = getId(s, ver);
    int m;
    cin >> m;
    string ts;
    int tver;
    while (m--) {
      cin >> ts >> tver;
      int u = getId(ts, tver);
      g[v].push_back(u);
    }
  }
  memset(depth, -1, sizeof(depth));
  depth[0] = 0;
  queue<int> q({0});
  mp[projs[0].first] = {0, projs[0].second};
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    if (mp[projs[v].first].second != projs[v].second) {
      continue;
    }
    for (const auto &to : g[v]) {
      if (depth[to] == -1 || depth[to] > depth[v] + 1) {
        depth[to] = depth[v] + 1;
        q.push(to);
      }
      const auto &name = projs[to].first;
      int ver = projs[to].second;
      int dep = depth[to];
      if (!mp.count(name)) {
        mp[name] = {dep, ver};
      } else {
        int d1, v1;
        tie(d1, v1) = mp[name];
        if (dep < d1 || dep == d1 && ver > v1) {
          mp[name] = {dep, ver};
        }
      }
    }
  }
  cout << (int)mp.size() - 1 << "\n";
  for (const auto &it : mp) {
    const auto &name = it.first;
    int ver = it.second.second;
    if (name == projs[0].first) {
      continue;
    }
    cout << name << ' ' << ver << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  solve();
  return 0;
}
