#include <bits/stdc++.h>
using namespace std;
string s[1111];
int version[1111], k[1111];
vector<pair<string, int> > dep[1111];
vector<int> g[1111];
map<pair<string, int>, int> ind;
int q1, q2;
int dist[111111], n;
int q[111111], current_dist;
bool invalid_dep[1111111];
bool used[11111];
map<string, int> current_map;
map<string, int> answer_map;
set<string> early_strings;
void update_answer(int i) {
  current_dist = dist[i];
  for (map<string, int>::iterator it = current_map.begin();
       it != current_map.end(); it++) {
    if (answer_map.find(it->first) == answer_map.end()) {
      answer_map[it->first] = it->second;
    }
  }
  current_map.clear();
}
int main() {
  std::ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> s[i] >> version[i];
    ind[make_pair(s[i], version[i])] = i;
    cin >> k[i];
    for (int j = 1; j <= k[i]; ++j) {
      string temp_dep;
      int temp_vers;
      cin >> temp_dep >> temp_vers;
      dep[i].push_back(pair<string, int>(temp_dep, temp_vers));
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < k[i]; ++j) g[i].push_back(ind[dep[i][j]]);
  }
  q[0] = 1;
  dist[0] = 1;
  used[1] = true;
  q1 = 1;
  q2 = 0;
  int previous_dist = 0;
  while (q1 != q2) {
    int p = q[q2];
    if (dist[q2] != previous_dist) {
      for (int i = q2; i < q1; ++i)
        if (early_strings.find(s[q[i]]) != early_strings.end())
          invalid_dep[i] = true;
      for (int i = q2; i < q1; ++i) {
        for (int j = q2; j < q1; ++j)
          if (version[q[i]] > version[q[j]] && s[q[i]] == s[q[j]]) {
            invalid_dep[j] = true;
          }
      }
      previous_dist = dist[q2];
    }
    if (invalid_dep[q2] == false)
      for (int i = 0; i < k[p]; ++i)
        if (!used[g[p][i]]) {
          used[g[p][i]] = true;
          dist[q1] = dist[q2] + 1;
          q[q1] = g[p][i];
          q1++;
        }
    early_strings.insert(s[q[q2]]);
    q2++;
  }
  current_dist = 0;
  for (int i = 0; i < q1; i++) {
    if (dist[i] != current_dist) {
      update_answer(i);
    }
    int cur_vers = current_map[s[q[i]]];
    if (cur_vers < version[q[i]]) {
      current_map[s[q[i]]] = version[q[i]];
    }
  }
  update_answer(0);
  cout << answer_map.size() - 1 << endl;
  for (map<string, int>::iterator it = answer_map.begin();
       it != answer_map.end(); it++) {
    if (it->first != s[1]) cout << it->first << " " << it->second << endl;
  }
  return 0;
}
