#include <bits/stdc++.h>
using namespace std;
const int Z = (int)1e3 + 111;
vector<int> g[Z];
map<pair<string, int>, int> zip;
map<int, pair<string, int> > unZip;
int n, cnt;
void make(string s, int f) {
  if (!zip[make_pair(s, f)]) {
    zip[make_pair(s, f)] = ++cnt;
    unZip[cnt] = make_pair(s, f);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    string s1, s2;
    int f1, f2;
    cin >> s1 >> f1;
    make(s1, f1);
    int k;
    cin >> k;
    while (k--) {
      cin >> s2 >> f2;
      make(s2, f2);
      int u = zip[make_pair(s1, f1)];
      int v = zip[make_pair(s2, f2)];
      g[u].push_back(v);
    }
  }
  queue<int> q;
  q.push(1);
  map<string, int> used;
  used[unZip[1].first] = 1;
  vector<pair<string, int> > ans;
  for (int iterations = 1; iterations <= 1000 + 228; ++iterations) {
    map<string, int> cur;
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (int to : g[v]) {
        pair<string, int> x = unZip[to];
        if (used[x.first]) continue;
        if (!cur[x.first]) {
          cur[x.first] = x.second;
        } else {
          cur[x.first] = max(cur[x.first], x.second);
        }
      }
    }
    for (auto x : cur) {
      ans.push_back(make_pair(x.first, x.second));
      used[x.first] = 1;
      q.push(zip[make_pair(x.first, x.second)]);
    }
  }
  sort((ans).begin(), (ans).end());
  cout << (int)ans.size() << '\n';
  for (auto i : ans) {
    cout << i.first << ' ' << i.second << '\n';
  }
  return 0;
}
