3 8
2 4 4 4
7 2 5 2
6 5 6 3
20
3 2 U 1000000
3 2 U 1
3 2 U 2
3 2 U 3
3 2 U 4
3 2 U 5
3 2 U 6
3 2 U 7
3 2 U 8
3 2 U 9
3 2 U 10
3 2 U 11
3 2 U 12
3 2 U 13
3 2 U 14
3 2 U 15
3 2 U 16
3 2 U 17
3 2 U 18
3 2 U 19
 4 2
0 0 1 0
2 0 2 1
2 2 1 2
0 2 0 1
15
1 1 R 7
0 0 U 3
1 1 R 888888888888887
0 0 U 404040404040403
2 1 D 1000000000000000
2 0 R 324354768576473
2 0 L 325436475453245
2 0 U 764536452343654
2 0 D 943654765876545
1 1 U 1000000000000000
1 1 D 989898789898789
1 1 L 123456787654344
0 1 L 0
0 1 L 1
0 1 L 999999999999993
 1 10
1 5 9 5
1
5 5 L 1
 3 7
2 4 2 7
4 7 4 6
7 3 5 3
10
6 7 L 1
6 7 L 2
6 7 L 3
6 7 L 4
6 7 L 5
6 7 L 6
6 7 L 7
6 7 L 8
6 7 L 9
6 7 L 10

                                                               0                                                                2
                                                               3                                                                3
                                                               3                                                                4
                                                               4                                                                4
                                                               5                                                                4
                                                               6                                                                4
                                                               6                                                                3
                                                               6                                                                2
                                                               5                                                                2
                                                               4                                                                2
                                                               3                                                                2
                                                               2                                                                2
                                                               1                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                                1                                                                0
                                                               2                                                                1
                                                               1                                                                0
                                                               2                                                                1
                                                               2                                                                1
                                                               2                                                                1
                                                               0                                                                1
                                                               0                                                                0
                                                               2                                                                1
                                                               2                                                                2
                                                               1                                                                2
                                                               0                                                                2
                                                               0                                                                1
                                                               0                                                                0
                                                               0                                                                0
                                                                6                                                                5
                                                                5                                                                7
                                                               4                                                                7
                                                               4                                                                6
                                                               4                                                                5
                                                               4                                                                4
                                                               4                                                                3
                                                               4                                                                2
                                                               4                                                                1
                                                               4                                                                0
                                                               4                                                                0

#include <bits/stdc++.h>
using namespace std;
const int DX[] = {-1, 0, +1, 0}, DY[] = {0, +1, 0, -1};
const char DC[] = {'L', 'U', 'R', 'D'};
int size;
int np;
int px[100000];
int py[100000];
int ppx[100000];
int ppy[100000];
int pd[100000];
int nq;
int qx[100000];
int qy[100000];
int qd[100000];
long long qt[100000];
int pnextnode[100000];
int pnextdist[100000];
int pnextpos[100000];
int qnextnode[100000];
int qnextdist[100000];
int qnextpos[100000];
int stepnode[50][100000];
long long steppos[50][100000];
void simulate(int &at, long long &pos, long long cnt, int maxstep) {
  pos += cnt;
  while (pnextnode[at] != -1 && pos >= pnextdist[at]) {
    while (maxstep >= 0 && (1LL << maxstep) > pos) --maxstep;
    int to;
    long long npos;
    char type;
    if (maxstep >= 0 && (1LL << maxstep) >= pnextdist[at]) {
      to = stepnode[maxstep][at],
      npos = pos - (1LL << maxstep) + steppos[maxstep][at];
      type = 'A';
    } else {
      to = pnextnode[at], npos = pos - pnextdist[at] + pnextpos[at];
      type = 'B';
    }
    at = to, pos = npos;
  }
}
int gnode[1001][1001];
long long gpos[1001][1001];
void calcnext(int x, int y, int d, int &nextnode, int &nextdist, int &nextpos,
              int skip) {
  nextdist = 0;
  while (x >= 0 && x <= size && y >= 0 && y <= size) {
    if (gnode[x][y] != -1 && gnode[x][y] != skip) {
      nextnode = gnode[x][y];
      nextpos = gpos[x][y];
      return;
    }
    x += DX[d], y += DY[d], ++nextdist;
  }
  nextnode = -1;
}
void run(int casenr) {
  cin >> np >> size;
  for (int i = (0); i < (np); ++i) cin >> ppx[i] >> ppy[i] >> px[i] >> py[i];
  for (int i = (0); i < (np); ++i) {
    int dx = px[i] - ppx[i], dy = py[i] - ppy[i];
    if (dx < 0) dx = -1;
    if (dx > 0) dx = +1;
    if (dy < 0) dy = -1;
    if (dy > 0) dy = +1;
    pd[i] = -1;
    for (int k = (0); k < (4); ++k)
      if (DX[k] == dx && DY[k] == dy) pd[i] = k;
  }
  cin >> nq;
  for (int i = (0); i < (nq); ++i) {
    char tmp, dc;
    cin >> qx[i] >> qy[i] >> dc >> qt[i];
    qd[i] = -1;
    for (int k = (0); k < (4); ++k)
      if (DC[k] == dc) qd[i] = k;
  }
  memset(gnode, -1, sizeof(gnode));
  memset(gpos, -1, sizeof(gpos));
  for (int i = (0); i < (np); ++i)
    for (int x = ppx[i], y = ppy[i];; x += DX[pd[i]], y += DY[pd[i]]) {
      gnode[x][y] = i;
      gpos[x][y] = -abs(x - px[i]) - abs(y - py[i]);
      if (x == px[i] && y == py[i]) break;
    }
  for (int i = (0); i < (np); ++i)
    calcnext(px[i], py[i], pd[i], pnextnode[i], pnextdist[i], pnextpos[i], i);
  for (int i = (0); i < (nq); ++i)
    calcnext(qx[i], qy[i], qd[i], qnextnode[i], qnextdist[i], qnextpos[i], -1);
  for (int j = (0); j < (np); ++j) {
    stepnode[0][j] = j;
    steppos[0][j] = 0;
    simulate(stepnode[0][j], steppos[0][j], 1, -1);
  }
  for (int i = (1); i < (50); ++i)
    for (int j = (0); j < (np); ++j) {
      stepnode[i][j] = stepnode[i - 1][j];
      steppos[i][j] = steppos[i - 1][j];
      simulate(stepnode[i][j], steppos[i][j], 1LL << (i - 1), i - 1);
    }
  for (int qi = (0); qi < (nq); ++qi) {
    long long x, y;
    if (qnextnode[qi] != -1 && qt[qi] >= qnextdist[qi]) {
      int at = qnextnode[qi];
      long long pos = qnextpos[qi];
      simulate(at, pos, qt[qi] - qnextdist[qi], 50);
      x = px[at] + pos * DX[pd[at]], y = py[at] + pos * DY[pd[at]];
    } else {
      x = qx[qi] + qt[qi] * DX[qd[qi]], y = qy[qi] + qt[qi] * DY[qd[qi]];
    }
    if (x < 0) x = 0;
    if (x > size) x = size;
    if (y < 0) y = 0;
    if (y > size) y = size;
    cout << x << " " << y << endl;
  }
}
int main() {
  run(1);
  return 0;
}
