#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<pair<string, string>> rules;
string a, b;
while (cin >> a >> b) {
rules.push_back({a, b});
}
bool used[26] = {};
used['S' - 'A'] = true;
for (int i = 0; i < 26; ++i) {
for (auto &p : rules) {
if (p.first.size() == 1 && used[p.first[0] - 'A']) {
for (char c : p.second) {
if (c >= 'A' && c <= 'Z') {
used[c - 'A'] = true;
}
}
}
}
}
for (auto &p : rules) {
if (p.first.size() == 1 && used[p.first[0] - 'A']) {
cout << p.first << " " << p.second << endl;
}
}
return 0;
}