#include <bits/stdc++.h>
using namespace std;
string get_str(int n) {
  string ans;
  while (n > 0) {
    ans.push_back(char(n % 10 + 48));
    n /= 10;
  }
  reverse(ans.begin(), ans.end());
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, c = 0, x, k, v, ind = 0, ans = 0;
  string st, s;
  cin >> n;
  vector<vector<int> > g(n);
  vector<pair<string, int> > a(n);
  map<string, int> m;
  map<string, pair<int, int> > used;
  set<string> for_ans;
  set<string> for_ans2;
  for (int i = 0; i < n; i++) {
    cin >> s >> x;
    for_ans.insert(s);
    for_ans2.insert(s);
    used[s] = {-1, -1};
    st = s + get_str(x);
    cin >> k;
    if (m[st] == 0) {
      m[st] = c + 1;
      a[c] = {s, x};
      c++;
    }
    v = m[st] - 1;
    for (int j = 0; j < k; j++) {
      cin >> s >> x;
      st = s + get_str(x);
      if (m[st] == 0) {
        m[st] = c + 1;
        a[c] = {s, x};
        c++;
      }
      g[v].push_back(m[st] - 1);
    }
  }
  vector<int> h(n);
  vector<bool> used_v(n, false);
  vector<int> q = {0};
  h[0] = 0;
  used_v[0] = true;
  while (ind != q.size()) {
    v = q[ind];
    x = a[v].second;
    s = a[v].first;
    if (v == 0 || (h[v] == used[s].first && x == used[s].second)) {
      for (int i = 0; i < g[v].size(); i++) {
        if (!used_v[g[v][i]] && a[g[v][i]].first != a[0].first) {
          h[g[v][i]] = h[v] + 1;
          used_v[g[v][i]] = true;
          q.push_back(g[v][i]);
          s = a[g[v][i]].first;
          x = a[g[v][i]].second;
          if (used[s].second == -1 ||
              (used[s].first == h[g[v][i]] && used[s].second < x)) {
            used[s].second = x;
            used[s].first = h[g[v][i]];
          }
        }
      }
    }
    ind++;
  }
  while (!for_ans.empty()) {
    s = *for_ans.begin();
    if (used[s].second != -1) {
      ans++;
    }
    for_ans.erase(s);
  }
  cout << ans << '\n';
  while (!for_ans2.empty()) {
    s = *for_ans2.begin();
    if (used[s].second != -1) {
      cout << s << " " << used[s].second << '\n';
    }
    for_ans2.erase(s);
  }
  return 0;
}
