#include <bits/stdc++.h>
using namespace std;
const double pi = atan2(0, -1);
const int inf = 10000000;
int n;
vector<pair<string, int> > projects;
map<pair<string, int>, int> num;
vector<vector<pair<string, int> > > a;
vector<vector<int> > mat;
vector<int> dist, allowed;
queue<int> q;
bool Comp(pair<int, int> p1, pair<int, int> p2) {
  if (p1.first < p2.first) return true;
  if (p1.first == p2.first &&
      projects[p1.second].second >= projects[p2.second].second)
    return true;
  return false;
}
bool bfs() {
  q.push(0);
  for (int i = 1; i < n; i++) dist[i] = inf;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < mat[v].size(); i++)
      if (dist[mat[v][i]] == inf && allowed[mat[v][i]]) {
        dist[mat[v][i]] = dist[v] + 1;
        q.push(mat[v][i]);
      }
  }
  vector<pair<int, int> > arr;
  map<string, int> temp;
  for (int i = 0; i < n; i++)
    if (dist[i] < inf) {
      arr.push_back(make_pair(dist[i], i));
      if (temp.find(projects[i].first) == temp.end())
        temp[projects[i].first] = 1;
      else
        temp[projects[i].first]++;
    }
  sort(arr.begin(), arr.end(), Comp);
  for (int i = 0; i < arr.size(); i++)
    if (temp[projects[arr[i].second].first] > 1) {
      for (int j = i + 1; j < arr.size(); j++)
        if (projects[arr[j].second].first == projects[arr[i].second].first)
          allowed[arr[j].second] = 0;
      return true;
    }
  return false;
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin >> n;
  projects.resize(n);
  a.resize(n);
  mat.resize(n);
  dist.resize(n);
  allowed = vector<int>(n, 1);
  dist[0] = 0;
  for (int i = 0; i < n; i++) {
    cin >> projects[i].first >> projects[i].second;
    num[make_pair(projects[i].first, projects[i].second)] = i;
    int x;
    cin >> x;
    a[i].resize(x);
    for (int j = 0; j < x; j++) cin >> a[i][j].first >> a[i][j].second;
  }
  for (int i = 0; i < n; i++) {
    mat[i].resize(a[i].size());
    for (int j = 0; j < mat[i].size(); j++) mat[i][j] = num[a[i][j]];
  }
  while (bfs()) {
  }
  set<pair<string, int> > ans;
  for (int i = 1; i < n; i++)
    if (dist[i] < inf) ans.insert(projects[i]);
  cout << ans.size() << endl;
  for (set<pair<string, int> >::iterator it = ans.begin(); it != ans.end();
       it++)
    cout << it->first << " " << it->second << endl;
  return 0;
}
