#include <bits/stdc++.h>
using namespace std;
void set_precise(int n) {
  cout << fixed;
  cout << setprecision(n);
}
map<pair<string, int>, int> m;
pair<string, int> prs[1003];
vector<int> g[1003];
void add_proj(pair<string, int> p, int &cnt_m) {
  m[p] = cnt_m;
  prs[cnt_m] = p;
  ++cnt_m;
}
pair<string, int> read_graph(int n) {
  int cnt_m = 0;
  pair<string, int> root;
  for (int i = 0; i < n; ++i) {
    if (i == 0) {
      string s;
      int num;
      cin >> s >> num;
      root = make_pair(s, num);
      add_proj(root, cnt_m);
      int k;
      cin >> k;
      int a = m[root];
      for (int j = 0; j < k; ++j) {
        string t;
        int cur_num;
        cin >> t >> cur_num;
        pair<string, int> cur_proj = make_pair(t, cur_num);
        if (m.count(cur_proj)) {
          int b = m[cur_proj];
          g[a].push_back(b);
        } else {
          add_proj(cur_proj, cnt_m);
          int b = m[cur_proj];
          g[a].push_back(b);
        }
      }
    } else {
      string s;
      int num;
      cin >> s >> num;
      pair<string, int> p = make_pair(s, num);
      if (!m.count(p)) {
        add_proj(p, cnt_m);
      }
      int k;
      cin >> k;
      int a = m[p];
      for (int i = 0; i < k; ++i) {
        string t;
        int cur_num;
        cin >> t >> cur_num;
        pair<string, int> cur_proj = make_pair(t, cur_num);
        if (m.count(cur_proj)) {
          int b = m[cur_proj];
          g[a].push_back(b);
        } else {
          add_proj(cur_proj, cnt_m);
          int b = m[cur_proj];
          g[a].push_back(b);
        }
      }
    }
  }
  return root;
}
set<string> used_pr;
vector<int> from;
vector<pair<string, int> > to;
int used[1003] = {0};
vector<pair<string, int> > ans;
int main() {
  int n;
  cin >> n;
  pair<string, int> root = read_graph(n);
  used_pr.insert(root.first);
  from.push_back(0);
  used[0] = 1;
  while (from.size() > 0) {
    to.resize(0);
    for (int i = 0; i < from.size(); ++i) {
      int cur = from[i];
      for (int j = 0; j < g[cur].size(); ++j) {
        int next = g[cur][j];
        if (!used[next]) {
          used[next] = 1;
          pair<string, int> next_proj = prs[next];
          if (!used_pr.count(next_proj.first)) {
            to.push_back(next_proj);
          }
        }
      }
    }
    from.resize(0);
    sort(to.rbegin(), to.rend());
    for (int i = 0; i < to.size(); ++i) {
      if ((i == 0) || (to[i].first != to[i - 1].first)) {
        int proj_gr = m[to[i]];
        from.push_back(proj_gr);
        used_pr.insert(to[i].first);
        ans.push_back(to[i]);
      }
    }
  }
  sort(ans.begin(), ans.end());
  cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); ++i) {
    cout << ans[i].first << " " << ans[i].second << endl;
  }
  return 0;
}
