#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long INF64 = 1e18;
const int N = 1005;
const int M = 20;
const long long MOD = 1000000007;
const long double Pi = 3.1415926535897932384626433832795;
const long double EPS = 1e-8;
int n;
vector<pair<string, int>> a;
vector<pair<string, int>> rel[N];
map<pair<string, int>, int> ntod;
vector<pair<string, int>> bfs() {
  vector<int> d(n, INF);
  d[0] = 0;
  queue<int> q;
  q.push(0);
  vector<pair<string, int>> ans;
  map<string, pair<int, int>> b_ver;
  for (int i = 0; i < int(n); i++) b_ver[a[i].first] = make_pair(INF, -1);
  b_ver[a[0].first] = make_pair(0, a[0].second);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    pair<int, int> ch = b_ver[a[u].first];
    if (ch.first < d[u] || ch.first == d[u] && ch.second > a[u].second)
      continue;
    if (u != 0) ans.push_back(a[u]);
    for (int i = 0; i < int(int((rel[u]).size())); i++) {
      int to = ntod[rel[u][i]];
      if (d[to] > INF / 2) {
        d[to] = d[u] + 1;
        q.push(to);
        ch = b_ver[a[to].first];
        if (ch.first > d[to] || ch.first == d[to] && ch.second < a[to].second)
          b_ver[a[to].first] = make_pair(d[to], a[to].second);
      }
    }
  }
  sort(ans.begin(), ans.end());
  return ans;
}
void solve() {
  cin >> n;
  a.resize(n);
  char tmp[M];
  for (int i = 0; i < int(n); i++) {
    scanf("%s%d", &tmp, &a[i].second);
    a[i].first = tmp;
    ntod[a[i]] = i;
    int m;
    scanf("%d", &m);
    rel[i].resize(m);
    for (int j = 0; j < int(m); j++) {
      scanf("%s%d", &tmp, &rel[i][j].second);
      rel[i][j].first = tmp;
    }
    scanf("%*c");
  }
  vector<pair<string, int>> ans = bfs();
  cout << int((ans).size()) << endl;
  for (int i = 0; i < int(int((ans).size())); i++) {
    cout << ans[i].first;
    printf(" %d\n", ans[i].second);
  }
}
int main() {
  srand(time(NULL));
  solve();
}
