#include <bits/stdc++.h>
using namespace std;
int ptr;
int n;
string s;
int v;
int dist[1000];
vector<vector<int> > g(1000);
map<pair<string, int>, int> m;
map<int, pair<string, int> > mr;
map<string, int> anotherMap;
map<string, int> picked;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    string name;
    int ver;
    cin >> name >> ver;
    if (!i) s = name, v = ver;
    if (!m.count({name, ver})) {
      mr[ptr] = {name, ver};
      m[{name, ver}] = ptr++;
    }
    int sons;
    cin >> sons;
    int u = m[{name, ver}];
    for (int i = 0; i < sons; i++) {
      cin >> name >> ver;
      if (!m.count({name, ver})) {
        mr[ptr] = {name, ver};
        m[{name, ver}] = ptr++;
      }
      int v = m[make_pair(name, ver)];
      g[u].push_back(v);
    }
  }
  int start = m[{s, v}];
  vector<int> q;
  memset(dist, -1, sizeof dist);
  dist[start] = 0;
  q.push_back(start);
  vector<pair<string, int> > ans;
  vector<int> onDist;
  while (!q.empty()) {
    for (int i = 0; i < q.size(); i++) {
      int U = q[i];
      for (int i = 0; i < g[U].size(); i++) {
        int v = g[U][i];
        pair<string, int> u = mr[v];
        if (dist[v] == -1 && !picked.count(u.first) && u.first != s) {
          dist[v] = dist[U] + 1;
          onDist.push_back(v);
        }
      }
    }
    anotherMap.clear();
    q.clear();
    for (int i = 0; i < onDist.size(); i++) {
      int v = onDist[i];
      pair<string, int> u = mr[v];
      anotherMap[u.first] = max(anotherMap[u.first], u.second);
    }
    for (int i = 0; i < onDist.size(); i++) {
      int v = onDist[i];
      pair<string, int> u = mr[v];
      if (anotherMap[u.first] == u.second) {
        q.push_back(v);
        picked[u.first] = u.second;
      }
    }
    onDist.clear();
  }
  cout << picked.size() << endl;
  for (auto it = picked.begin(); it != picked.end(); it++) {
    cout << it->first << " " << it->second << endl;
  }
}
