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 N = 1005;
int dx[5] = {0, 1, 0, -1, 0};
int dy[5] = {0, 0, 1, 0, -1};
int n, b, q;
int a[N + 5][N + 5];
int nx[N * N * 5 + 55][2];
int ans[111111];
long long t[111111];
int hash1(int x, int y, int d) {
  if (a[x][y]) d = a[x][y];
  return d * N * N + x * N + y;
}
int main() {
  int i, j, k, x1, y1, x2, y2, d, h;
  char ch[5];
  scanf("%d%d", &n, &b);
  for (i = 1; i <= n; i = i + 1) {
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    if (x1 == x2) {
      if (y1 > y2)
        d = 4;
      else
        d = 2;
    } else {
      if (x1 > x2)
        d = 3;
      else
        d = 1;
    }
    while (x1 != x2 || y1 != y2) {
      a[x1][y1] = d;
      x1 += dx[d];
      y1 += dy[d];
    }
    a[x2][y2] = d;
  }
  for (i = 0; i <= b; i = i + 1)
    for (j = 0; j <= b; j = j + 1)
      for (k = 1; k <= 4; k = k + 1) {
        if (a[i][j] && a[i][j] != k) continue;
        h = hash1(i, j, k);
        x1 = i + dx[k];
        y1 = j + dy[k];
        if (x1 < 0 || x1 > b || y1 < 0 || y1 > b)
          nx[h][0] = h;
        else
          nx[h][0] = hash1(x1, y1, k);
      }
  scanf("%d", &q);
  for (i = 1; i <= q; i = i + 1) {
    scanf("%d%d%s%lld", &x1, &x2, &ch, t + i);
    if (ch[0] == 'R') d = 1;
    if (ch[0] == 'U') d = 2;
    if (ch[0] == 'L') d = 3;
    if (ch[0] == 'D') d = 4;
    ans[i] = hash1(x1, x2, d);
  }
  for (i = 0; i <= 60; i = i + 1) {
    h = i & 1;
    for (j = 1; j <= q; j = j + 1)
      if (t[j] & ((long long)1 << i)) ans[j] = nx[ans[j]][h];
    for (j = 1; j < N * N * 5; j = j + 1) nx[j][h ^ 1] = nx[nx[j][h]][h];
  }
  for (i = 1; i <= q; i = i + 1) {
    y1 = ans[i] % N;
    ans[i] /= N;
    x1 = ans[i] % N;
    cout << x1 << ' ' << y1 << endl;
  }
  return 0;
}
