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;
inline int gi() {
  char c = getchar();
  int x = 0;
  for (; c < '0' || c > '9'; c = getchar())
    ;
  for (; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + c - '0';
  return x;
}
const int N = 2e5 + 5, L = 50;
const long long inf = 1e16;
struct node {
  int x1, x2, y1, y2, id;
} a[N];
bool cmp1(node a, node b) {
  if (max(a.x1, a.x2) == max(b.x1, b.x2)) return a.id < b.id;
  return max(a.x1, a.x2) > max(b.x1, b.x2);
}
bool cmp2(node a, node b) { return a.id < b.id; }
int n, m, q, to[L + 2][N], cov[N << 2];
long long t[N], dis[L + 2][N];
const int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
void build(int x = 1, int l = -1, int r = m + 1) {
  cov[x] = 0;
  if (l == r) return;
  int mid = l + r >> 1;
  build((x << 1), l, mid), build((x << 1 | 1), mid + 1, r);
}
void update(int sl, int sr, int w, int x = 1, int l = -1, int r = m + 1) {
  if (sl <= l && r <= sr) {
    cov[x] = w;
    return;
  }
  if (cov[x]) cov[(x << 1)] = cov[(x << 1 | 1)] = cov[x];
  int mid = l + r >> 1;
  if (sl <= mid) update(sl, sr, w, (x << 1), l, mid);
  if (sr > mid) update(sl, sr, w, (x << 1 | 1), mid + 1, r);
  cov[x] = (cov[(x << 1)] == cov[(x << 1 | 1)] ? cov[(x << 1)] : 0);
}
int query(int s, int x = 1, int l = -1, int r = m + 1) {
  if (cov[x] || l == r) return cov[x];
  int mid = l + r >> 1;
  return s <= mid ? query(s, (x << 1), l, mid)
                  : query(s, (x << 1 | 1), mid + 1, r);
}
void transform() {
  for (int i = 1; i <= n + q; ++i) {
    swap(a[i].x1, a[i].y1), a[i].x1 = m - a[i].x1;
    swap(a[i].x2, a[i].y2), a[i].x2 = m - a[i].x2;
  }
}
inline char getdir(node a) {
  if (a.x1 > a.x2) return 0;
  if (a.x1 < a.x2) return 1;
  if (a.y1 > a.y2) return 2;
  if (a.y1 < a.y2) return 3;
}
inline bool have(int l1, int r1, int l2, int r2) {
  if (l1 > r1) swap(l1, r1);
  if (l2 > r2) swap(l2, r2);
  if (l2 <= l1 && l1 <= r2) return true;
  if (l2 <= r1 && r1 <= r2) return true;
  return false;
}
inline int getdis(node a, node b) {
  if (getdir(a) < 2 && getdir(b) < 2 && have(a.x1, a.x2, b.x1, b.x2)) return 0;
  if (getdir(a) > 1 && getdir(b) > 1 && have(a.y1, a.y2, b.y1, b.y2)) return 0;
  switch (getdir(a)) {
    case 0:
      return a.x2 - max(b.x1, b.x2);
    case 1:
      return min(b.x1, b.x2) - a.x2;
    case 2:
      return a.y2 - max(b.y1, b.y2);
    case 3:
      return min(b.y1, b.y2) - a.y2;
  }
}
inline int odis(node a, node b) { return abs(a.x2 - b.x2) + abs(a.y2 - b.y2); }
void move(int& x, int& y, int d, int t) { x += t * dx[d], y += t * dy[d]; }
int main() {
  n = gi(), m = gi();
  for (int i = 1; i <= n; ++i)
    a[i].x1 = gi(), a[i].y1 = gi(), a[i].x2 = gi(), a[i].y2 = gi(), a[i].id = i;
  q = gi();
  for (int i = 1; i <= q; ++i) {
    int x = gi(), y = gi(), nx, ny;
    char op[2];
    scanf("%s%lld", op, &t[i]);
    if (op[0] == 'L') nx = x + 1, ny = y;
    if (op[0] == 'R') nx = x - 1, ny = y;
    if (op[0] == 'U') nx = x, ny = y - 1;
    if (op[0] == 'D') nx = x, ny = y + 1;
    a[n + i] = (node){nx, x, ny, y, i + n};
  }
  for (int T = 1; T <= 4; transform(), ++T) {
    sort(a + 1, a + 1 + n + q, cmp1), build();
    for (int i = 1; i <= n + q; ++i) {
      if (a[i].x1 < a[i].x2) {
        int now = query(a[i].y1);
        to[0][a[i].id] = a[now].id;
        dis[0][a[i].id] = odis(a[i], a[now]);
      }
      if (a[i].id <= n) update(min(a[i].y1, a[i].y2), max(a[i].y1, a[i].y2), i);
    }
  }
  for (int i = 1; i <= L; ++i)
    for (int j = 1; j <= n + q; ++j) {
      to[i][j] = to[i - 1][to[i - 1][j]];
      dis[i][j] = min(inf, dis[i - 1][j] + dis[i - 1][to[i - 1][j]]);
    }
  sort(a + 1, a + 1 + n + q, cmp2);
  for (int i = 1; i <= q; ++i) {
    int u = n + i;
    for (int j = L; ~j; --j)
      if (to[j][u] && t[i] >= dis[j][u]) t[i] -= dis[j][u], u = to[j][u];
    int x = a[u].x2, y = a[u].y2;
    if (!to[0][u]) {
      move(x, y, getdir(a[u]), min(t[i], 1ll * m));
      x = max(x, 0), x = min(x, m);
      y = max(y, 0), y = min(y, m);
      printf("%d %d\n", x, y);
      continue;
    }
    node nxt = a[to[0][u]];
    int d = getdis(a[u], nxt);
    if (d > t[i])
      move(x, y, getdir(a[u]), t[i]);
    else {
      t[i] -= d;
      move(x, y, getdir(a[u]), d);
      move(x, y, getdir(nxt), t[i]);
    }
    printf("%d %d\n", x, y);
  }
}
