#include <bits/stdc++.h>
using namespace std;
string get_string(int delim) {
  int a;
  string ret;
  while ((a = getchar()) != delim && a != EOF) ret += a;
  return ret;
}
pair<string, int> first;
map<pair<string, int>, vector<pair<string, int> > > g;
map<pair<string, int>, int> d;
map<string, int> res;
void bfs(vector<pair<string, int> > now) {
  if (now.empty()) return;
  vector<pair<string, int> > nxt;
  for (auto &u : now) {
    for (auto &v : g[u]) {
      if (!d.count(v) && !res.count(v.first)) {
        d[v] = d[u] + 1;
        nxt.push_back(v);
      }
    }
  }
  sort(nxt.begin(), nxt.end());
  vector<pair<string, int> > nxt1;
  for (int i = 0; i < nxt.size(); i++) {
    if (i + 1 == nxt.size() || nxt[i].first != nxt[i + 1].first) {
      nxt1.push_back(nxt[i]);
      res[nxt[i].first] = nxt[i].second;
    }
  }
  bfs(nxt1);
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    while (s.empty() || s[0] < 'a' || s[0] > 'z') cin >> s;
    int a;
    cin >> a;
    if (!i) {
      first = {s, a};
      res[s] = a;
    }
    int m;
    cin >> m;
    for (int j = 0; j < m; j++) {
      string x;
      int e;
      cin >> x >> e;
      g[{s, a}].push_back({x, e});
    }
  }
  d[first] = 0;
  bfs({first});
  int r = res.size();
  cout << r - 1 << endl;
  for (auto p : res) {
    if (p.first != first.first) {
      cout << p.first << ' ' << p.second << endl;
    }
  }
  return 0;
}
