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;
int bel[1007][1007], step[1007][1007], vis[1007][1007];
int mp[1007][1007];
int tox[1007][1007], toy[1007][1007];
struct Node {
  int x1, y1, x2, y2;
  int d;
} arr[1007];
int dir[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
int n, b;
bool inside(int x, int y) { return x >= 0 && y >= 0 && x <= b && y <= b; }
int main() {
  cin >> n >> b;
  int xx, yy, dx, dy, t;
  char s[5];
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d%d%d", &arr[i].x1, &arr[i].y1, &arr[i].x2, &arr[i].y2);
    if (arr[i].y1 > arr[i].y2) arr[i].d = 0;
    if (arr[i].y1 < arr[i].y2) arr[i].d = 1;
    if (arr[i].x1 > arr[i].x2) arr[i].d = 2;
    if (arr[i].x1 < arr[i].x2) arr[i].d = 3;
    for (int x = min(arr[i].x1, arr[i].x2); x <= max(arr[i].x1, arr[i].x2); ++x)
      for (int y = min(arr[i].y1, arr[i].y2); y <= max(arr[i].y1, arr[i].y2);
           ++y)
        bel[x][y] = i;
  }
  for (int i = 1; i <= n; ++i) {
    dx = dir[arr[i].d][0];
    dy = dir[arr[i].d][1];
    xx = arr[i].x2 + dx;
    yy = arr[i].y2 + dy;
    while (inside(xx, yy) && !bel[xx][yy]) xx += dx, yy += dy;
    for (int x = min(arr[i].x1, arr[i].x2); x <= max(arr[i].x1, arr[i].x2); ++x)
      for (int y = min(arr[i].y1, arr[i].y2); y <= max(arr[i].y1, arr[i].y2);
           ++y)
        tox[x][y] = xx, toy[x][y] = yy;
  }
  int m;
  cin >> m;
  while (m--) {
    scanf("%d%d%s%d", &xx, &yy, s, &t);
    switch (s[0]) {
      case 'D':
        dx = dir[0][0], dy = dir[0][1];
        break;
      case 'U':
        dx = dir[1][0], dy = dir[1][1];
        break;
      case 'L':
        dx = dir[2][0], dy = dir[2][1];
        break;
      case 'R':
        dx = dir[3][0], dy = dir[3][1];
        break;
    }
    while (inside(xx, yy) && !bel[xx][yy] && t) xx += dx, yy += dy, --t;
    if (!inside(xx, yy)) {
      xx -= dx, yy -= dy;
      printf("%d %d\n", xx, yy);
      continue;
    }
    if (!t) {
      printf("%d %d\n", xx, yy);
      continue;
    }
    int tx, ty;
    memset(step, -1, sizeof(step));
    while (t) {
      if (!inside(xx, yy)) break;
      tx = tox[xx][yy];
      ty = toy[xx][yy];
      if (step[xx][yy] >= 0)
        t %= step[xx][yy] - t;
      else
        step[xx][yy] = t;
      dx = dy = 0;
      if (tx > xx) dx = 1;
      if (tx < xx) dx = -1;
      if (ty > yy) dy = 1;
      if (ty < yy) dy = -1;
      if (abs(tx - xx) + abs(ty - yy) < t) {
        t -= abs(tx - xx) + abs(ty - yy);
        xx = tx, yy = ty;
      } else
        break;
    }
    if (inside(xx, yy)) {
      xx += t * dx;
      yy += t * dy;
    }
    if (xx < 0) xx = 0;
    if (yy < 0) yy = 0;
    if (xx > b) xx = b;
    if (yy > b) yy = b;
    printf("%d %d\n", xx, yy);
  }
}
