#include <bits/stdc++.h>
using namespace std;
int n;
map<string, pair<int, int> > ans;
pair<string, pair<int, int> > polproj;
map<string, pair<int, int> >::iterator globit;
queue<pair<int, int> > qbfs;
vector<vector<vector<string> > > g;
void bfs(int line, int level);
int main() {
  iostream::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int indexi, indexj, addNum;
  multimap<string, pair<int, int> > projects;
  multimap<string, pair<int, int> >::iterator it;
  pair<string, int> in;
  cin >> n;
  g.resize(n, vector<vector<string> >(n, vector<string>(3, "0")));
  vector<string> temp;
  for (int i = 0; i < n; i++) {
    cin >> in.first >> in.second;
    if (i == 0) {
      ans.insert(make_pair(in.first, make_pair(in.second, 0)));
      polproj.first = in.first;
      polproj.second.first = in.second;
      polproj.second.second = 0;
    }
    it = projects.find(in.first);
    if (it == projects.end()) {
      projects.insert(
          make_pair(in.first, make_pair(in.second, projects.size())));
      indexi = projects.size() - 1;
    } else {
      indexi = -1;
      while (it->first == in.first) {
        if (it->second.first == in.second) {
          indexi = it->second.second;
          break;
        } else
          it++;
      }
      if (indexi == -1) {
        projects.insert(
            make_pair(in.first, make_pair(in.second, projects.size())));
        indexi = projects.size() - 1;
      }
    }
    cin >> addNum;
    for (int j = 0; j < addNum; j++) {
      cin >> in.first >> in.second;
      it = projects.find(in.first);
      if (it == projects.end()) {
        projects.insert(
            make_pair(in.first, make_pair(in.second, projects.size())));
        indexj = projects.size() - 1;
      } else {
        indexj = -1;
        while (it->first == in.first) {
          if (it->second.first == in.second) {
            indexj = it->second.second;
            break;
          } else
            it++;
        }
        if (indexj == -1) {
          projects.insert(
              make_pair(in.first, make_pair(in.second, projects.size())));
          indexj = projects.size() - 1;
        }
      }
      g[indexi][indexj][0] = "1";
      g[indexi][indexj][1] = in.first;
      g[indexi][indexj][2] = to_string(in.second);
    }
  }
  qbfs.push({0, 0});
  bfs(0, 1);
  if (ans[polproj.first].first == polproj.second.first &&
      ans[polproj.first].second == polproj.second.second)
    ans.erase(polproj.first);
  cout << ans.size() << endl;
  for (globit = ans.begin(); globit != ans.end(); globit++)
    cout << globit->first << ' ' << globit->second.first << endl;
  return 0;
}
void bfs(int line, int level) {
  map<string, pair<int, int> > locm;
  map<string, pair<int, int> >::iterator locit;
  do {
    for (int i = 0; i < n; i++) {
      if (g[line][i][0][0] == '1') {
        locit = locm.find(g[line][i][1]);
        if (locit == locm.end())
          locm.insert(
              make_pair(g[line][i][1], make_pair(stoi(g[line][i][2]), i)));
        else {
          if (locit->second.first < stoi(g[line][i][2])) {
            locit->second.first = stoi(g[line][i][2]);
            locit->second.second = i;
          }
        }
      }
    }
    qbfs.pop();
    line = qbfs.front().first;
    level = qbfs.front().second;
  } while (!qbfs.empty() && qbfs.front().second == level);
  for (locit = locm.begin(); locit != locm.end(); locit++) {
    globit = ans.find(locit->first);
    if (globit == ans.end()) {
      ans.insert(
          make_pair(locit->first, make_pair(locit->second.first, level)));
      qbfs.push(make_pair(locit->second.second, level + 1));
    }
  }
  if (!qbfs.empty()) bfs(qbfs.front().first, qbfs.front().second);
}
