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 maxn = 2e5 + 5;
const int walk[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
struct ARROW {
  int x0, y0, x1, y1;
  ARROW *to[55];
  long long dis[55];
  int dir() { return x0 < x1 ? 0 : y0 < y1 ? 1 : x0 > x1 ? 2 : 3; }
};
inline bool xl(ARROW *a, ARROW *b) {
  return max(a->x0, a->x1) < max(b->x0, b->x1);
}
inline bool xg(ARROW *a, ARROW *b) {
  return min(a->x0, a->x1) > min(b->x0, b->x1);
}
inline bool yl(ARROW *a, ARROW *b) {
  return max(a->y0, a->y1) < max(b->y0, b->y1);
}
inline bool yg(ARROW *a, ARROW *b) {
  return min(a->y0, a->y1) > min(b->y0, b->y1);
}
int N, B, Q;
long long T[maxn];
ARROW A[maxn], *S[maxn];
void Init(bool (*cmp)(ARROW *, ARROW *), int dir, int ARROW::*y0,
          int ARROW::*y1) {
  for (int i = 1; i <= N + Q; i++) S[i] = A + N + Q - i + 1;
  stable_sort(S + 1, S + N + Q + 1, cmp);
  multimap<int, ARROW *> F;
  typedef multimap<int, ARROW *>::iterator it;
  for (int i = 1; i <= N + Q; i++) {
    if (S[i] - A <= N) {
      it lo = F.lower_bound(min(S[i]->*y0, S[i]->*y1));
      it hi = F.upper_bound(max(S[i]->*y0, S[i]->*y1));
      for (it j = lo; j != hi; j++) {
        j->second->to[0] = S[i];
        j->second->dis[0] =
            abs(j->second->x1 - S[i]->x1) + abs(j->second->y1 - S[i]->y1);
      }
      F.erase(lo, hi);
    }
    if (S[i]->dir() == dir) F.insert(make_pair(S[i]->*y0, S[i]));
  }
}
int main() {
  scanf("%d%d", &N, &B);
  for (int i = 1; i <= N; i++)
    scanf("%d%d%d%d", &A[i].x0, &A[i].y0, &A[i].x1, &A[i].y1);
  scanf("%d", &Q);
  for (int i = 1; i <= Q; i++) {
    char dir;
    scanf("%d%d %c%I64d", &A[i + N].x0, &A[i + N].y0, &dir, &T[i]);
    A[i + N].x1 = A[i + N].x0, A[i + N].y1 = A[i + N].y0;
    switch (dir) {
      case 'R':
        A[i + N].x0--;
        break;
      case 'L':
        A[i + N].x0++;
        break;
      case 'U':
        A[i + N].y0--;
        break;
      case 'D':
        A[i + N].y0++;
    }
  }
  Init(xl, 0, &ARROW::y0, &ARROW::y1);
  Init(xg, 2, &ARROW::y0, &ARROW::y1);
  Init(yl, 1, &ARROW::x0, &ARROW::x1);
  Init(yg, 3, &ARROW::x0, &ARROW::x1);
  for (int k = 1; k < 50; k++)
    for (ARROW *i = A + 1; i <= A + N + Q; i++)
      if (i->to[k - 1] && i->to[k - 1]->to[k - 1]) {
        i->to[k] = i->to[k - 1]->to[k - 1];
        i->dis[k] = i->dis[k - 1] + i->to[k - 1]->dis[k - 1];
      }
  for (int i = 1; i <= Q; i++) {
    ARROW *now = A + i + N;
    long long t = T[i];
    for (int k = (t ? 63 - __builtin_clzll(t) : -1); k >= 0; k--)
      if (now->to[k] && now->dis[k] <= t) t -= now->dis[k], now = now->to[k];
    int x = now->x1, y = now->y1, dir = now->dir();
    if (now->to[0]) {
      int half;
      if (now->x0 == now->x1) {
        half = max(0, min((now->to[0]->y0 - now->y1) / walk[dir][1],
                          (now->to[0]->y1 - now->y1) / walk[dir][1]));
        if (half <= t)
          t -= half, y += walk[dir][1] * half, dir = now->to[0]->dir();
      } else {
        half = max(0, min((now->to[0]->x0 - now->x1) / walk[dir][0],
                          (now->to[0]->x1 - now->x1) / walk[dir][0]));
        if (half <= t)
          t -= half, x += walk[dir][0] * half, dir = now->to[0]->dir();
      }
    }
    x = min<long long>(B, max(0ll, x + walk[dir][0] * t));
    y = min<long long>(B, max(0ll, y + walk[dir][1] * t));
    printf("%d %d\n", x, y);
  }
  return 0;
}
