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;
bool debug = false;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
long long ln, lk, lm;
pair<int, int> from[100005], to[100005];
int dir[100005], q, ans[100005 * 2];
char o[2];
string DIR = "URDL";
struct query {
  int x, y, id, d;
  long long t;
} qy[100005];
int getdir(int x0, int y0, int x1, int y1) {
  if (x0 == x1) {
    if (y0 < y1)
      return 0;
    else
      return 2;
  } else {
    if (x0 < x1)
      return 1;
    else
      return 3;
  }
}
struct EVENT {
  int tim, type, id;
  int x;
  int l, r;
} eve[100005 * 2];
struct segTree {
  int col[4 * 100005];
  void pushDown(int rt) {
    if (col[rt] != -1) {
      col[(rt << 1)] = col[rt];
      col[(rt << 1 | 1)] = col[rt];
      col[rt] = -1;
    }
  }
  void update(int rt, int l, int r, int L, int R, int k) {
    if (L <= l && r <= R) {
      col[rt] = k;
      return;
    }
    pushDown(rt);
    int mid = (l + r) / 2;
    if (L <= mid) update((rt << 1), l, mid, L, R, k);
    if (R > mid) update((rt << 1 | 1), mid + 1, r, L, R, k);
  }
  int query(int rt, int l, int r, int x) {
    if (l == r) return col[rt];
    pushDown(rt);
    int mid = (l + r) / 2;
    if (x <= mid)
      return query((rt << 1), l, mid, x);
    else
      return query((rt << 1 | 1), mid + 1, r, x);
  }
} T;
bool cmp(const EVENT &a, const EVENT &b) {
  return a.tim > b.tim || (a.tim == b.tim && a.type > b.type);
}
int ff[61][100005], d2[100005];
long long d1[61][100005];
void pre() {
  int tot = 0;
  for (int(k) = 0; (k) < (int)(4); (k)++) {
    tot = 0;
    for (int(i) = 1; (i) <= (int)(n + q); (i)++) {
      int x, y, dd;
      if (i <= n) {
        x = to[i].first + dx[k];
        y = to[i].second + dy[k];
        dd = dir[i];
      } else {
        x = qy[i - n].x;
        y = qy[i - n].y;
        dd = qy[i - n].d;
      }
      if (dd != k) continue;
      tot++;
      eve[tot].tim = x * dx[k] + y * dy[k];
      eve[tot].type = 0;
      if (k & 1)
        eve[tot].x = y;
      else
        eve[tot].x = x;
      eve[tot].id = i;
    }
    for (int(i) = 1; (i) <= (int)(n); (i)++) {
      tot++;
      eve[tot].tim = max(from[i].first * dx[k] + from[i].second * dy[k],
                         to[i].first * dx[k] + to[i].second * dy[k]);
      eve[tot].type = 1;
      if (k & 1) {
        eve[tot].l = min(from[i].second, to[i].second);
        eve[tot].r = max(from[i].second, to[i].second);
      } else {
        eve[tot].l = min(from[i].first, to[i].first);
        eve[tot].r = max(from[i].first, to[i].first);
      }
      eve[tot].id = i;
    }
    sort(eve + 1, eve + tot + 1, cmp);
    T.update(1, 0, m, 0, m, 0);
    for (int(i) = 1; (i) <= (int)(tot); (i)++) {
      if (eve[i].type == 0)
        ans[eve[i].id] = T.query(1, 0, m, eve[i].x);
      else
        T.update(1, 0, m, eve[i].l, eve[i].r, eve[i].id);
    }
  }
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    int x, y, len, fa = ans[i];
    ff[0][i] = fa;
    if (!fa) continue;
    if (dir[i] & 1)
      len = min(abs(to[i].first - to[fa].first),
                abs(to[i].first - from[fa].first));
    else
      len = min(abs(to[i].second - to[fa].second),
                abs(to[i].second - from[fa].second));
    d2[i] = len;
    x = to[i].first + len * dx[dir[i]];
    y = to[i].second + len * dy[dir[i]];
    d1[0][i] = d2[i] + abs(x - to[fa].first) + abs(y - to[fa].second);
  }
  for (int(i) = 1; (i) <= (int)(50); (i)++) {
    for (int(j) = 1; (j) <= (int)(n); (j)++) {
      ff[i][j] = ff[i - 1][ff[i - 1][j]];
      d1[i][j] = d1[i - 1][j] + d1[i - 1][ff[i - 1][j]];
      if (d1[i][j] > (1LL << 60)) d1[i][j] = (1LL << 60);
    }
  }
}
void walk(long long &x, long long &y, int dir, long long t) {
  x = min((long long)m, max(0ll, x + t * dx[dir]));
  y = min((long long)m, max(0ll, y + t * dy[dir]));
}
int main() {
  scanf("%d%d", &n, &m);
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    scanf("%d%d%d%d", &from[i].first, &from[i].second, &to[i].first,
          &to[i].second);
    dir[i] = getdir(from[i].first, from[i].second, to[i].first, to[i].second);
  }
  scanf("%d", &q);
  for (int(i) = 1; (i) <= (int)(q); (i)++) {
    scanf("%d%d%s%lld", &qy[i].x, &qy[i].y, o, &qy[i].t);
    for (int(k) = 0; (k) < (int)(4); (k)++)
      if (DIR[k] == o[0]) qy[i].d = k;
  }
  pre();
  for (int(i) = 1; (i) <= (int)(q); (i)++) {
    long long x = qy[i].x, y = qy[i].y, w = qy[i].d, u = ans[i + n], dis1, dis2;
    long long t = qy[i].t;
    if (w & 1) {
      if (((x > from[u].first) ^ (x > to[u].first)) == 1)
        dis1 = 0;
      else
        dis1 = min(abs(x - from[u].first), abs(x - to[u].first));
    } else {
      if (((y > from[u].second) ^ (y > to[u].second)) == 1)
        dis1 = 0;
      else
        dis1 = min(abs(y - from[u].second), abs(y - to[u].second));
    }
    if (!u || t < dis1) {
      walk(x, y, w, t);
      printf("%lld %lld\n", x, y);
      continue;
    }
    t -= dis1;
    walk(x, y, w, dis1);
    dis2 = abs(x - to[u].first) + abs(y - to[u].second);
    if (t < dis2) {
      walk(x, y, dir[u], t);
      printf("%lld %lld\n", x, y);
      continue;
    }
    t -= dis2;
    walk(x, y, dir[u], dis2);
    for (int j = 50; j >= 0; --j)
      if (ff[j][u] && t >= d1[j][u]) {
        t -= d1[j][u];
        u = ff[j][u];
        x = to[u].first;
        y = to[u].second;
      }
    if (!ff[0][u] || t < d2[u]) {
      walk(x, y, dir[u], t);
      printf("%lld %lld\n", x, y);
      continue;
    }
    t -= d2[u];
    walk(x, y, dir[u], d2[u]);
    walk(x, y, dir[ff[0][u]], t);
    printf("%lld %lld\n", x, y);
  }
  return 0;
}
