#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct Person {
string phone;
char seq;
string start, end;
Person(string phone, char seq, string start, string end) : phone(phone), seq(seq), start(start), end(end) {}
};
int main() {
int n;
cin >> n;
vector<Person> persons;
while (n--) {
string phone, start, end;
char seq;
cin >> phone >> seq >> start >> end;
persons.emplace_back(phone, seq, start, end);
}
sort(persons.begin(), persons.end(),
[](const auto &a, const auto &b) { return a.start == b.start ? a.phone < b.phone : a.start < b.start; });
string s;
cin >> s;
vector<Person> res;
for (const auto &person:persons) if (person.phone == s) res.emplace_back(person);
for (const auto &person:persons) {
for (const auto &a:res) {
if (s != person.phone && a.seq == person.seq && a.start <= person.end && a.end >= person.start) {
cout << person.phone << " " << person.seq << " " << person.start << " " << person.end << endl;
break;
}
}
}
return 0;
}