#include <bits/stdc++.h>
using namespace std;
char base[33][33];
int nx[4][51][33][33];
int ny[4][51][33][33];
int md[4][51][33][33];
int n, b;
int dir(char c) {
  if (c == 'L') return 0;
  if (c == 'U') return 1;
  if (c == 'R') return 2;
  if (c == 'D') return 3;
}
int mov[4][2] = {
    -1, 0, 0, 1, 1, 0, 0, -1,
};
int main() {
  scanf("%d%d", &n, &b);
  for (int i = 0; i <= b; i++)
    for (int j = 0; j <= b; j++) base[i][j] = '.';
  for (int i = 1; i <= n; i++) {
    int x1, x2, y1, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    char fil;
    if (x2 > x1) fil = 'R';
    if (x1 > x2) fil = 'L';
    if (y2 > y1) fil = 'U';
    if (y1 > y2) fil = 'D';
    for (int x = min(x1, x2); x <= max(x1, x2); x++)
      for (int y = min(y1, y2); y <= max(y1, y2); y++) base[x][y] = fil;
  }
  for (int i = 0; i <= b; i++)
    for (int j = 0; j <= b; j++)
      for (int k = 0; k < 4; k++) {
        int x, y, d;
        x = i;
        y = j;
        d = k;
        if (base[x][y] != '.') d = dir(base[x][y]);
        x += mov[d][0];
        y += mov[d][1];
        if (x < 0 || x > b || y < 0 || y > b) {
          nx[k][0][i][j] = i;
          ny[k][0][i][j] = j;
          md[k][0][i][j] = k;
        } else {
          nx[k][0][i][j] = x;
          ny[k][0][i][j] = y;
          md[k][0][i][j] = d;
        }
      }
  for (int lvl = 1; lvl < 51; lvl++)
    for (int i = 0; i <= b; i++)
      for (int j = 0; j <= b; j++)
        for (int k = 0; k < 4; k++) {
          int x, y, d;
          x = nx[k][lvl - 1][i][j];
          y = ny[k][lvl - 1][i][j];
          d = md[k][lvl - 1][i][j];
          nx[k][lvl][i][j] = nx[d][lvl - 1][x][y];
          ny[k][lvl][i][j] = ny[d][lvl - 1][x][y];
          md[k][lvl][i][j] = md[d][lvl - 1][x][y];
        }
  int q;
  scanf("%d", &q);
  while (q--) {
    int x, y, d;
    char inp[10];
    long long tme;
    scanf("%d%d", &x, &y);
    scanf("%s", inp);
    cin >> tme;
    d = dir(inp[0]);
    for (int it = 50; it >= 0; it--)
      if (tme & (1ll << it)) {
        int sx, sy, sd;
        sx = nx[d][it][x][y];
        sy = ny[d][it][x][y];
        sd = md[d][it][x][y];
        x = sx;
        y = sy;
        d = sd;
      }
    printf("%d %d\n", x, y);
  }
}
