#include <bits/stdc++.h>
template <class T>
inline T qmin(T a, T b) {
  return a < b ? a : b;
}
template <class T>
inline T qmax(T a, T b) {
  return a > b ? a : b;
}
const double eps = 1e-9;
const double PI = acos(-1);
const int inf = 0x3f3f3f3f;
const int mod = (int)1e9 + 7;
const int maxn = (int)1e3 + 5;
const long long INF = 0x3f3f3f3f3f3f3f3f;
using namespace std;
map<pair<string, int>, int> M;
map<int, pair<string, int> > M2;
map<string, bool> M3;
vector<int> G[maxn];
int n, tot;
queue<int> q;
bool vis[maxn];
int now_step;
vector<pair<string, int> > vec;
vector<pair<string, int> > ans;
bool cmp(pair<string, int> p, pair<string, int> q) {
  return p.first == q.first ? p.second > q.second : p.first < q.first;
}
void bfs() {
  q.push(1);
  vis[1] = 1;
  while (!q.empty()) {
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      if (M3.find(M2[u].first) != M3.end()) continue;
      vec.push_back(M2[u]);
    }
    sort(vec.begin(), vec.end(), cmp);
    for (int i = 0; i < (int)vec.size(); i++) {
      while (i != vec.size() - 1 && vec[i].first == vec[i + 1].first)
        vec.erase(vec.begin() + i + 1);
    }
    for (auto x : vec) {
      ans.push_back(x);
      M3[x.first] = 1;
      for (int v : G[M[x]]) {
        if (!vis[v]) {
          q.push(v);
          vis[v] = 1;
        }
      }
    }
    vec.clear();
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  tot = 0;
  for (__typeof(n) i = 1; i <= (n); i++) {
    string s;
    cin >> s;
    int vs;
    cin >> vs;
    int u;
    if (M.find(make_pair(s, vs)) != M.end()) {
      u = M[make_pair(s, vs)];
    } else {
      u = ++tot;
      M[make_pair(s, vs)] = u;
      M2[u] = make_pair(s, vs);
    }
    int m;
    cin >> m;
    while (m--) {
      int v;
      cin >> s >> vs;
      if (M.find(make_pair(s, vs)) != M.end()) {
        v = M[make_pair(s, vs)];
      } else {
        v = ++tot;
        M[make_pair(s, vs)] = v;
        M2[v] = make_pair(s, vs);
      }
      G[u].push_back(v);
    }
  }
  bfs();
  cout << ans.size() - 1 << "\n";
  sort(ans.begin(), ans.end(), cmp);
  for (auto x : ans) {
    if (x == M2[1]) continue;
    cout << x.first << ' ' << x.second << "\n";
  }
  return 0;
}
