#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int cnt = 0;
  map<pair<string, int>, int> q;
  vector<pair<string, int> > q1(n);
  vector<vector<int> > g(n);
  map<string, pair<int, int> > ans;
  vector<string> w;
  string s;
  int v;
  for (int i = 0; i < n; i++) {
    string s1;
    int v1;
    cin >> s1 >> v1;
    if (i == 0) {
      s = s1;
      v = v1;
    }
    if (!ans.count(s1)) {
      ans[s1] = {-1, -1};
      w.push_back(s1);
    }
    if (!q.count({s1, v1})) {
      q1[cnt] = {s1, v1};
      q[{s1, v1}] = cnt++;
    }
    int n1;
    cin >> n1;
    for (int i1 = 0; i1 < n1; i1++) {
      string s2;
      int v2;
      cin >> s2 >> v2;
      if (!q.count({s2, v2})) {
        q1[cnt] = {s2, v2};
        q[{s2, v2}] = cnt++;
      }
      g[q[{s1, v1}]].push_back(q[{s2, v2}]);
    }
  }
  queue<pair<int, int> > bfs;
  bfs.push({q[{s, v}], 0});
  while (!bfs.empty()) {
    int x = bfs.front().first;
    int num = bfs.front().second;
    bfs.pop();
    if (num % 2 == 1) {
      if (ans[q1[x].first] == make_pair(-1, -1) ||
          ans[q1[x].first].second == num &&
              ans[q1[x].first].first < q1[x].second && q1[x].first != s) {
        ans[q1[x].first] = {q1[x].second, num};
        bfs.push({x, num + 1});
      }
    } else {
      if (num == 0 ||
          ans[q1[x].first].first == q1[x].second && q1[x].first != s)
        for (int i = 0; i < g[x].size(); i++) bfs.push({g[x][i], num + 1});
    }
  }
  sort(w.begin(), w.end());
  int qwe = 0;
  for (int i = 0; i < w.size(); i++) {
    if (ans[w[i]] != make_pair(-1, -1) && w[i] != s) qwe++;
  }
  cout << qwe << endl;
  for (int i = 0; i < w.size(); i++)
    if (ans[w[i]] != make_pair(-1, -1) && w[i] != s)
      cout << w[i] << " " << ans[w[i]].first << endl;
  return 0;
}
