#include <bits/stdc++.h>
using namespace std;
const long long inf = 3e14 + 5;
const int maxn = 3e5 + 5;
int vec[35][35];
long long vis[35][35];
int fx[35][35];
struct POS {
  int x, y;
};
int b;
POS dfs(int x, int y, int fang, long long now, long long havetime) {
  POS ret;
  if (vec[x][y] > 0) fang = vec[x][y];
  if (x == 0) {
    if (fang == 3) {
      ret.x = x, ret.y = y;
      return ret;
    }
  } else if (x == b) {
    if (fang == 4) {
      ret.x = x, ret.y = y;
      return ret;
    }
  }
  if (y == 0) {
    if (fang == 2) {
      ret.x = x, ret.y = y;
      return ret;
    }
  } else if (y == b) {
    if (fang == 1) {
      ret.x = x, ret.y = y;
      return ret;
    }
  }
  if (vis[x][y] != -1 && fx[x][y] == fang) {
    long long ct = now - vis[x][y];
    long long tmp = (havetime - now) / ct;
    now += tmp * ct;
  }
  if (havetime == now) {
    ret.x = x, ret.y = y;
    return ret;
  }
  vis[x][y] = now;
  fx[x][y] = fang;
  if (fang == 1)
    y++;
  else if (fang == 2)
    y--;
  else if (fang == 3)
    x--;
  else
    x++;
  return dfs(x, y, fang, now + 1, havetime);
}
int main() {
  int n;
  scanf("%d%d", &n, &b);
  for (int i = 0; i < n; i++) {
    int x0, y0, x1, y1;
    scanf("%d%d%d%d", &x0, &y0, &x1, &y1);
    if (x0 == x1) {
      if (y0 < y1) {
        for (int j = y0; j <= y1; j++) vec[x0][j] = 1;
      } else if (y0 > y1) {
        for (int j = y1; j <= y0; j++) vec[x0][j] = 2;
      }
    } else if (y0 == y1) {
      if (x0 < x1) {
        for (int j = x0; j <= x1; j++) vec[j][y0] = 4;
      } else if (x0 > x1) {
        for (int j = x1; j <= x0; j++) vec[j][y0] = 3;
      }
    }
  }
  int q;
  int x0, y0;
  long long t;
  char fan[5];
  scanf("%d", &q);
  while (q--) {
    memset(vis, -1, sizeof(vis));
    scanf("%d %d %s %lld", &x0, &y0, fan, &t);
    int fang;
    if (fan[0] == 'U')
      fang = 1;
    else if (fan[0] == 'D')
      fang = 2;
    else if (fan[0] == 'L')
      fang = 3;
    else
      fang = 4;
    POS ans = dfs(x0, y0, fang, 0, t);
    printf("%d %d\n", ans.x, ans.y);
  }
  return 0;
}
