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 MX = 1005, MV = MX * MX * 4, n2 = 26, INF = 1000000000;
const long long int LINF = 1000000000000000000ll;
const double eps = 1e-9;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
int ci[256];
int n, m;
int to[n2][MV];
bool used[MV];
int tov(int x, int y, int vec) { return x * m * 4 + y * 4 + vec; }
int main() {
  ci['L'] = 0;
  ci['R'] = 2;
  ci['U'] = 3;
  ci['D'] = 1;
  int ni, nj, v, u, x, x2, y, y2;
  long long int t, z;
  scanf("%d%d", &n, &m);
  m++;
  int vs = m * m * 4;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < m; j++)
      for (int k = 0; k < 4; k++) {
        ni = i + dx[k];
        nj = j + dy[k];
        v = tov(i, j, k);
        if (ni < 0 || nj < 0 || ni >= m || nj >= m) {
          to[0][v] = v;
        } else {
          to[0][v] = tov(ni, nj, k);
        }
      }
  int av, dz;
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d%d", &x, &y, &x2, &y2);
    if (x == x2) {
      if (y < y2) {
        av = 3;
        dz = 1;
      } else {
        av = 1;
        dz = -1;
      }
      for (; y - dz != y2; y += dz) {
        v = tov(x, y, av);
        for (int k = 0; k < 4; k++) {
          u = tov(x, y, k);
          to[0][u] = to[0][v];
        }
      }
    } else {
      if (x < x2) {
        av = 2;
        dz = 1;
      } else {
        av = 0;
        dz = -1;
      }
      for (; x - dz != x2; x += dz) {
        v = tov(x, y, av);
        for (int k = 0; k < 4; k++) {
          u = tov(x, y, k);
          to[0][u] = to[0][v];
        }
      }
    }
  }
  for (int i = 1; i <= n2 - 1; i++)
    for (int j = 0; j < vs; j++)
      to[i][j] = to[i - 1][to[i - 1][to[i - 1][to[i - 1][j]]]];
  scanf("%d", &n);
  char c;
  for (int ni = 0; ni < n; ni++) {
    scanf("%d%d %c%I64d", &x, &y, &c, &t);
    av = ci[c];
    v = tov(x, y, av);
    z = 2ll << ((n2 - 1) * 2);
    for (int i = n2 - 1; i >= 0; i--) {
      if (t & z) v = to[i][to[i][v]];
      z >>= 1;
      if (t & z) v = to[i][v];
      z >>= 1;
    }
    v /= 4;
    printf("%d %d\n", v / m, v % m);
  }
  return 0;
}
