3 8
2 4 4 4
7 2 5 2
6 5 6 3
20
3 2 U 1000000
3 2 U 1
3 2 U 2
3 2 U 3
3 2 U 4
3 2 U 5
3 2 U 6
3 2 U 7
3 2 U 8
3 2 U 9
3 2 U 10
3 2 U 11
3 2 U 12
3 2 U 13
3 2 U 14
3 2 U 15
3 2 U 16
3 2 U 17
3 2 U 18
3 2 U 19
 4 2
0 0 1 0
2 0 2 1
2 2 1 2
0 2 0 1
15
1 1 R 7
0 0 U 3
1 1 R 888888888888887
0 0 U 404040404040403
2 1 D 1000000000000000
2 0 R 324354768576473
2 0 L 325436475453245
2 0 U 764536452343654
2 0 D 943654765876545
1 1 U 1000000000000000
1 1 D 989898789898789
1 1 L 123456787654344
0 1 L 0
0 1 L 1
0 1 L 999999999999993
 1 10
1 5 9 5
1
5 5 L 1
 3 7
2 4 2 7
4 7 4 6
7 3 5 3
10
6 7 L 1
6 7 L 2
6 7 L 3
6 7 L 4
6 7 L 5
6 7 L 6
6 7 L 7
6 7 L 8
6 7 L 9
6 7 L 10

                                                               0                                                                2
                                                               3                                                                3
                                                               3                                                                4
                                                               4                                                                4
                                                               5                                                                4
                                                               6                                                                4
                                                               6                                                                3
                                                               6                                                                2
                                                               5                                                                2
                                                               4                                                                2
                                                               3                                                                2
                                                               2                                                                2
                                                               1                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                                1                                                                0
                                                               2                                                                1
                                                               1                                                                0
                                                               2                                                                1
                                                               2                                                                1
                                                               2                                                                1
                                                               0                                                                1
                                                               0                                                                0
                                                               2                                                                1
                                                               2                                                                2
                                                               1                                                                2
                                                               0                                                                2
                                                               0                                                                1
                                                               0                                                                0
                                                               0                                                                0
                                                                6                                                                5
                                                                5                                                                7
                                                               4                                                                7
                                                               4                                                                6
                                                               4                                                                5
                                                               4                                                                4
                                                               4                                                                3
                                                               4                                                                2
                                                               4                                                                1
                                                               4                                                                0
                                                               4                                                                0

#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const unsigned gen_seed =
    std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937_64 gen(gen_seed);
std::vector<int> to;
int b;
int A;
int encode(int x, int y, int d) { return d * A * A + x * A + y; }
std::vector<int> dx, dy;
int main() {
  dx.push_back(1);
  dx.push_back(0);
  dx.push_back(-1);
  dx.push_back(0);
  dy.push_back(0);
  dy.push_back(1);
  dy.push_back(0);
  dy.push_back(-1);
  int n;
  scanf("%d %d", &n, &b);
  A = b + 1;
  to.resize(4 * (b + 1) * (b + 1));
  std::vector<std::vector<int> > f(b + 1, std::vector<int>(b + 1, -1));
  for (int i = 0; i < n; i++) {
    int x0, y0, x1, y1;
    scanf("%d %d %d %d", &x0, &y0, &x1, &y1);
    int dir = 0;
    if (x1 > x0)
      dir = 0;
    else if (y1 > y0)
      dir = 1;
    else if (x1 < x0)
      dir = 2;
    else if (y1 < y0)
      dir = 3;
    if (x0 > x1) swap(x0, x1);
    if (y0 > y1) swap(y0, y1);
    for (int x = x0; x < x1 + 1; x++)
      for (int y = y0; y < y1 + 1; y++) f[x][y] = dir;
  }
  int q;
  scanf("%d", &q);
  std::vector<int> pos(q);
  std::vector<long long> len(q);
  for (int i = 0; i < q; i++) {
    int x, y;
    char c;
    scanf("%d %d %c %lld", &x, &y, &c, &len[i]);
    int dir = 0;
    if (c == 'U')
      dir = 1;
    else if (c == 'L')
      dir = 2;
    else if (c == 'D')
      dir = 3;
    pos[i] = encode(x, y, dir);
  }
  for (int x = 0; x < b + 1; x++)
    for (int y = 0; y < b + 1; y++)
      for (int dir = 0; dir < 4; dir++) {
        int top = dir;
        if (f[x][y] != -1) top = f[x][y];
        int nx = x + dx[top];
        int ny = y + dy[top];
        int cur = encode(x, y, dir);
        if (nx < 0 || nx > b || ny < 0 || ny > b)
          to[cur] = cur;
        else
          to[cur] = encode(nx, ny, top);
      }
  for (int it = 0; it < 60; it++) {
    for (int i = 0; i < q; i++) {
      if (len[i] % 2) pos[i] = to[pos[i]];
      len[i] /= 2;
    }
    std::vector<int> nto(4 * A * A);
    for (int i = 0; i < 4 * A * A; i++) nto[i] = to[to[i]];
    to = std::move(nto);
    nto.clear();
  }
  for (int i = 0; i < q; i++) {
    printf("%d %d\n", (pos[i] / A) % A, pos[i] % A);
  }
}
