#include <bits/stdc++.h>
using namespace std;
int c[1005][1005];
int mm[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
short g[54][50][50][4][2];
char dir[54][50][50][4];
int main() {
  int m, n, i, j, k, h, x1, y1, x2, y2, dx, dy, dd, x, y;
  char str[10];
  long long t;
  memset(c, -1, sizeof(c));
  scanf("%d %d", &m, &n);
  for (i = 0; i < m; i++) {
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
    if (x1 == x2) {
      if (y1 < y2) {
        for (j = y1; j <= y2; j++) {
          c[x1][j] = 1;
        }
      } else {
        for (j = y1; j >= y2; j--) {
          c[x1][j] = 3;
        }
      }
    } else {
      if (x1 < x2) {
        for (j = x1; j <= x2; j++) {
          c[j][y1] = 0;
        }
      } else {
        for (j = x1; j >= x2; j--) {
          c[j][y1] = 2;
        }
      }
    }
  }
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= n; j++) {
      for (k = 0; k < 4; k++) {
        if (c[i][j] == -1) {
          dx = i + mm[k][0];
          dy = j + mm[k][1];
          dd = k;
        } else {
          dx = i + mm[c[i][j]][0];
          dy = j + mm[c[i][j]][1];
          dd = c[i][j];
        }
        if (dx < 0) dx = 0;
        if (dy < 0) dy = 0;
        if (dx > n) dx = n;
        if (dy > n) dy = n;
        g[0][i][j][k][0] = dx;
        g[0][i][j][k][1] = dy;
        dir[0][i][j][k] = dd;
      }
    }
  }
  for (h = 1; h <= 52; h++) {
    for (i = 0; i <= n; i++) {
      for (j = 0; j <= n; j++) {
        for (k = 0; k < 4; k++) {
          x1 = g[h - 1][i][j][k][0];
          y1 = g[h - 1][i][j][k][1];
          dd = dir[h - 1][i][j][k];
          g[h][i][j][k][0] = g[h - 1][x1][y1][dd][0];
          g[h][i][j][k][1] = g[h - 1][x1][y1][dd][1];
          dir[h][i][j][k] = dir[h - 1][x1][y1][dd];
        }
      }
    }
  }
  scanf("%d", &m);
  for (i = 0; i < m; i++) {
    scanf("%d %d %s %I64d", &x, &y, str, &t);
    if (t == 0) {
      printf("%d %d\n", x, y);
      continue;
    }
    if (str[0] == 'L') {
      dd = 2;
    } else if (str[0] == 'R') {
      dd = 0;
    } else if (str[0] == 'D') {
      dd = 3;
    } else {
      dd = 1;
    }
    for (j = 52; j >= 0; j--) {
      if ((1LL << j) <= t) {
        t -= (1LL << j);
        dx = g[j][x][y][dd][0];
        dy = g[j][x][y][dd][1];
        dd = dir[j][x][y][dd];
        x = dx;
        y = dy;
      }
    }
    printf("%d %d\n", x, y);
  }
  return 0;
}
