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;
struct coord {
  int x, y, dir;
};
struct node {
  int cycle, cyclepos;
  int len;
  int visited;
};
int main() {
  int N, B;
  scanf("%d %d", &N, &B);
  static struct node a[1005][1005][4];
  static int d[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
  int x, y, dir;
  static int ch[1005][1005];
  for (x = 0; x <= B; x++) {
    for (y = 0; y <= B; y++) {
      ch[x][y] = -1;
      for (dir = 0; dir < 4; dir++) {
        a[x][y][dir].visited = -1;
      }
    }
  }
  static struct coord q[1005 * 1005 * 4];
  int X0, Y0, X1, Y1;
  int i, temp;
  for (i = 0; i < N; i++) {
    scanf("%d %d %d %d", &X0, &Y0, &X1, &Y1);
    int ndir = -1;
    if (X0 == X1) {
      if (Y0 < Y1)
        ndir = 0;
      else
        ndir = 1;
    } else {
      if (X0 < X1)
        ndir = 3;
      else
        ndir = 2;
    }
    if (X0 > X1) {
      temp = X0;
      X0 = X1;
      X1 = temp;
    }
    if (Y0 > Y1) {
      temp = Y0;
      Y0 = Y1;
      Y1 = temp;
    }
    if (X0 == X1) {
      x = X0;
      for (y = Y0; y <= Y1; y++) {
        ch[x][y] = ndir;
      }
    } else {
      y = Y0;
      for (x = X0; x <= X1; x++) {
        ch[x][y] = ndir;
      }
    }
  }
  static int cycles[1005 * 1005 * 4][2];
  int cyclesc = 0;
  int qw, z, st;
  qw = 0;
  int x7, y7, dir7;
  for (x7 = 0; x7 <= B; x7++) {
    for (y7 = 0; y7 <= B; y7++) {
      for (dir7 = 0; dir7 < 4; dir7++) {
        if (a[x7][y7][dir7].visited == -1) {
          x = x7;
          y = y7;
          dir = dir7;
          st = qw;
          while (1) {
            if (a[x][y][dir].visited == -2) {
              int cycle, cyclepos, len;
              cycle = a[x][y][dir].cycle;
              cyclepos = a[x][y][dir].cyclepos;
              len = a[x][y][dir].len;
              for (z = qw - 1; z >= st; z--) {
                a[q[z].x][q[z].y][q[z].dir].cycle = cycle;
                a[q[z].x][q[z].y][q[z].dir].cyclepos = cyclepos;
                len++;
                a[q[z].x][q[z].y][q[z].dir].len = len;
                a[q[z].x][q[z].y][q[z].dir].visited = -2;
              }
              break;
            } else if (a[x][y][dir].visited != -1) {
              int cycle, cyclepos, len;
              len = qw - a[x][y][dir].visited;
              cycles[cyclesc][0] = a[x][y][dir].visited;
              cycles[cyclesc][1] = qw;
              cycle = cyclesc;
              cyclepos = len;
              len = 0;
              int saved = a[x][y][dir].visited;
              cyclesc++;
              for (z = qw - 1; z >= saved; z--) {
                a[q[z].x][q[z].y][q[z].dir].cycle = cycle;
                cyclepos--;
                a[q[z].x][q[z].y][q[z].dir].cyclepos = cyclepos;
                a[q[z].x][q[z].y][q[z].dir].len = 0;
                a[q[z].x][q[z].y][q[z].dir].visited = -2;
              }
              for (z = saved - 1; z >= st; z--) {
                a[q[z].x][q[z].y][q[z].dir].cycle = cycle;
                a[q[z].x][q[z].y][q[z].dir].cyclepos = cyclepos;
                len++;
                a[q[z].x][q[z].y][q[z].dir].len = len;
                a[q[z].x][q[z].y][q[z].dir].visited = -2;
              }
              break;
            }
            a[x][y][dir].visited = qw;
            q[qw].x = x;
            q[qw].y = y;
            q[qw].dir = dir;
            qw++;
            if (ch[x][y] != -1) dir = ch[x][y];
            x += d[dir][0];
            y += d[dir][1];
            if ((x < 0) || (x > B) || (y < 0) || (y > B)) {
              x -= d[dir][0];
              y -= d[dir][1];
            }
          }
        }
      }
    }
  }
  int Q;
  scanf("%d", &Q);
  int iQ;
  for (iQ = 0; iQ < Q; iQ++) {
    long long T;
    char c;
    scanf("%d %d %c %I64i", &x, &y, &c, &T);
    if (c == 'U')
      dir = 0;
    else if (c == 'D')
      dir = 1;
    else if (c == 'L')
      dir = 2;
    else
      dir = 3;
    if (T < (long long)(a[x][y][dir].len)) {
      while (1) {
        if (T == 0LL) break;
        if (ch[x][y] != -1) dir = ch[x][y];
        x += d[dir][0];
        y += d[dir][1];
        T--;
      }
      printf("%d %d\n", x, y);
    } else {
      T -= (long long)(a[x][y][dir].len);
      int clen = cycles[a[x][y][dir].cycle][1] - cycles[a[x][y][dir].cycle][0];
      T %= (long long)(clen);
      int pos = cycles[a[x][y][dir].cycle][0] +
                ((a[x][y][dir].cyclepos + (int)(T)) % clen);
      printf("%d %d\n", q[pos].x, q[pos].y);
    }
  }
  return 0;
}
