#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:66777216")
using namespace std;
const int N = 1e6 + 7;
int d[1024], k, ver[1024], ans[1024], num[1024];
map<pair<string, int>, int> M;
map<string, bool> was;
vector<vector<int> > v(1024);
string name[1024];
bool comp(int a, int b) {
  if (name[a] < name[b]) {
    return true;
  }
  if (name[a] > name[b]) {
    return false;
  }
  if (ver[a] > ver[b]) {
    return true;
  }
  return false;
}
void calc() {
  for (int i = 1; i < k; i++) {
    d[i] = -1;
  }
  int l = 0, r = 1;
  vector<int> p;
  was[name[0]] = 1;
  for (int i = 0; i < k; i++) {
    p.clear();
    for (int j = 0; j < k; j++) {
      if (d[j] == i) {
        for (int g = 0; g < v[j].size(); g++) {
          int to = v[j][g];
          if (!was[name[to]] && num[to] == 0) {
            p.push_back(to);
            num[to] = 1;
          }
        }
      }
    }
    sort(p.begin(), p.end(), comp);
    for (int j = 0; j < p.size(); j++) {
      int to = p[j];
      if (!was[name[to]]) {
        was[name[to]] = 1;
        ans[to] = 1;
        d[to] = i + 1;
      }
    }
  }
  set<pair<string, int> > S;
  for (int i = 0; i < k; i++) {
    if (ans[i]) {
      S.insert({name[i], ver[i]});
    }
  }
  cout << S.size() << endl;
  while (S.size()) {
    cout << S.begin()->first << " " << S.begin()->second << endl;
    S.erase(S.begin());
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  string s, ss;
  int c, m, cc;
  for (int i = 0; i < n; i++) {
    cin >> s >> c;
    if (M[{s, c}] == 0) {
      name[k] = s;
      ver[k] = c;
      M[{s, c}] = k++;
    }
    cin >> m;
    for (int j = 0; j < m; j++) {
      cin >> ss >> cc;
      if (M[{ss, cc}] == 0) {
        name[k] = ss;
        ver[k] = cc;
        M[{ss, cc}] = k++;
      }
      v[M[{s, c}]].push_back(M[{ss, cc}]);
    }
  }
  calc();
  cin >> n;
  return 0;
}
