#include <bits/stdc++.h>
using namespace std;
int ans[2][2000];
char a[40][40];
int was[40][40][4];
int ti[40][40][4];
int dirx[26];
int diry[26];
int num[26];
char s[3];
int main() {
  dirx['L' - 'A'] = dirx['R' - 'A'] = 0;
  dirx['U' - 'A'] = -1;
  dirx['D' - 'A'] = 1;
  diry['U' - 'A'] = diry['D' - 'A'] = 0;
  diry['L' - 'A'] = -1;
  diry['R' - 'A'] = 1;
  num['L' - 'A'] = 1;
  num['R' - 'A'] = 2;
  num['U' - 'A'] = 3;
  num['D' - 'A'] = 4;
  int n, b;
  scanf("%d%d", &n, &b);
  for (int i = (0); i < (b + 1); ++i)
    for (int j = (0); j < (b + 1); ++j) a[i][j] = 'H';
  for (int i = (0); i < (n); ++i) {
    int x1, y1, x2, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    if (x1 > x2)
      for (int i = (x2); i < (x1 + 1); ++i) a[i][y1] = 'U';
    if (x2 > x1)
      for (int i = (x1); i < (x2 + 1); ++i) a[i][y1] = 'D';
    if (y1 > y2)
      for (int i = (y2); i < (y1 + 1); ++i) a[x1][i] = 'L';
    if (y2 > y1)
      for (int i = (y1); i < (y2 + 1); ++i) a[x1][i] = 'R';
  }
  int q;
  scanf("%d", &q);
  for (int i = (0); i < (q); ++i) {
    int x, y;
    long long t;
    scanf("%d%d%s%I64d", &x, &y, &s, &t);
    if (s[0] == 'L')
      s[0] = 'U';
    else if (s[0] == 'R')
      s[0] = 'D';
    else if (s[0] == 'U')
      s[0] = 'R';
    else
      s[0] = 'L';
    int dx = dirx[s[0] - 'A'];
    int dy = diry[s[0] - 'A'];
    int d = num[s[0] - 'A'];
    bool out = false;
    int last = -1;
    int begg = -1, endd = -1;
    while (1) {
      if ((x < 0) || (x > b) || (y < 0) || (y > b)) {
        out = true;
        break;
      }
      last++;
      if (was[x][y][d] == i + 1) {
        begg = ti[x][y][d];
        endd = last;
        break;
      }
      was[x][y][d] = i + 1;
      ti[x][y][d] = last;
      ans[0][last] = x;
      ans[1][last] = y;
      if (a[x][y] != 'H') {
        dx = dirx[a[x][y] - 'A'];
        dy = diry[a[x][y] - 'A'];
        d = num[a[x][y] - 'A'];
      }
      x += dx;
      y += dy;
    }
    if (out) {
      if (t > last) t = last;
      printf("%d %d\n", ans[0][t], ans[1][t]);
    } else {
      if (t < begg)
        printf("%d %d\n", ans[0][t], ans[1][t]);
      else {
        t -= begg;
        t %= (endd - begg);
        printf("%d %d\n", ans[0][begg + t], ans[1][begg + t]);
      }
    }
  }
  return 0;
}
