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 int maxn = 300010;
struct NN {
  int x1, y1, x2, y2;
  int x, y;
  int len;
} nn[1100];
int b;
int bel[1010][1010];
int tox[1010][1010], toy[1010][1010];
void init(int n) {
  int i, j, ii, jj;
  for (i = 1; i <= n; i++) {
    for (ii = nn[i].x1, jj = nn[i].y1, j = 0; j < nn[i].len + 1;
         j++, ii += nn[i].x, jj += nn[i].y)
      bel[ii][jj] = i;
  }
  for (i = 1; i <= n; i++) {
    ii = nn[i].x2 + nn[i].x;
    jj = nn[i].y2 + nn[i].y;
    while (ii >= 0 && ii <= b && jj >= 0 && jj <= b && !bel[ii][jj])
      ii += nn[i].x, jj += nn[i].y;
    for (j = 0; j < nn[i].len + 1; j++) {
      tox[nn[i].x1 + nn[i].x * j][nn[i].y1 + nn[i].y * j] = ii;
      toy[nn[i].x1 + nn[i].x * j][nn[i].y1 + nn[i].y * j] = jj;
    }
  }
}
int vis[1010][1010];
long long tim[1010][1010];
int main() {
  int n, i, j, k;
  cin >> n >> b;
  for (i = 1; i <= n; i++) {
    scanf("%d%d%d%d", &nn[i].x1, &nn[i].y1, &nn[i].x2, &nn[i].y2);
    nn[i].len = abs(nn[i].x2 - nn[i].x1) + abs(nn[i].y2 - nn[i].y1);
    if (nn[i].x1 < nn[i].x2)
      nn[i].x = 1;
    else if (nn[i].x1 > nn[i].x2)
      nn[i].x = -1;
    if (nn[i].y1 < nn[i].y2)
      nn[i].y = 1;
    else if (nn[i].y1 > nn[i].y2)
      nn[i].y = -1;
  }
  init(n);
  int m;
  cin >> m;
  int x, y;
  long long t;
  char s[10];
  int xx, yy;
  int woca = 1;
  while (m--) {
    scanf("%d%d%s%I64d", &x, &y, s, &t);
    if (!t) {
      printf("%d %d\n", x, y);
      continue;
    }
    if (s[0] == 'U')
      xx = 0, yy = 1;
    else if (s[0] == 'D')
      xx = 0, yy = -1;
    else if (s[0] == 'L')
      xx = -1, yy = 0;
    else
      xx = 1, yy = 0;
    while (x >= 0 && x <= b && y >= 0 && y <= b && !bel[x][y] && t) {
      x += xx, y += yy;
      t--;
    }
    if (x < 0 || y < 0 || x > b || y > b) {
      x -= xx, y -= yy;
      printf("%d %d\n", x, y);
      continue;
    }
    if (t == 0) {
      printf("%d %d\n", x, y);
      continue;
    }
    woca++;
    int dx = xx, dy = yy;
    while (t) {
      if (x < 0 || y < 0 || x > b || y > b) break;
      if (vis[x][y] == woca)
        t %= tim[x][y] - t;
      else
        vis[x][y] = woca, tim[x][y] = t;
      xx = tox[x][y], yy = toy[x][y];
      dx = dy = 0;
      if (xx > x)
        dx = 1;
      else if (xx < x)
        dx = -1;
      if (yy > y)
        dy = 1;
      else if (yy < y)
        dy = -1;
      if (abs(xx - x) + abs(yy - y) > t) {
        break;
      }
      t -= abs(xx - x) + abs(yy - y);
      x = xx, y = yy;
    }
    if (x < 0 || y < 0 || x > b || y > b) {
      if (x < 0) x++;
      if (y < 0) y++;
      if (x > b) x--;
      if (y > b) y--;
      t = 0;
    }
    int tt = t;
    printf("%d %d\n", x + tt * dx, y + tt * dy);
  }
}
