#include <bits/stdc++.h>
using namespace std;
long long n;
map<long long, pair<string, long long> > no_to_node;
map<pair<string, long long>, long long> node_to_no;
vector<vector<long long> > adj;
void faster() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
}
void bfs(long long s) {
  queue<long long> q;
  q.push(s);
  set<pair<string, long long> > st;
  map<string, long long> vs;
  map<long long, long long> dist;
  vector<long long> visited(n + 1, 0);
  visited[s] = 1;
  dist[s] = 0;
  st.insert({no_to_node[s].first, dist[s]});
  vs[no_to_node[s].first] = no_to_node[s].second;
  long long v;
  long long temp;
  while (!q.empty()) {
    v = q.front();
    q.pop();
    for (auto e : adj[v]) {
      if (!visited[e]) {
        dist[e] = dist[v] + 1;
        auto it = find_if(st.begin(), st.end(),
                          [e](const pair<string, long long>& p) {
                            return p.first == no_to_node[e].first;
                          });
        if (it != st.end()) {
          if (dist[e] == it->second) {
            if (vs[it->first] < no_to_node[e].second) {
              if (q.front() == node_to_no[{it->first, vs[it->first]}]) q.pop();
              temp = q.front();
              q.pop();
              q.push(temp);
              while (q.front() != temp) {
                if (q.front() != node_to_no[{it->first, vs[it->first]}]) {
                  q.push(q.front());
                  q.pop();
                } else
                  q.pop();
              }
              vs[it->first] = no_to_node[e].second;
              q.push(e);
            }
          }
        } else {
          st.insert({no_to_node[e].first, dist[e]});
          vs[no_to_node[e].first] = no_to_node[e].second;
          q.push(e);
        }
        visited[e] = 1;
      }
    }
  }
  cout << (long long)((st).size()) - 1 << endl;
  for (auto e : st)
    if (e.first != no_to_node[s].first)
      cout << e.first << " " << vs[e.first] << endl;
}
void solve() {
  cin >> n;
  adj.resize(n + 1);
  string temps, tempsc;
  long long tempn, tempnc;
  long long nod;
  long long ind = 0;
  for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
       i += 1 - 2 * ((0) > (n))) {
    cin >> temps >> tempn;
    if (node_to_no.find({temps, tempn}) == node_to_no.end()) {
      node_to_no.insert({{temps, tempn}, ++ind});
      no_to_node.insert({ind, {temps, tempn}});
    }
    cin >> nod;
    for (__typeof(nod) j = (0) - ((0) > (nod)); j != (nod) - ((0) > (nod));
         j += 1 - 2 * ((0) > (nod))) {
      cin >> tempsc >> tempnc;
      if (node_to_no.find({tempsc, tempnc}) == node_to_no.end()) {
        node_to_no.insert({{tempsc, tempnc}, ++ind});
        no_to_node.insert({ind, {tempsc, tempnc}});
      }
      adj[node_to_no[{temps, tempn}]].emplace_back(
          node_to_no[{tempsc, tempnc}]);
    }
  }
  bfs(1);
}
signed main() {
  faster();
  long long t = 1;
  while (t--) solve();
}
