#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:500000000")
using namespace std;
struct Dir {
  int x, y;
  int p;
  Dir() {}
  Dir(int x, int y, int p) : x(x), y(y), p(p) {}
};
Dir u[33][33][4][60];
Dir A[33][33];
int n, b;
void readVec() {
  int x1, y1, x2, y2;
  scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
  if (x1 == x2) {
    if (y1 < y2)
      for (int y = y1; y <= y2; y++) A[x1][y] = Dir(0, 1, 1);
    else
      for (int y = y1; y >= y2; y--) A[x1][y] = Dir(0, -1, 0);
  } else {
    if (x1 < x2)
      for (int x = x1; x <= x2; x++) A[x][y1] = Dir(1, 0, 3);
    else
      for (int x = x1; x >= x2; x--) A[x][y1] = Dir(-1, 0, 2);
  }
}
int main() {
  int i, j, q, x, y, p, t;
  scanf("%d %d", &n, &b);
  for (i = 0; i < n; i++) {
    readVec();
  }
  Dir h[] = {Dir(0, -1, 0), Dir(0, 1, 1), Dir(-1, 0, 2), Dir(1, 0, 3)};
  Dir vec;
  for (x = 0; x <= b; x++) {
    for (y = 0; y <= b; y++) {
      for (p = 0; p < 4; p++) {
        vec = A[x][y].x == 0 && A[x][y].y == 0 ? h[p] : A[x][y];
        Dir &f = u[x][y][p][0];
        f = Dir(x + vec.x, y + vec.y, vec.p);
        if (f.x < 0 || f.y < 0 || f.x > b || f.y > b) f = Dir(x, y, vec.p);
      }
    }
  }
  for (t = 1; t < 60; t++) {
    for (x = 0; x <= b; x++) {
      for (y = 0; y <= b; y++) {
        for (p = 0; p < 4; p++) {
          Dir &nx = u[x][y][p][t - 1];
          u[x][y][p][t] = u[nx.x][nx.y][nx.p][t - 1];
        }
      }
    }
  }
  scanf("%d", &q);
  long long st;
  char cmd[11];
  int idx[128];
  idx['L'] = 2;
  idx['R'] = 3;
  idx['U'] = 1;
  idx['D'] = 0;
  while (q--) {
    scanf("%d %d %s %I64d", &x, &y, cmd, &st);
    if (st == 0) {
      printf("%d %d\n", x, y);
      continue;
    }
    st--;
    Dir p = u[x][y][idx[*cmd]][0];
    for (long long g = 0; g < 60; g++) {
      if (st & (1ll << g)) {
        p = u[p.x][p.y][p.p][(int)g];
      }
    }
    printf("%d %d\n", p.x, p.y);
  }
}
