#include <bits/stdc++.h>
using namespace std;
vector<pair<string, long long> > Name(1010);
long long n;
vector<vector<pair<string, long long> > > a(1010);
map<string, long long> already;
map<string, long long>::iterator ii;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  long long i, j;
  for (i = 1; i <= n; i++) {
    string w;
    long long version;
    cin >> w >> version;
    if (i == 1) {
      already[w] = version;
    }
    Name[i] = make_pair(w, version);
    long long m;
    cin >> m;
    for (j = 0; j < m; j++) {
      cin >> w >> version;
      a[i].push_back(make_pair(w, version));
    }
  }
  vector<pair<string, long long> > q;
  map<string, long long> pretendent;
  q.push_back(Name[1]);
  while (q.size() > 0) {
    long long qs = 0, qf = q.size();
    vector<pair<string, long long> > temp;
    temp.clear();
    pretendent.clear();
    while (qs < qf) {
      long long cur;
      for (i = 1; i <= n; i++) {
        if (Name[i].first == q[qs].first && Name[i].second == q[qs].second)
          cur = i;
      }
      for (i = 0; i < a[cur].size(); i++) {
        string task = a[cur][i].first;
        long long version = a[cur][i].second;
        if (already[task] == 0)
          pretendent[task] = max(pretendent[task], version);
      }
      qs++;
    }
    q.clear();
    for (ii = pretendent.begin(); ii != pretendent.end(); ii++) {
      string task = ii->first;
      long long version = ii->second;
      already[task] = version;
      q.push_back(make_pair(task, version));
    }
  }
  if (already[Name[1].first] == Name[1].second) already.erase(Name[1].first);
  cout << already.size() << endl;
  for (ii = already.begin(); ii != already.end(); ii++) {
    cout << ii->first << " " << ii->second << endl;
  }
  return 0;
}
