#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1000000000")
using namespace std;
const double PI = 3.14159265358979323846;
const int maxn = (int)2001 + 11;
const long long mod = (long long)1e9 + 7;
const int inf = (int)1e9 + 7;
const double eps = 1e-9;
int n, x, xx, y, k;
string str, ss;
map<int, pair<string, int> > name;
map<pair<string, int>, int> num;
vector<vector<pair<string, int> > > vv(maxn);
vector<vector<int> > v(maxn);
map<string, int> was;
map<string, int> dist;
set<pair<int, int> > s;
vector<pair<string, int> > ans;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> str >> x;
    name[i + 1] = make_pair(str, x);
    num[make_pair(str, x)] = i + 1;
    cin >> k;
    for (int j = 0; j < k; j++) {
      cin >> ss >> xx;
      vv[i + 1].push_back(make_pair(ss, xx));
    }
  }
  for (int i = 0; i < n; i++) {
    x = i + 1;
    for (int j = 0; j < vv[i + 1].size(); j++) {
      y = num[vv[i + 1][j]];
      v[x].push_back(y);
    }
  }
  s.insert(make_pair(0, 1));
  was[name[1].first] = name[1].second;
  dist[name[1].first] = 0;
  while (!s.empty()) {
    int dst = s.begin()->first;
    int pos = s.begin()->second;
    s.erase(s.begin());
    if (dst != 0) {
      ans.push_back(make_pair(name[pos].first, name[pos].second));
    }
    for (int i = 0; i < v[pos].size(); i++) {
      int to = v[pos][i];
      if (was.count(name[to].first)) {
        if (dist[name[to].first] < dst + 1) {
          continue;
        } else {
          if (name[to].second > was[name[to].first]) {
            int tmp = num[make_pair(name[to].first, was[name[to].first])];
            s.erase(make_pair(dst + 1, tmp));
            was[name[to].first] = name[to].second;
            s.insert(make_pair(dst + 1, to));
          }
        }
      } else {
        was[name[to].first] = name[to].second;
        s.insert(make_pair(dst + 1, to));
        dist[name[to].first] = dst + 1;
      }
    }
  }
  sort(ans.begin(), ans.end());
  cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i].first << ' ' << ans[i].second << endl;
  }
  return 0;
}
