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>
const double eps = 1e-9;
const double pi = acos(-1);
const int oo = 1000000000;
const int mod = 1000000007;
const double E = 2.7182818284590452353602874713527;
using namespace std;
int m[1005][1005], f[31][4100001];
int main() {
  int n, b, q;
  cin >> n >> b;
  for (int i = 1; i <= n; i++) {
    int x1, y1, x2, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    if (x1 == x2)
      for (int j = min(y1, y2); j <= max(y1, y2); j++) m[x1][j] = 3 + (y1 < y2);
    else
      for (int j = min(x1, x2); j <= max(x1, x2); j++) m[j][y1] = 1 + (x1 < x2);
  }
  for (int i = 0; i <= b; i++)
    for (int j = 0; j <= b; j++)
      for (int t = 1; t <= 4; t++) {
        int x = i, y = j, z;
        if (m[i][j])
          z = m[i][j];
        else
          z = t;
        if (z == 1)
          x = max(i - 1, 0);
        else if (z == 2)
          x = min(i + 1, b);
        else if (z == 3)
          y = max(j - 1, 0);
        else
          y = min(j + 1, b);
        f[0][(t - 1) * (b + 1) * (b + 1) + i * (b + 1) + j + 1] =
            (z - 1) * (b + 1) * (b + 1) + x * (b + 1) + y + 1;
      }
  for (int i = 1; i <= 30; i++)
    for (int j = 1; j <= (b + 1) * (b + 1) * 4; j++)
      f[i][j] = f[i - 1][f[i - 1][j]];
  cin >> q;
  while (q--) {
    int x, y, z, p, ax, ay;
    long long t;
    char c;
    scanf("%d%d %c%I64d", &x, &y, &c, &t);
    if (c == 'L')
      z = 1;
    else if (c == 'R')
      z = 2;
    else if (c == 'D')
      z = 3;
    else
      z = 4;
    p = (z - 1) * (b + 1) * (b + 1) + x * (b + 1) + y + 1;
    for (int i = 30; i >= 0; i--)
      if (t >= (1 << i)) {
        t -= (1 << i);
        p = f[i][p];
      }
    ax = (p - 1) % ((b + 1) * (b + 1)) / (b + 1);
    ay = (p - 1) % ((b + 1) * (b + 1)) % (b + 1);
    printf("%d %d\n", ax, ay);
  }
  return 0;
}
