#include <bits/stdc++.h>
using namespace std;
const int maxn = 300010;
struct NN {
  int x1, y1, x2, y2;
  int x, y;
  int len;
} nn[1100];
int b;
int bel[1010][1010];
int tox[1010][1010], toy[1010][1010];
void init(int n) {
  int i, j, ii, jj;
  for (i = 1; i <= n; i++) {
    for (ii = nn[i].x1, jj = nn[i].y1, j = 0; j < nn[i].len + 1;
         j++, ii += nn[i].x, jj += nn[i].y)
      bel[ii][jj] = i;
  }
  for (i = 1; i <= n; i++) {
    ii = nn[i].x2 + nn[i].x;
    jj = nn[i].y2 + nn[i].y;
    while (ii >= 0 && ii <= b && jj >= 0 && jj <= b && !bel[ii][jj])
      ii += nn[i].x, jj += nn[i].y;
    for (j = 0; j < nn[i].len + 1; j++) {
      tox[nn[i].x1 + nn[i].x * j][nn[i].y1 + nn[i].y * j] = ii;
      toy[nn[i].x1 + nn[i].x * j][nn[i].y1 + nn[i].y * j] = jj;
    }
  }
}
int vis[1010][1010];
long long tim[1010][1010];
int main() {
  int n, i, j, k;
  cin >> n >> b;
  for (i = 1; i <= n; i++) {
    scanf("%d%d%d%d", &nn[i].x1, &nn[i].y1, &nn[i].x2, &nn[i].y2);
    nn[i].len = abs(nn[i].x2 - nn[i].x1) + abs(nn[i].y2 - nn[i].y1);
    if (nn[i].x1 < nn[i].x2)
      nn[i].x = 1;
    else if (nn[i].x1 > nn[i].x2)
      nn[i].x = -1;
    if (nn[i].y1 < nn[i].y2)
      nn[i].y = 1;
    else if (nn[i].y1 > nn[i].y2)
      nn[i].y = -1;
  }
  init(n);
  int m;
  cin >> m;
  int x, y;
  long long t;
  char s[10];
  int xx, yy;
  int woca = 1;
  while (m--) {
    scanf("%d%d%s%I64d", &x, &y, s, &t);
    if (!t) {
      printf("%d %d\n", x, y);
      continue;
    }
    if (s[0] == 'U')
      xx = 0, yy = 1;
    else if (s[0] == 'D')
      xx = 0, yy = -1;
    else if (s[0] == 'L')
      xx = -1, yy = 0;
    else
      xx = 1, yy = 0;
    while (x >= 0 && x <= b && y >= 0 && y <= b && !bel[x][y] && t) {
      x += xx, y += yy;
      t--;
    }
    if (x < 0 || y < 0 || x > b || y > b) {
      x -= xx, y -= yy;
      printf("%d %d\n", x, y);
      continue;
    }
    if (t == 0) {
      printf("%d %d\n", x, y);
      continue;
    }
    woca++;
    int dx = xx, dy = yy;
    while (t) {
      if (x < 0 || y < 0 || x > b || y > b) break;
      if (vis[x][y] == woca)
        t %= tim[x][y] - t;
      else
        vis[x][y] = woca, tim[x][y] = t;
      xx = tox[x][y], yy = toy[x][y];
      dx = dy = 0;
      if (xx > x)
        dx = 1;
      else if (xx < x)
        dx = -1;
      if (yy > y)
        dy = 1;
      else if (yy < y)
        dy = -1;
      if (abs(xx - x) + abs(yy - y) > t) {
        break;
      }
      t -= abs(xx - x) + abs(yy - y);
      x = xx, y = yy;
    }
    if (x < 0 || y < 0 || x > b || y > b) {
      if (x < 0) x++;
      if (y < 0) y++;
      if (x > b) x--;
      if (y > b) y--;
      t = 0;
    }
    int tt = t;
    printf("%d %d\n", x + tt * dx, y + tt * dy);
  }
}
