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 inf = (long long)1e17;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const char dir[4] = {'U', 'R', 'D', 'L'};
inline int mabs(int x) { return (x > 0 ? x : -x); }
const int N = 400010;
int arr[N];
long long when[N], dist[N], qt[N];
int pv[55][N];
long long pt[55][N];
int xa[N], ya[N], xb[N], yb[N], arrd[N];
int wx[N], wy[N], wd[N];
int n, b, q;
int what[1010][1010];
void get_next_all() {
  for (int i = 1; i <= n + q; i++) {
    int cx = wx[i], cy = wy[i];
    arr[i] = 0;
    when[i] = 0;
    while (cx >= 0 && cy >= 0 && cx <= b && cy <= b) {
      if (what[cx][cy] != 0) {
        arr[i] = what[cx][cy];
        break;
      }
      when[i]++;
      cx += dx[wd[i]];
      cy += dy[wd[i]];
    }
    if (arr[i] == 0) when[i] = inf;
  }
}
void mark(int xa, int ya, int xb, int yb, int arrow) {
  if (xa > xb) {
    int tmp = xa;
    xa = xb;
    xb = tmp;
  }
  if (ya > yb) {
    int tmp = ya;
    ya = yb;
    yb = tmp;
  }
  for (int i = xa; i <= xb; i++)
    for (int j = ya; j <= yb; j++) what[i][j] = arrow;
}
int main() {
  scanf("%d %d", &n, &b);
  for (int i = 0; i <= b; i++)
    for (int j = 0; j <= b; j++) what[i][j] = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d %d %d %d", xa + i, ya + i, xb + i, yb + i);
    if (yb[i] > ya[i])
      arrd[i] = 0;
    else if (xb[i] > xa[i])
      arrd[i] = 1;
    else if (yb[i] < ya[i])
      arrd[i] = 2;
    else
      arrd[i] = 3;
    wx[i] = xb[i] + dx[arrd[i]];
    wy[i] = yb[i] + dy[arrd[i]];
    wd[i] = arrd[i];
    mark(xa[i], ya[i], xb[i], yb[i], i);
  }
  scanf("%d", &q);
  for (int i = n + 1; i <= n + q; i++) {
    char foo;
    scanf("%d %d %c %I64d", wx + i, wy + i, &foo, qt + i);
    wd[i] = 0;
    for (int j = 0; j < 4; j++)
      if (foo == dir[j]) wd[i] = j;
  }
  get_next_all();
  for (int i = 1; i <= n + q; i++)
    if (arr[i] == 0)
      dist[i] = inf;
    else
      dist[i] = mabs(wx[i] - xb[arr[i]]) + mabs(wy[i] - yb[arr[i]]);
  arr[0] = 0;
  when[0] = inf;
  dist[0] = inf;
  for (int i = 0; i <= n; i++) pv[0][i] = arr[i], pt[0][i] = dist[i] + 1;
  for (int j = 1; j <= 50; j++)
    for (int i = 0; i <= n; i++) {
      pv[j][i] = pv[j - 1][pv[j - 1][i]];
      pt[j][i] = pt[j - 1][i] + pt[j - 1][pv[j - 1][i]];
      if (pt[j][i] > inf) pt[j][i] = inf;
    }
  for (int i = n + 1; i <= n + q; i++) {
    if (qt[i] <= dist[i]) {
      if (qt[i] <= when[i]) {
        long long ax = wx[i] + dx[wd[i]] * qt[i];
        long long ay = wy[i] + dy[wd[i]] * qt[i];
        if (ax < 0) ax = 0;
        if (ay < 0) ay = 0;
        if (ax > b) ax = b;
        if (ay > b) ay = b;
        printf("%d %d\n", (int)ax, (int)ay);
        continue;
      }
      long long ax = wx[i] + dx[wd[i]] * when[i];
      long long ay = wy[i] + dy[wd[i]] * when[i];
      ax += dx[arrd[arr[i]]] * (qt[i] - when[i]);
      ay += dy[arrd[arr[i]]] * (qt[i] - when[i]);
      if (ax < 0) ax = 0;
      if (ay < 0) ay = 0;
      if (ax > b) ax = b;
      if (ay > b) ay = b;
      printf("%d %d\n", (int)ax, (int)ay);
      continue;
    }
    qt[i] -= dist[i];
    int where = arr[i];
    for (int j = 50; j >= 0; j--)
      if (pt[j][where] <= qt[i]) {
        qt[i] -= pt[j][where];
        where = pv[j][where];
      }
    if (arr[where] == 0 || qt[i] <= when[where] + 1) {
      long long ax = xb[where] + dx[arrd[where]] * qt[i];
      long long ay = yb[where] + dy[arrd[where]] * qt[i];
      if (ax < 0) ax = 0;
      if (ay < 0) ay = 0;
      if (ax > b) ax = b;
      if (ay > b) ay = b;
      printf("%d %d\n", (int)ax, (int)ay);
      continue;
    }
    int ax = xb[where] + dx[arrd[where]] * (when[where] + 1);
    int ay = yb[where] + dy[arrd[where]] * (when[where] + 1);
    ax += dx[arrd[arr[where]]] * (qt[i] - (when[where] + 1));
    ay += dy[arrd[arr[where]]] * (qt[i] - (when[where] + 1));
    printf("%d %d\n", ax, ay);
  }
  return 0;
}
