#include <bits/stdc++.h>
using namespace std;
class tree {
 public:
  pair<string, int> split(string s) {
    int left = 0, right = s.size() - 1;
    i = right / 2;
    pair<string, int> a;
    while (s[i] != ' ') {
      if (isalpha(s[i]))
        left = i;
      else
        right = i;
      i = (left + right) / 2;
    }
    a.first = s.substr(0, i);
    a.second = stoi(s.substr(i + 1, s.size()));
    return a;
  }
  void add_name(string s) {
    name++;
    subnames.resize(name + 1);
    names.push_back(split(s));
  }
  void add_subname(string s) {
    pair<string, int> a = split(s);
    if (a.first != names[name].first) addition(subnames[name], a);
  }
  void addition(vector<pair<string, int>> &a, pair<string, int> s) {
    for (int i = 0; i < a.size(); i++)
      if (a[i].first == s.first) {
        if (a[i].second < s.second) a[i].second = s.second;
        return;
      }
    a.push_back(s);
  }
  void get_final() {
    vector<vector<pair<string, int>>> pre_final(1);
    map<string, int> final;
    pre_final[0] = subnames[0];
    for (i = 0; pre_final[i].size(); i++) {
      pre_final.resize(i + 2);
      for (j = 0; j < pre_final[i].size(); j++) {
        final.insert(pre_final[i][j]);
        for (k = 1; k < names.size(); k++)
          if (pre_final[i][j].first == names[k].first) {
            if (pre_final[i][j].second == names[k].second)
              for (pair<string, int> now : subnames[k])
                addition(pre_final[i + 1], now);
            names[k].first = "";
          }
      }
    }
    if (final.find(names[0].first) != final.end())
      final.erase(final.find(names[0].first));
    cout << final.size() << endl;
    for (auto now : final) cout << now.first << ' ' << now.second << endl;
  }

 private:
  int k = 0, name = -1, i, j;
  vector<pair<string, int>> names;
  vector<vector<pair<string, int>>> subnames;
};
int main() {
  string s;
  tree a;
  int n, m;
  getline(cin, s);
  n = stoi(s);
  while (n--) {
    getline(cin, s);
    a.add_name(s);
    getline(cin, s);
    m = stoi(s);
    while (m--) {
      getline(cin, s);
      a.add_subname(s);
    }
    getline(cin, s);
  }
  a.get_final();
  return 0;
}
