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 int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const char dz[4] = {'R', 'U', 'L', 'D'};
int n, m, bs, i, j, xa, ya, xb, yb, xx, yy, dd, d[4], a[1010][1010], g[1010],
    c[1010], x[1010], y[1010], u[1010], f[1010], it;
long long cnt, cur;
char s[5];
int main() {
  scanf("%d%d", &n, &bs);
  for (i = 1; i <= n; i++) {
    scanf("%d%d%d%d", &xa, &ya, &xb, &yb);
    x[i] = xb;
    y[i] = yb;
    if (xa == xb) {
      if (ya > yb) {
        d[i] = 3;
        for (j = yb; j <= ya; j++) a[xa][j] = i;
      } else {
        d[i] = 1;
        for (j = ya; j <= yb; j++) a[xa][j] = i;
      }
    } else {
      if (xa > xb) {
        d[i] = 2;
        for (j = xb; j <= xa; j++) a[j][ya] = i;
      } else {
        d[i] = 0;
        for (j = xa; j <= xb; j++) a[j][ya] = i;
      }
    }
  }
  for (i = 1; i <= n; i++) {
    for (xx = x[i], yy = y[i], dd = d[i];
         xx >= 0 && yy >= 0 && xx <= bs && yy <= bs;
         xx += dx[dd], yy += dy[dd], c[i]++) {
      j = a[xx][yy];
      if (j > 0) {
        dd = d[j];
        if (c[i] > 0 && x[j] == xx && y[j] == yy) break;
      }
    }
    if (xx >= 0 && yy >= 0 && xx <= bs && yy <= bs) g[i] = j;
  }
  scanf("%d", &m);
  while (m--) {
    scanf("%d%d", &xx, &yy);
    scanf("%s", s);
    for (dd = 0; dd < 4; dd++)
      if (dz[dd] == s[0]) break;
    scanf("%I64d", &cnt);
    for (; xx >= 0 && yy >= 0 && xx <= bs && yy <= bs;
         xx += dx[dd], yy += dy[dd], cnt--) {
      if (cnt == 0) break;
      j = a[xx][yy];
      if (j > 0) {
        dd = d[j];
        if (x[j] == xx && y[j] == yy) break;
      }
    }
    if (xx >= 0 && yy >= 0 && xx <= bs && yy <= bs) {
      if (cnt == 0) {
        printf("%d %d\n", xx, yy);
        continue;
      }
    } else {
      printf("%d %d\n", xx - dx[dd], yy - dy[dd]);
      continue;
    }
    for (cur = 0, ++it;; j = g[j]) {
      if (u[j] == it) {
        cnt -= f[j];
        cnt %= cur - f[j];
        cur = 0;
        ++it;
      }
      xx = x[j];
      yy = y[j];
      dd = d[j];
      if (g[j] == 0) break;
      if (cur + c[j] > cnt) {
        cnt -= cur;
        break;
      }
      u[j] = it;
      f[j] = cur;
      cur += c[j];
    }
    for (; xx >= 0 && yy >= 0 && xx <= bs && yy <= bs;
         xx += dx[dd], yy += dy[dd], cnt--) {
      if (cnt == 0) break;
      j = a[xx][yy];
      if (j > 0) dd = d[j];
    }
    if (xx >= 0 && yy >= 0 && xx <= bs && yy <= bs)
      printf("%d %d\n", xx, yy);
    else
      printf("%d %d\n", xx - dx[dd], yy - dy[dd]);
  }
  return 0;
}
