#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using ll = long long;
using ull = unsigned long long;
using ld = long double;
constexpr ll INF = 2e18;
constexpr ld EPS = 1e-9;
constexpr int MOD = 1e9 + 7;
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
template <typename T1, typename T2>
using safe_map = unordered_map<T1, T2, custom_hash>;
template <typename T1, typename T2>
istream &operator>>(istream &istream, pair<T1, T2> &p) {
  istream >> p.first >> p.second;
  return istream;
}
template <typename T>
istream &operator>>(istream &istream, vector<T> &v) {
  for (auto &it : v) cin >> it;
  return istream;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &ostream, const pair<T1, T2> &p) {
  ostream << p.first << " " << p.second;
  return ostream;
}
template <typename T>
ostream &operator<<(ostream &ostream, const vector<T> &c) {
  for (auto &it : c) cout << it << " ";
  return ostream;
}
template <typename T>
void print(T &&t) {
  cout << t << "\n";
}
template <typename T, typename... Args>
void print(T &&t, Args &&...args) {
  cout << t << " ";
  print(forward<Args>(args)...);
}
template <typename T>
int32_t size_i(T &container) {
  return static_cast<int32_t>(container.size());
}
long long GCD(long long a, long long b) {
  if (!b) return a;
  return GCD(b, a % b);
}
long long LCM(long long a, long long b) { return (a * b) / GCD(a, b); }
long long modpow(ll x, long long n, long long m = MOD) {
  ll res = 1;
  while (n > 0) {
    if (n & 1) res = (res * x) % m;
    x = (x * x) % m;
    n >>= 1;
  }
  return res;
}
long long binpow(long long x, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n & 1) res = res * x;
    x = (x * x);
    n >>= 1;
  }
  return res;
}
long long modinv(long long x, long long m = MOD) { return modpow(x, m - 2, m); }
mt19937 rng;
long long getRandomNumber(long long l, long long r) {
  uniform_int_distribution<long long> dist(l, r);
  return dist(rng);
}
void preSolve() {
  rng = mt19937(chrono::steady_clock::now().time_since_epoch().count());
}
string getUniqueName(string name, long long ver) {
  return name + string(":") + to_string(ver);
}
pair<string, long long> breakName(string name) {
  string a = "", b = "";
  long long i = 0;
  for (; i < name.size(); i++) {
    if (name[i] == ':') break;
    a += name[i];
  }
  for (i = i + 1; i < name.size(); i++) b += name[i];
  return make_pair(a, stoi(b));
}
void solve(long long tc) {
  long long n;
  cin >> n;
  map<string, vector<string>> adj;
  string mainProject = "?";
  for (long long i = 0; i < n; i++) {
    string name;
    long long ver;
    cin >> name >> ver;
    string un = getUniqueName(name, ver);
    if (mainProject == "?") mainProject = un;
    long long d;
    cin >> d;
    while (d--) {
      string na;
      long long v;
      cin >> na >> v;
      adj[un].push_back(getUniqueName(na, v));
    }
  }
  queue<string> q;
  q.push(mainProject);
  map<string, long long> present;
  auto [mpN, mpV] = breakName(mainProject);
  present[mpN] = mpV;
  while (q.size()) {
    long long sz = q.size();
    set<string> toAdd;
    while (sz--) {
      auto curr = q.front();
      q.pop();
      auto [currName, currVer] = breakName(curr);
      auto &adjCurr = adj[curr];
      for (auto &dep : adjCurr) {
        auto [depName, depVer] = breakName(dep);
        if (present.count(depName)) continue;
        toAdd.insert(dep);
      }
    }
    for (auto &dep : toAdd) {
      auto [depName, depVer] = breakName(dep);
      present[depName] = max(present[depName], depVer);
    }
    for (auto &dep : toAdd) {
      auto [depName, depVer] = breakName(dep);
      if (present[depName] != depVer) continue;
      q.push(dep);
    }
  }
  print(present.size() - 1);
  for (auto dep : present) {
    if (mpN == dep.first && mpV == dep.second) continue;
    print(dep);
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(12) << fixed;
  preSolve();
  long long tests = 1;
  for (long long t = 1; t <= tests; t++) solve(t);
  return 0;
}
