#include <bits/stdc++.h>
using namespace std;
const int MXN = 1e6 + 228;
const int MAXN = 1e3 + 228;
const long long INF = 1e9 + 7;
const long long BINF = 1e15;
const int mod = 1e9 + 7;
const long double EPS = 1e-15;
const long long dx[] = {0, 0, 1, -1};
const long long dy[] = {1, -1, 0, 0};
int n;
string second[MAXN];
int m[MAXN], ver[MAXN];
string a[MAXN][MAXN];
int vr[MAXN][MAXN];
unsigned long long Hash[MAXN];
int num[MAXN];
map<int, int> id[MXN];
map<unsigned long long, int> is;
int used[MXN];
int was[MXN];
vector<int> g[MXN];
vector<int> w;
queue<int> st;
void bfs(int v) {
  st.push(v);
  used[v] = 1;
  while (st.size()) {
    int v = st.front();
    st.pop();
    for (auto to : g[v]) {
      if (!used[num[to]]) {
        if (!was[num[to]]) {
          w.push_back(num[to]);
        }
        was[num[to]] = max(was[num[to]], ver[to]);
      }
    }
    if (!st.size()) {
      for (auto to : w) {
        st.push(id[was[to]][to]);
        used[to] = was[to];
        was[to] = 0;
      }
      w.clear();
    }
  }
}
unsigned long long pw, p = 229;
vector<pair<string, int> > ans;
int o;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> second[i] >> ver[i];
    cin >> m[i];
    for (int j = 1; j <= m[i]; j++) {
      cin >> a[i][j] >> vr[i][j];
    }
    pw = 1;
    for (int j = 0; j < second[i].size(); j++) {
      Hash[i] += pw * (second[i][j] - 'a' + 1);
      pw *= p;
    }
    if (!is[Hash[i]]) {
      is[Hash[i]] = ++o;
    }
    num[i] = is[Hash[i]];
    id[ver[i]][num[i]] = i;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m[i]; j++) {
      unsigned long long H = 0;
      pw = 1;
      for (int k = 0; k < a[i][j].size(); k++) {
        H += pw * (a[i][j][k] - 'a' + 1);
        pw *= p;
      }
      g[i].push_back(id[vr[i][j]][is[H]]);
    }
  }
  bfs(1);
  for (int i = 2; i <= n; i++) {
    if (used[num[i]] == ver[i]) {
      ans.push_back({second[i], ver[i]});
    }
  }
  sort(ans.begin(), ans.end());
  cout << ans.size() << '\n';
  for (auto to : ans) {
    cout << to.first << ' ' << to.second << '\n';
  }
  return 0;
}
