#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;
}
