#include <bits/stdc++.h>   using namespace std;   class input {    private:     bool fail;     istream* stream;       public:     input(istream& stream_ = cin) { stream = &stream_; }     istream& getStream() { return *stream; }     template <typename T = int>     T read() {       T value;       if (*stream >> value) {         fail = false;       } else {         fail = true;       }       return value;     }     template <typename T = int>     vector<T> readArray() {       size_t size = read<size_t>();       return readArray<T>(size);     }     template <typename T = int>     vector<T> readArray(size_t size) {       vector<T> v(size);       for (size_t i = 0; i < size; i++) {         if (*stream >> v[i]) {           fail = false;         } else {           fail = true;         }       }       return v;     }     bool isFailed() { return fail; }     static input& getStdin() {       static input in(cin);       return in;     }   };   class output {    private:     ostream* stream;       public:     output(ostream& stream_ = cout) { stream = &stream_; }     ostream& getStream() { return *stream; }     template <typename T>     output& put(T& value) {       *stream << value;       return *this;     }     template <typename T>     output& put(T& value, int precision) {       *stream << setprecision(precision) << value;       return *this;     }     output& flush() {       stream->flush();       return *this;     }     template <typename T>     output& write(const T& value) {       *stream << value << endl;       return *this;     }     template <typename T>     output& write(T& value, int precision) {       *stream << setprecision(precision) << fixed << value << endl;       return *this;     }     template <typename T>     output& writeArray(const vector<T>& v, bool appendSize = true) {       if (appendSize) {         write(v.size());       }       for (auto& x : v) {         *stream << x <<    ;       }       *stream << endl;       return *this;     }     output& writeLine() {       *stream << endl;       return *this;     }     output getStdout() {       output out(cout);       return out;     }     output getStderr() {       output err(cerr);       return err;     }   };   namespace std {   template <>   struct hash<pair<string, int>> {     size_t operator()(const pair<string, int>& x) const {       return hash<string>()(x.first) ^ hash<int>()(x.second);     }   };   };  // namespace std   void solve(input in, output out, output err) {     pair<string, int> root{};     unordered_map<pair<string, int>, vector<pair<string, int>>> data;     int n = in.read();     for (int i = 0; i < n; i++) {       string name = in.read<string>();       int ver = in.read();       pair<string, int> x = make_pair(name, ver);       if (root.first.empty()) {         root = x;       }       int x_deps_count = in.read();       vector<pair<string, int>> x_deps(x_deps_count);       for (int j = 0; j < x_deps_count; j++) {         string depName = in.read<string>();         int depVer = in.read();         x_deps[j] = make_pair(depName, depVer);       }       data[x] = x_deps;     }     vector<pair<string, pair<int, int>>> answer;     unordered_map<string, pair<int, int>> info;     queue<string> q;     q.push(root.first);     info[root.first] = make_pair(root.second, 0);     while (!q.empty()) {       auto& cur = q.front();       auto& curInfo = info[cur];       if (cur != root.first) {         answer.emplace_back(cur, curInfo);       }       auto deps = data.find(make_pair(cur, curInfo.first));       if (deps != data.end()) {         for (const auto& next : deps->second) {           pair<int, int> nextInfo = make_pair(next.second, curInfo.second + 1);           auto addPtr = info.emplace(next.first, nextInfo);           if (!addPtr.second) {             if (addPtr.first->second.second == nextInfo.second &&                 addPtr.first->second.first < nextInfo.first) {               info[next.first] = nextInfo;             }           } else {             q.push(next.first);           }         }       }       q.pop();     }     sort(answer.begin(), answer.end());     out.write(answer.size());     for (const auto& x : answer) {       out.put(x.first).put(   ).write(x.second.first);     }   }   int main() {     input in(cin);     output out(cout);     output err(cerr);     while (true) {       auto startT = clock();       solve(in, out, err);       auto endT = clock();       cerr << endl;       cerr <<  Working time:   << (endT - startT) <<   ms  << endl;       cerr <<  -------------------------  << endl;       bool ok = false;       while (!in.isFailed() && !ok) {         ok = in.read<char>() ==  $ ;       }       if (!ok) {         break;       }     }   }