#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:512000000")
using namespace std;
const double PI = acos(-1.0);
const int INF = 1000000000;
const int MOD = 1000000007;
struct position {
  short x, y;
  char direction;
  position() {}
  position(short x, short y, char direction)
      : x(x), y(y), direction(direction) {}
};
int sgn(int x) {
  if (x < 0) return -1;
  if (x > 0) return +1;
  return 0;
}
const int N = 50;
int main() {
  int _start = clock();
  int n, b;
  cin >> n >> b;
  int dx[] = {0, 0, -1, 1};
  int dy[] = {1, -1, 0, 0};
  map<char, int> directions;
  directions['U'] = 0;
  directions['D'] = 1;
  directions['L'] = 2;
  directions['R'] = 3;
  position to[N][N][4][56];
  char arrows[N][N];
  memset(arrows, -1, sizeof(arrows));
  int x0, y0, x1, y1;
  for (int i = 0; i < n; ++i) {
    scanf("%d %d %d %d", &x0, &y0, &x1, &y1);
    int cx = sgn(x1 - x0);
    int cy = sgn(y1 - y0);
    int dir = 0;
    for (int j = 0; j < 4; ++j) {
      if (dx[j] == cx && dy[j] == cy) {
        dir = j;
        break;
      }
    }
    do {
      arrows[x0][y0] = dir;
      x0 += cx;
      y0 += cy;
    } while (!(x0 == x1 && y0 == y1));
    arrows[x0][y0] = dir;
  }
  for (int i = 0; i <= b; ++i) {
    for (int j = 0; j <= b; ++j) {
      for (int dir = 0; dir < 4; ++dir) {
        int tdir = dir;
        if (arrows[i][j] != -1) tdir = arrows[i][j];
        int nx = i + dx[tdir], ny = j + dy[tdir];
        if (nx < 0 || nx > b || ny < 0 || ny > b) {
          to[i][j][dir][0] = position(i, j, tdir);
          continue;
        }
        to[i][j][dir][0] = position(nx, ny, tdir);
      }
    }
  }
  for (int step = 1; step <= 55; ++step) {
    for (int i = 0; i <= b; ++i) {
      for (int j = 0; j <= b; ++j) {
        for (int dir = 0; dir < 4; ++dir) {
          position p1 = to[i][j][dir][step - 1];
          to[i][j][dir][step] = to[p1.x][p1.y][p1.direction][step - 1];
        }
      }
    }
  }
  int q, x, y, dir;
  long long t;
  cin >> q;
  while (q--) {
    scanf("%d %d %c", &x, &y, &dir);
    cin >> t;
    if (t == 0) {
      printf("%d %d\n", x, y);
      continue;
    }
    dir = directions[dir];
    position at(x, y, dir);
    for (int i = 55; i >= 0; --i) {
      if (t & (1ll << i)) {
        at = to[at.x][at.y][at.direction][i];
      }
    }
    printf("%d %d\n", at.x, at.y);
  }
  cerr << endl
       << endl
       << "Time: " << (double)(clock() - _start) / CLOCKS_PER_SEC << endl;
  return 0;
}
