#include <bits/stdc++.h>
using namespace std;
int v[31][31];
int dx[5] = {0, 0, 1, 0, -1};
int dy[5] = {0, 1, 0, -1, 0};
int used[31][31][6];
int getV(char c) {
  if (c == 'U') return 1;
  if (c == 'R') return 2;
  if (c == 'D') return 3;
  if (c == 'L') return 4;
}
void fill0() {
  for (int i = 0; i < 31; i++)
    for (int j = 0; j < 31; j++)
      for (int r = 1; r <= 5; r++) used[i][j][r] = -1;
}
int n, b;
bool out(int x, int y) { return max(x, y) > b || min(x, y) < 0; }
int main() {
  scanf("%d%d", &n, &b);
  for (int i = 0; i < n; i++) {
    int sx, sy, fx, fy, r = 0;
    scanf("%d%d%d%d", &sx, &sy, &fx, &fy);
    if (sx == fx) {
      if (sy < fy)
        r = 1;
      else
        r = 3;
      for (int j = min(sy, fy); j <= max(sy, fy); j++) v[sx][j] = r;
    } else {
      if (sx < fx)
        r = 2;
      else
        r = 4;
      for (int j = min(sx, fx); j <= max(sx, fx); j++) v[j][sy] = r;
    }
  }
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int x, y, r;
    long long TL;
    char c;
    scanf("%d%d%*c%c%I64d", &x, &y, &c, &TL);
    r = getV(c);
    fill0();
    bool OUT = 0, LOOP = 0;
    long long t, need;
    for (t = 0; t < TL; t++) {
      if (used[x][y][r] != -1) {
        LOOP = 1;
        break;
      }
      used[x][y][r] = t;
      if (v[x][y] != 0) r = v[x][y];
      if (out(x + dx[r], y + dy[r])) {
        OUT = 1;
        break;
      }
      x += dx[r];
      y += dy[r];
    }
    if (LOOP) {
      need = TL - t;
      need %= t - used[x][y][r];
      for (t = 0; t < need; t++) {
        if (v[x][y] != 0) r = v[x][y];
        x += dx[r];
        y += dy[r];
      }
      printf("%d %d\n", x, y);
    } else {
      printf("%d %d\n", x, y);
    }
  }
}
