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 unsigned _mod = 998244353;
const unsigned mod = 1e9 + 7;
const long long infi = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
long long ksm(long long x, long long y) {
  long long ret = 1;
  while (y) {
    if (y & 1ll) ret = ret * x % mod;
    y >>= 1ll;
    x = x * x % mod;
  }
  return ret;
}
long long qpow(long long x, long long y) {
  long long ret = 1;
  while (y) {
    if (y & 1ll) ret = ret * x;
    y >>= 1ll;
    x = x * x;
  }
  return ret;
}
long long _gcd(long long x, long long y) { return y ? _gcd(y, x % y) : x; }
long long dx[5] = {0, 1, 0, -1, 0}, dy[5] = {0, 0, 1, 0, -1}, n, b, q,
          a[1010][1010], x[5100010][2], ans[1200010], t[1200010], x1,
          suibiandefineyixiajiuhaole, x2, y2, d, h;
char ch;
long long hash1(long long x, long long y, long long d) {
  if (a[x][y]) d = a[x][y];
  return d * 1001 * 1001 + x * 1001 + y;
}
int main() {
  cin >> n >> b;
  for (int i = 1; i <= n; ++i) {
    x1 = read(), suibiandefineyixiajiuhaole = read(), x2 = read(), y2 = read();
    if (x1 == x2)
      d = 2 + (suibiandefineyixiajiuhaole > y2) * 2;
    else
      d = 1 + (x1 > x2) * 2;
    while (x1 != x2 || suibiandefineyixiajiuhaole != y2)
      a[x1][suibiandefineyixiajiuhaole] = d, x1 += dx[d],
      suibiandefineyixiajiuhaole += dy[d];
    a[x2][y2] = d;
  }
  for (register int i = 0; i <= b; ++i)
    for (register int j = 0; j <= b; ++j)
      for (register int k = 1; k <= 4; ++k)
        if (!a[i][j] || a[i][j] == k) {
          h = hash1(i, j, k);
          x1 = i + dx[k];
          suibiandefineyixiajiuhaole = j + dy[k];
          if (x1 < 0 || x1 > b || suibiandefineyixiajiuhaole < 0 ||
              suibiandefineyixiajiuhaole > b)
            x[h][0] = h;
          else
            x[h][0] = hash1(x1, suibiandefineyixiajiuhaole, k);
        }
  cin >> q;
  for (register int i = 1; i <= q; ++i) {
    x1 = read(), x2 = read();
    cin >> ch;
    t[i] = read();
    if (ch == 'R') d = 1;
    if (ch == 'U') d = 2;
    if (ch == 'L') d = 3;
    if (ch == 'D') d = 4;
    ans[i] = hash1(x1, x2, d);
  }
  for (register int i = 0; i <= 60; ++i) {
    h = i & 1;
    for (register int j = 1; j <= q; ++j)
      if (t[j] & (1ll << i)) ans[j] = x[ans[j]][h];
    for (register int j = 1; j <= 5100000; ++j) x[j][h ^ 1] = x[x[j][h]][h];
  }
  for (register int i = 1; i <= q; ++i) {
    suibiandefineyixiajiuhaole = ans[i] % 1001;
    x1 = (ans[i] / 1001) % 1001;
    cout << x1 << ' ' << suibiandefineyixiajiuhaole << '\n';
  }
  return 0;
}
