/** * Amida, the City of Miracle (Problem A, Practice Contest for Japan Domestic, 2006-06-18) * by Kenji Inoue, 2007-01-05 **/ #include #include #include using namespace std; typedef multimap< int, pair > HL; // horizontal line int main() { int n, m, a; while (cin >> n >> m >> a, (n || m || a)) { HL hl; set hs; // heights for (int i=0; i> h >> p >> q; hl.insert( make_pair(h, make_pair(p, q)) ); hs.insert(h); } int c = a; // current line for (set::reverse_iterator sit = hs.rbegin(); sit != hs.rend(); sit++) { int h = *sit; for (HL::iterator it = hl.lower_bound(h); it != hl.upper_bound(h); it++) { pair v = it->second; if (v.first == c) { c = v.second; break; } else if (v.second == c) { c = v.first; break; } } } cout << c << endl; } return 0; }