<code class="language-cpp">int main() { cin.tie(0)->sync_with_stdio(0); cout.tie(0); int n, k; cin >> n >> k; while (k--) { int x, y; cin >> x >> y; string op; cin >> op; for (int i = 0; i < op.size(); i++) { int tx = x, ty = y; if (op[i] == 'f') ty++; else if (op[i] == 'b') ty--; else if (op[i] == 'l') tx--; else tx++; if (tx >= 1 and tx <= n and ty >= 1 and ty <= n) { x = tx, y = ty; } } cout << x << ' ' << y << '\n'; } } </code>