#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 6;
vector<int> g[N];
pair<int, int> in_q[N];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  map<string, vector<int>> proj;
  vector<pair<pair<string, int>, vector<pair<string, int>>>> queries(n);
  pair<string, int> first;
  for (int i = 0; i < n; ++i) {
    cin >> queries[i].first.first >> queries[i].first.second;
    proj[queries[i].first.first].push_back(queries[i].first.second);
    if (!i) first = queries[i].first;
    int k;
    cin >> k;
    queries[i].second.resize(k);
    for (int j = 0; j < k; ++j)
      cin >> queries[i].second[j].first >> queries[i].second[j].second;
  }
  vector<pair<pair<string, int>, int>> anti_dict;
  map<pair<string, int>, int> dict;
  int pr = 0;
  for (auto p : proj) {
    sort(p.second.begin(), p.second.end());
    for (int version : p.second) {
      pair<string, int> cur = make_pair(p.first, version);
      dict[cur] = anti_dict.size();
      anti_dict.push_back(make_pair(cur, pr));
    }
    pr++;
  }
  for (int i = 0; i < n; ++i) {
    int v = dict[queries[i].first];
    for (pair<string, int> p : queries[i].second) g[v].push_back(dict[p]);
  }
  fill(in_q, in_q + N, make_pair(-1, -1));
  queue<int> que;
  int st = dict[first];
  que.push(anti_dict[st].second);
  in_q[anti_dict[st].second] = make_pair(0, st);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    int dist = in_q[v].first + 1;
    for (int to : g[in_q[v].second]) {
      int par = anti_dict[to].second;
      if (in_q[par].first == -1) que.push(par);
      if (in_q[par].first == -1 || in_q[par].first == dist) {
        in_q[par].first = dist;
        in_q[par].second = max(in_q[par].second, to);
      }
    }
  }
  vector<pair<string, int>> ans;
  for (int i = 0; i < pr; ++i) {
    if (in_q[i].first == -1) continue;
    if (in_q[i].second == st) continue;
    ans.push_back(anti_dict[in_q[i].second].first);
  }
  sort(ans.begin(), ans.end());
  cout << ans.size() << "\n";
  ;
  for (auto p : ans) cout << p.first << " " << p.second << "\n";
  ;
}
