#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2222;
const int INF = 1e9;
struct vertex {
  string name;
  int version;
};
bool operator<(vertex a, vertex b) {
  return (a.name < b.name || a.name == b.name && a.version > b.version);
}
vertex a[MAXN];
map<vertex, int> mp;
map<string, pair<int, int> > ans;
int n;
int cnt;
int l, r, kol;
int st[MAXN * MAXN];
int dist[MAXN];
vector<int> num[MAXN];
vector<vertex> vec[MAXN];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  string s;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i].name >> a[i].version;
    cin >> cnt;
    vec[i].resize(cnt);
    for (int j = 0; j < cnt; ++j) {
      cin >> vec[i][j].name >> vec[i][j].version;
    }
    mp[a[i]] = i;
  }
  for (int i = 1; i <= n; ++i) {
    num[i].resize(vec[i].size());
    for (int j = 0; j < num[i].size(); ++j) {
      num[i][j] = mp[vec[i][j]];
    }
  }
  for (int i = 2; i <= n; ++i) dist[i] = INF, ans[a[i].name] = {-1, -1};
  ans[a[1].name] = {0, a[1].version};
  st[1] = 1;
  l = r = kol = 1;
  while (kol) {
    kol = 0;
    for (int i = l; i <= r; ++i) {
      int v = st[i];
      if (ans[a[v].name].second != a[v].version) continue;
      for (int j = 0; j < num[v].size(); ++j) {
        int to = num[v][j];
        if (dist[v] + 1 < dist[to]) {
          dist[to] = dist[v] + 1;
          pair<int, int> P = ans[a[to].name];
          if (dist[to] < P.first || (P.first == -1) ||
              (dist[to] == P.first && a[to].version > P.second))
            ans[a[to].name] = {dist[to], a[to].version};
          ++kol;
          st[r + kol] = to;
        }
      }
    }
    l = r + 1;
    r += kol;
  }
  for (int i = 1; i <= n; ++i)
    if (dist[i] < INF) {
      pair<int, int> p = ans[a[i].name];
      if (p.first == -1 || (p.first > dist[i]) ||
          (p.first == dist[i] && p.second < a[i].version))
        ans[a[i].name] = {dist[i], a[i].version};
    }
  vector<vertex> answer;
  for (int i = 2; i <= n; ++i)
    if (make_pair(dist[i], a[i].version) == ans[a[i].name]) {
      answer.push_back(a[i]);
    }
  sort(answer.begin(), answer.end());
  cout << (int)answer.size() << '\n';
  for (int i = 0; i < answer.size(); ++i)
    cout << answer[i].name << ' ' << answer[i].version << '\n';
  return 0;
}
