#include <bits/stdc++.h>
using namespace std;
int x[5100], y[5100];
int vis[33][33][5];
int md[33][33];
int n, b, q;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int ask(int xx, int yy, int dd, long long tt) {
  memset(vis, -1, sizeof(vis));
  vis[xx][yy][dd] = 0;
  x[0] = xx;
  y[0] = yy;
  for (int px = xx, py = yy, pd = dd, i = 1; i <= tt + 1; ++i) {
    if (i > tt) return i - 1;
    if (md[px][py] != -1) pd = md[px][py];
    px = px + dx[pd];
    py = py + dy[pd];
    if (px < 0 || px > b) return i - 1;
    if (py < 0 || py > b) return i - 1;
    if (vis[px][py][pd] != -1) {
      int circle = i - vis[px][py][pd];
      return (tt - vis[px][py][pd]) % circle + vis[px][py][pd];
    }
    vis[px][py][pd] = i;
    x[i] = px;
    y[i] = py;
  }
  return 0;
}
void init() {
  scanf("%d %d", &n, &b);
  int ax, ay, bx, by, dd;
  memset(md, -1, sizeof(md));
  while (n--) {
    scanf("%d %d %d %d", &ax, &ay, &bx, &by);
    if (ax < bx)
      dd = 0;
    else if (ax > bx)
      dd = 2;
    else if (ay < by)
      dd = 1;
    else
      dd = 3;
    for (int i = min(ax, bx); i <= max(ax, bx); ++i)
      for (int j = min(ay, by); j <= max(ay, by); ++j) md[i][j] = dd;
  }
}
char s[12];
int main() {
  init();
  cin >> q;
  long long t;
  int xx, yy, dd;
  while (q--) {
    scanf("%d %d %s %I64d", &xx, &yy, s, &t);
    if (s[0] == 'R')
      dd = 0;
    else if (s[0] == 'U')
      dd = 1;
    else if (s[0] == 'L')
      dd = 2;
    else
      dd = 3;
    int pos = ask(xx, yy, dd, t);
    printf("%d %d\n", x[pos], y[pos]);
  }
  return 0;
}
