#include <bits/stdc++.h>
using namespace std;
int d[40][40];
int ax[4] = {-1, 0, 1, 0};
int ay[4] = {0, -1, 0, 1};
char str[110];
int get() {
  scanf("%s", str);
  if (str[0] == 'L') return 0;
  if (str[0] == 'D') return 1;
  if (str[0] == 'R') return 2;
  if (str[0] == 'U') return 3;
}
int vis[40][40];
int m, n;
bool inside(int x, int y) {
  if (x < 0 || x > n || y < 0 || y > n) return false;
  return true;
}
pair<int, int> at[100010];
int main() {
  scanf("%d %d", &m, &n);
  memset(d, -1, sizeof(d));
  while (m--) {
    int x1, y1, x2, y2;
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
    if (x1 == x2) {
      if (y1 < y2) {
        for (int i = y1; i <= y2; i++) d[x1][i] = 3;
      } else {
        for (int i = y2; i <= y1; i++) d[x1][i] = 1;
      }
    } else {
      if (x1 < x2) {
        for (int i = x1; i <= x2; i++) d[i][y1] = 2;
      } else {
        for (int i = x2; i <= x1; i++) d[i][y1] = 0;
      }
    }
  }
  int Q;
  scanf("%d", &Q);
  while (Q--) {
    int x, y;
    scanf("%d %d", &x, &y);
    int flag = get();
    long long T;
    scanf("%I64d", &T);
    long long now = 0;
    memset(vis, -1, sizeof(vis));
    if (d[x][y] != -1) vis[x][y] = 0;
    at[0] = make_pair(x, y);
    while (now < T) {
      now++;
      if (d[x][y] != -1) flag = d[x][y];
      int xx = x + ax[flag];
      int yy = y + ay[flag];
      if (!inside(xx, yy)) break;
      if (vis[xx][yy] != -1) {
        long long t = vis[xx][yy];
        long long rrr = now - t;
        long long left = T - now;
        left %= rrr;
        left += t;
        x = at[left].first;
        y = at[left].second;
        break;
      }
      at[now] = make_pair(xx, yy);
      if (d[xx][yy] != -1) vis[xx][yy] = now;
      x = xx;
      y = yy;
    }
    printf("%d %d\n", x, y);
  }
  return 0;
}
