#include <algorithm>
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <tuple>
#include <vector>
#define REP(i, n) for (int i = 0; (i) < int(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < int(n); ++ (i))
#define ALL(x) begin(x), end(x)
using namespace std;

const int dy[] = { -1, 1, 0, 0 };
const int dx[] = { 0, 0, 1, -1 };
const char *dc = "SNEW";
const char *rev_dc = "NSWE";
int index_of(char c) { return strchr(dc, c) - dc; }

constexpr int H = 103;
constexpr int W = 103;
int main() {
    // input
    int n, x0, y0, t; scanf("%d%d%d%d", &n, &x0, &y0, &t);
    x0 = 2 * x0 + 1;
    y0 = 2 * y0 + 1;
    array<array<bool, W>, H> f = {};
    while (n --) {
        int xs, ys, xe, ye; scanf("%d%d%d%d", &xs, &ys, &xe, &ye);
        xs = 2 * xs + 1;
        ys = 2 * ys + 1;
        xe = 2 * xe + 1;
        ye = 2 * ye + 1;
        if (xs == xe) {
            REP3 (y, min(ys, ye), max(ys, ye) + 1) {
                f[xs][y] = true;
            }
        } else if (ys == ye) {
            REP3 (x, min(xs, xe), max(xs, xe) + 1) {
                f[x][ys] = true;
            }
        } else {
            assert (false);
        }
    }
    vector<tuple<int, int, char, char> > poss;
    REP (i, 4) {
        if (not f[x0 + dx[i]][y0 + dy[i]]) continue;
        poss.emplace_back(x0, y0, '*', dc[i]);
    }
    while (t --) {
        int d; char c; scanf("%d %c", &d, &c);
        // solve
        while (d --) {
            vector<tuple<int, int, char, char> > next_poss;
            for (auto pos : poss) {
                int x, y; char dc_i; tie(x, y, ignore, dc_i) = pos;
                int i = index_of(dc_i);
                if (not f[x + dx[i]][y + dy[i]]) continue;
                int nx = x + 2 * dx[i];
                int ny = y + 2 * dy[i];
                REP (j, 4) if (rev_dc[j] != dc[i]) {
                    if (i != j and not f[nx + dx[j]][ny + dy[j]]) continue;
                    next_poss.emplace_back(nx, ny, dc[i], dc[j]);
                }
            }
            poss = next_poss;
            sort(ALL(poss));
            poss.erase(unique(ALL(poss)), poss.end());
        }
        poss.erase(remove_if(ALL(poss), [&](tuple<int, int, char, char> a) {
            char c1, c2; tie(ignore, ignore, c1, c2) = a;
            return c1 != c and c2 != c;
        }), poss.end());
    }
    // output
    sort(ALL(poss));
    poss.erase(unique(ALL(poss), [](tuple<int, int, char, char> a, tuple<int, int, char, char> b) {
        return get<0>(a) == get<0>(b) and get<1>(a) == get<1>(b);
    }), poss.end());
    for (auto pos : poss) {
        printf("%d %d\n", (get<0>(pos) - 1) / 2, (get<1>(pos) - 1) / 2);
    }
    return 0;
}
### Prompt

Please provide a CPP coded solution to the problem described below:

Example

Input

4 2 1 1
1 1 1 2
2 2 2 1
2 2 1 2
1 1 2 1
9 N


Output

1 1
2 2

### Response

```cpp
#include <algorithm>
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <tuple>
#include <vector>
#define REP(i, n) for (int i = 0; (i) < int(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < int(n); ++ (i))
#define ALL(x) begin(x), end(x)
using namespace std;

const int dy[] = { -1, 1, 0, 0 };
const int dx[] = { 0, 0, 1, -1 };
const char *dc = "SNEW";
const char *rev_dc = "NSWE";
int index_of(char c) { return strchr(dc, c) - dc; }

constexpr int H = 103;
constexpr int W = 103;
int main() {
    // input
    int n, x0, y0, t; scanf("%d%d%d%d", &n, &x0, &y0, &t);
    x0 = 2 * x0 + 1;
    y0 = 2 * y0 + 1;
    array<array<bool, W>, H> f = {};
    while (n --) {
        int xs, ys, xe, ye; scanf("%d%d%d%d", &xs, &ys, &xe, &ye);
        xs = 2 * xs + 1;
        ys = 2 * ys + 1;
        xe = 2 * xe + 1;
        ye = 2 * ye + 1;
        if (xs == xe) {
            REP3 (y, min(ys, ye), max(ys, ye) + 1) {
                f[xs][y] = true;
            }
        } else if (ys == ye) {
            REP3 (x, min(xs, xe), max(xs, xe) + 1) {
                f[x][ys] = true;
            }
        } else {
            assert (false);
        }
    }
    vector<tuple<int, int, char, char> > poss;
    REP (i, 4) {
        if (not f[x0 + dx[i]][y0 + dy[i]]) continue;
        poss.emplace_back(x0, y0, '*', dc[i]);
    }
    while (t --) {
        int d; char c; scanf("%d %c", &d, &c);
        // solve
        while (d --) {
            vector<tuple<int, int, char, char> > next_poss;
            for (auto pos : poss) {
                int x, y; char dc_i; tie(x, y, ignore, dc_i) = pos;
                int i = index_of(dc_i);
                if (not f[x + dx[i]][y + dy[i]]) continue;
                int nx = x + 2 * dx[i];
                int ny = y + 2 * dy[i];
                REP (j, 4) if (rev_dc[j] != dc[i]) {
                    if (i != j and not f[nx + dx[j]][ny + dy[j]]) continue;
                    next_poss.emplace_back(nx, ny, dc[i], dc[j]);
                }
            }
            poss = next_poss;
            sort(ALL(poss));
            poss.erase(unique(ALL(poss)), poss.end());
        }
        poss.erase(remove_if(ALL(poss), [&](tuple<int, int, char, char> a) {
            char c1, c2; tie(ignore, ignore, c1, c2) = a;
            return c1 != c and c2 != c;
        }), poss.end());
    }
    // output
    sort(ALL(poss));
    poss.erase(unique(ALL(poss), [](tuple<int, int, char, char> a, tuple<int, int, char, char> b) {
        return get<0>(a) == get<0>(b) and get<1>(a) == get<1>(b);
    }), poss.end());
    for (auto pos : poss) {
        printf("%d %d\n", (get<0>(pos) - 1) / 2, (get<1>(pos) - 1) / 2);
    }
    return 0;
}
```