#include <bits/stdc++.h>
using namespace std;
template <class T>
T sqr(const T &a) {
  return a * a;
}
const int MAXN = 1010;
string name[MAXN];
int ver[MAXN];
vector<pair<string, int> > deps[MAXN];
vector<int> realDep[MAXN];
map<string, int> d, curVersion;
map<pair<string, int>, int> nameAndVer;
string q[MAXN];
int tail, head;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < (n); i++) {
    cin >> name[i] >> ver[i];
    nameAndVer[{name[i], ver[i]}] = i;
    int m;
    cin >> m;
    for (int j = 0; j < (m); j++) {
      string t;
      int v;
      cin >> t >> v;
      deps[i].push_back({t, v});
    }
  }
  for (int i = 0; i < (n); i++) {
    for (auto to : deps[i]) {
      assert(nameAndVer.count(to));
      realDep[i].push_back(nameAndVer[to]);
    }
  }
  curVersion[name[0]] = ver[0];
  q[head++] = name[0];
  d[name[0]] = 0;
  vector<pair<string, int> > ans;
  while (head > tail) {
    string myName = q[tail++];
    int myVersion = curVersion[myName];
    int myDist = d[myName];
    if (myDist != 0) {
      ans.push_back({myName, myVersion});
    }
    assert(nameAndVer.count({myName, myVersion}));
    int v = nameAndVer[{myName, myVersion}];
    for (const int to : realDep[v]) {
      int dist;
      if (d.count(name[to])) {
        dist = d[name[to]];
      } else {
        dist = 1e9;
      }
      if (dist > myDist + 1 ||
          (dist == myDist + 1 && ver[to] > curVersion[name[to]])) {
        if (!d.count(name[to])) {
          q[head++] = name[to];
        }
        curVersion[name[to]] = ver[to];
        d[name[to]] = myDist + 1;
      }
    }
  }
  sort((ans).begin(), (ans).end());
  cout << ans.size() << "\n";
  for (auto x : ans) {
    cout << x.first << " " << x.second << "\n";
  }
}
