#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
vector<vector<pair<string, int>>> adl;
vector<int> dist(2000, inf);
map<pair<string, int>, int> nums;
map<string, pair<int, int>> mins;
void bfs(string& poly, int version) {
  vector<string> order;
  order.push_back(poly);
  mins[poly] = {0, version};
  dist[1] = 0;
  for (int i = 0; i < order.size(); i++) {
    int u = nums[{order[i], mins[order[i]].second}];
    for (auto j : adl[u]) {
      int v = nums[j], ver = j.second;
      string& s = j.first;
      if (dist[v] > dist[u] + 1) {
        dist[v] = dist[u] + 1;
        order.push_back(s);
        if (mins.find(s) == mins.end()) mins[s] = {dist[v], ver};
        if (dist[v] < mins[s].first) {
          mins[s] = {dist[v], ver};
        } else if (dist[v] == mins[s].first) {
          mins[s].second = max(mins[s].second, ver);
        }
      }
    }
  }
}
int main() {
  int n;
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  adl.resize(2 * n);
  int m = 1, v = -1;
  string poly;
  for (int i = 0; i < n; i++) {
    int num, ind, vers;
    string s, s1;
    cin >> s >> num;
    if (i == 0) {
      poly = s;
      v = num;
    }
    if (nums[{s, num}] == 0) {
      nums[{s, num}] = m;
      m++;
    }
    ind = nums[{s, num}];
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      cin >> s1 >> vers;
      if (s == s1) continue;
      adl[ind].push_back({s1, vers});
    }
  }
  bfs(poly, v);
  cout << (int)mins.size() - 1 << '\n';
  for (auto p : mins)
    if (p.first != poly) cout << p.first << ' ' << p.second.second << '\n';
}
