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 = 200005;
const long long inf = 1e18;
int n, m, q, fa[50][N];
long long T[N], F[50][N];
struct node {
  int a, b, c, d;
  int id;
} A[N];
vector<node> v;
int cov[N << 2], tj[N << 2];
void clear_tree() {
  memset(cov, 0, sizeof(cov));
  memset(tj, 0, sizeof(tj));
}
void pushdown(int o) {
  if (cov[o]) {
    tj[(o << 1)] = tj[(o << 1 | 1)] = cov[(o << 1)] = cov[(o << 1 | 1)] =
        cov[o];
    cov[o] = 0;
  }
}
void modify(int o, int l, int r, int pl, int pr, int x) {
  if (l >= pl && r <= pr) {
    tj[o] = cov[o] = x;
    return;
  }
  if (l > pr || r < pl) return;
  pushdown(o);
  int mid = (l + r) >> 1;
  modify((o << 1), l, mid, pl, pr, x),
      modify((o << 1 | 1), mid + 1, r, pl, pr, x);
}
int query(int o, int l, int r, int p) {
  if (l == r) return tj[o];
  pushdown(o);
  int mid = (l + r) >> 1;
  return mid >= p ? query((o << 1), l, mid, p)
                  : query((o << 1 | 1), mid + 1, r, p);
}
void work() {
  sort(v.begin(), v.end(), [&](const node &p, const node &q) {
    int x = min(p.a, p.c);
    int y = min(q.a, q.c);
    return x != y ? x < y : p.id < q.id;
  });
  clear_tree();
  for (auto &p : v) {
    if (p.b == p.d && p.a > p.c) {
      int t = query(1, 0, m, p.b);
      fa[0][p.id] = t;
      if (!t)
        F[0][p.id] = inf;
      else
        F[0][p.id] = abs(p.c - A[t].c) + abs(p.d - A[t].d);
    }
    if (p.id <= n) modify(1, 0, m, min(p.b, p.d), max(p.b, p.d), p.id);
  }
}
void rotate() {
  for (auto &p : v) {
    int a = p.a, b = p.b, c = p.c, d = p.d;
    p.a = b;
    p.b = m - a;
    p.c = d;
    p.d = m - c;
  }
  for (int i = 1; i <= n; i++) {
    int a = A[i].a, b = A[i].b, c = A[i].c, d = A[i].d;
    A[i] = (node){b, m - a, d, m - c, A[i].id};
  }
}
void build() {
  for (int i = 1; i < 50; ++i)
    for (int j = 1; j <= n + q; j++) {
      fa[i][j] = fa[i - 1][fa[i - 1][j]];
      F[i][j] = F[i - 1][j] + F[i - 1][fa[i - 1][j]];
      if (F[i][j] > inf) F[i][j] = inf;
    }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, a, b, c, d; i <= n; i++) {
    scanf("%d%d%d%d", &a, &b, &c, &d);
    v.push_back(A[i] = (node){a, b, c, d, i});
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int x, y;
    char opt[3];
    scanf("%d%d%s%lld", &x, &y, opt, T + i);
    int a, b, c, d;
    a = c = x;
    b = d = y;
    switch (*opt) {
      case 'R':
        --a;
        break;
      case 'L':
        ++a;
        break;
      case 'U':
        --b;
        break;
      case 'D':
        ++b;
        break;
    };
    v.push_back(A[i + n] = (node){a, b, c, d, i + n});
  }
  for (int t = 4; t; t--) {
    work();
    rotate();
  }
  build();
  for (int i = 1; i <= q; i++) {
    long long t = T[i];
    int u = i + n, v;
    for (int j = 49; ~j; --j)
      if (F[j][u] <= t) t -= F[j][u], u = fa[j][u];
    v = fa[0][u];
    int dx, dy;
    long long r, x = A[u].c, y = A[u].d;
    dx = A[u].c - A[u].a, dy = A[u].d - A[u].b;
    if (dx) dx /= abs(dx);
    if (dy) dy /= abs(dy);
    if (!v)
      r = inf;
    else if (dx) {
      if (A[v].a <= A[u].c && A[v].c > A[u].c)
        r = 0;
      else if (A[v].a >= A[u].c && A[v].c < A[u].c)
        r = 0;
      else
        r = abs(A[u].c - A[v].c);
    } else if (dy) {
      if (A[v].b <= A[u].d && A[v].d > A[u].d)
        r = 0;
      else if (A[v].b >= A[u].d && A[v].d < A[u].d)
        r = 0;
      else
        r = abs(A[u].d - A[v].d);
    }
    x += dx * min(t, r);
    y += dy * min(t, r);
    t -= min(t, r);
    if (v) {
      dx = A[v].c - A[v].a, dy = A[v].d - A[v].b;
      if (dx) dx /= abs(dx);
      if (dy) dy /= abs(dy);
      x += dx * t;
      y += dy * t;
    }
    x = max(0ll, min<long long>(x, m));
    y = max(0ll, min<long long>(y, m));
    printf("%lld %lld\n", x, y);
  }
  return 0;
}
