#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.1415926535897932384626433832795l;
template <typename T>
inline auto sqr(T x) -> decltype(x * x) {
  return x * x;
}
template <typename T1, typename T2>
inline bool umx(T1& a, T2 b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2>
inline bool umn(T1& a, T2 b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int N = 1010;
map<string, vector<int> > string_indexes;
map<pair<string, int>, int> project_indexes;
int n;
vector<int> a[N];
const int inf = 1e9;
vector<pair<string, int> > projects;
vector<pair<string, int> > e[N];
map<string, int> ans;
int main() {
  cout.setf(ios::showpoint | ios::fixed);
  cout.precision(20);
  cin >> n;
  projects.resize(n);
  for (int i = int(0); i < int(n); ++i) {
    cin >> projects[i].first >> projects[i].second;
    project_indexes[make_pair(projects[i].first, projects[i].second)] = i;
    string_indexes[projects[i].first].push_back(projects[i].second);
    int k;
    cin >> k;
    for (int j = int(0); j < int(k); ++j) {
      string t;
      int num;
      cin >> t >> num;
      e[i].push_back(make_pair(t, num));
    }
  }
  for (int i = int(0); i < int(n); ++i) {
    for (int j = int(0); j < int(((int)(e[i]).size())); ++j) {
      a[i].push_back(project_indexes[e[i][j]]);
    }
  }
  for (int i = int(0); i < int(n); ++i) {
    ;
  }
  vector<int> tek, next;
  tek.push_back(0);
  ans[projects[0].first] = projects[0].second;
  while (!tek.empty()) {
    for (auto v : tek) {
      for (int i = int(0); i < int(((int)(a[v]).size())); ++i) {
        int to = a[v][i];
        if (ans[projects[to].first] > 0) {
          continue;
        } else {
          next.push_back(to);
        }
      }
    }
    map<string, int> tekans;
    map<string, int> best;
    for (auto to : next) {
      if (umx(tekans[projects[to].first], projects[to].second)) {
        best[projects[to].first] = projects[to].second;
      }
    }
    tek.clear();
    next.clear();
    for (auto it = best.begin(); it != best.end(); ++it) {
      tek.push_back(project_indexes[*it]);
      ans[it->first] = it->second;
    }
  }
  vector<pair<string, int> > ans1;
  for (auto it = ans.begin(); it != ans.end(); ++it) {
    if (it->first == projects[0].first) continue;
    if (it->second > 0) ans1.push_back(make_pair(it->first, it->second));
  }
  cout << ((int)(ans1).size()) << endl;
  for (int i = int(0); i < int(((int)(ans1).size())); ++i) {
    cout << ans1[i].first << ' ' << ans1[i].second << "\n";
  }
  return 0;
}
