#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 2;
bool vis[N];
string id_name[N];
map<string, int> name_id;
map<pair<int, int>, vector<pair<int, int> > > graph;
int main() {
  ios::sync_with_stdio(0);
  int n, cnt = 0, start_version = 0;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    string name;
    int version;
    cin >> name >> version;
    if (!start_version) start_version = version;
    if (!name_id[name]) {
      name_id[name] = ++cnt;
      id_name[cnt] = name;
    }
    int m;
    cin >> m;
    while (m--) {
      string to_name;
      int to_version;
      cin >> to_name >> to_version;
      if (!name_id[to_name]) {
        name_id[to_name] = ++cnt;
        id_name[cnt] = to_name;
      }
      graph[{version, name_id[name]}].push_back({to_version, name_id[to_name]});
    }
  }
  set<pair<string, int> > ans;
  priority_queue<pair<int, pair<int, int> > > que;
  que.push({0, {start_version, 1}});
  while (!que.empty()) {
    int time = que.top().first, version = que.top().second.first,
        id = que.top().second.second;
    que.pop();
    if (!vis[id]) {
      if (id != 1) ans.insert({id_name[id], version});
      vis[id] = true;
      for (auto to : graph[{version, id}]) que.push({time - 1, to});
    }
  }
  cout << ans.size() << endl;
  for (auto it : ans) cout << it.first << ' ' << it.second << endl;
  return 0;
}
