#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline", "fast-math", "unroll-loops", \
                     "no-stack-protector")
using namespace std;
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
                                       qu[55];
int f, qr;
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
}
inline void putc(char x) {
  *oS++ = x;
  if (oS == oT) flush();
}
template <class T>
inline void getc(T &x) {
  for (c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++);
       !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') ||
         (c >= 'A' && c <= 'Z'));
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    ;
  x = c;
}
template <class I>
inline void rd(I &x) {
  for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-') f = -1;
  for (x = 0; c <= '9' && c >= '0';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    x = x * 10 + (c & 15);
  x *= f;
}
template <class I>
inline void print(I x) {
  if (!x) putc('0');
  if (x < 0) putc('-'), x = -x;
  while (x) qu[++qr] = x % 10 + '0', x /= 10;
  while (qr) putc(qu[qr--]);
}
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_flusher_;
}  // namespace io
using io ::getc;
using io ::print;
using io ::putc;
using io ::rd;
const int MAXN = 4e5 + 5;
namespace Seg {
int tag[MAXN << 2];
void down(int x) {
  if (tag[x] == -1) return;
  tag[(x << 1)] = tag[(x << 1 | 1)] = tag[x];
  tag[x] = -1;
}
void init(int x, int l, int r) {
  tag[x] = -1;
  if (l == r) return;
  int mid = l + r >> 1;
  init((x << 1), l, mid);
  init((x << 1 | 1), mid + 1, r);
}
int query(int x, int l, int r, int pos) {
  if (l == r) return tag[x];
  int mid = l + r >> 1;
  down(x);
  if (pos <= mid)
    return query((x << 1), l, mid, pos);
  else
    return query((x << 1 | 1), mid + 1, r, pos);
}
void fix(int x, int l, int r, int ql, int qr, int id) {
  if (ql <= l && r <= qr) {
    tag[x] = id;
    return;
  }
  down(x);
  int mid = l + r >> 1;
  if (ql <= mid) fix((x << 1), l, mid, ql, qr, id);
  if (qr > mid) fix((x << 1 | 1), mid + 1, r, ql, qr, id);
}
}  // namespace Seg
struct Arrow {
  int X1, Y1, X2, Y2, dirx, diry, id, Maxx, Maxy, Minx, Miny;
  bool is;
  void init() {
    Maxx = X1 > X2 ? X1 : X2;
    Minx = X1 + X2 - Maxx;
    Maxy = Y1 > Y2 ? Y1 : Y2;
    Miny = Y1 + Y2 - Maxy;
  }
  bool operator<(const Arrow &x) const { return id < x.id; }
} a[MAXN << 1];
int m, n, Q, cnt, fa[MAXN][51];
long long dis[MAXN][51];
void link(int x, int f, int v) {
  fa[x][0] = f;
  dis[x][0] = v;
}
void solve(int dirx, int diry) {
  Seg::init(1, 0, m);
  for (int i = 1; i <= n + Q; i++) {
    if (a[i].is) {
      if (a[i].dirx != dirx || a[i].diry != diry) {
        if (diry)
          Seg::fix(1, 0, m, a[i].Minx, a[i].Maxx, i);
        else
          Seg::fix(1, 0, m, a[i].Miny, a[i].Maxy, i);
      }
    }
    if (a[i].dirx == dirx && a[i].diry == diry) {
      if (a[i].dirx) {
        int nxt = Seg::query(1, 0, m, a[i].Y1);
        ++cnt;
        a[cnt].id = cnt;
        a[cnt].Y1 = a[i].Y1;
        if (nxt == -1) {
          a[cnt].X1 = (dirx > 0) ? m : 0;
          link(a[i].id, cnt, abs(a[cnt].X1 - a[i].X2));
          link(cnt, cnt, 1);
        } else {
          if (a[nxt].dirx > 0) {
            a[cnt].X1 = min(a[i].X2, a[nxt].X2);
            link(cnt, a[nxt].id, max(0, a[nxt].X2 - a[i].X2));
          } else if (a[nxt].dirx < 0) {
            a[cnt].X1 = max(a[i].X2, a[nxt].X2);
            link(cnt, a[nxt].id, max(0, a[i].X2 - a[nxt].X2));
          } else {
            link(cnt, a[nxt].id, abs(a[nxt].Y2 - a[i].Y2));
            a[cnt].X1 = a[nxt].X1;
          }
          if (a[i].dirx < 0) {
            link(a[i].id, cnt, max(0, a[i].X2 - a[nxt].X2));
          } else {
            link(a[i].id, cnt, max(0, a[nxt].X2 - a[i].X2));
          }
        }
      } else {
        int nxt = Seg::query(1, 0, m, a[i].X1);
        ++cnt;
        a[cnt].id = cnt;
        a[cnt].X1 = a[i].X1;
        if (nxt == -1) {
          a[cnt].Y1 = (diry > 0) ? m : 0;
          link(a[i].id, cnt, abs(a[cnt].Y1 - a[i].Y2));
          link(cnt, cnt, 1);
        } else {
          if (a[nxt].diry > 0) {
            a[cnt].Y1 = min(a[i].Y2, a[nxt].Y2);
            link(cnt, a[nxt].id, max(0, a[nxt].Y2 - a[i].Y2));
          } else if (a[nxt].diry < 0) {
            a[cnt].Y1 = max(a[i].Y2, a[nxt].Y2);
            link(cnt, a[nxt].id, max(0, a[i].Y2 - a[nxt].Y2));
          } else {
            link(cnt, a[nxt].id, abs(a[nxt].X2 - a[i].X2));
            a[cnt].Y1 = a[nxt].Y1;
          }
          if (a[i].diry < 0)
            link(a[i].id, cnt, max(0, a[i].Y2 - a[nxt].Y2));
          else
            link(a[i].id, cnt, max(0, a[nxt].Y2 - a[i].Y2));
        }
      }
      a[cnt].X2 = a[cnt].X1;
      a[cnt].Y2 = a[cnt].Y1;
    }
  }
}
bool cmp1(const Arrow &x, const Arrow &y) {
  return (x.Maxx != y.Maxx) ? x.Maxx > y.Maxx : x.is > y.is;
}
bool cmp2(const Arrow &x, const Arrow &y) {
  return (x.Minx != y.Minx) ? x.Minx < y.Minx : x.is > y.is;
}
bool cmp3(const Arrow &x, const Arrow &y) {
  return (x.Maxy != y.Maxy) ? x.Maxy > y.Maxy : x.is > y.is;
}
bool cmp4(const Arrow &x, const Arrow &y) {
  return (x.Miny != y.Miny) ? x.Miny < y.Miny : x.is > y.is;
}
long long T[MAXN];
int Dir(int x) { return x > 0 ? 1 : (x < 0 ? (-1) : 0); }
int main() {
  rd(n);
  rd(m);
  for (int i = 1; i <= n; i++) {
    rd(a[i].X1);
    rd(a[i].Y1);
    rd(a[i].X2);
    rd(a[i].Y2);
    a[i].is = 1;
    a[i].id = i;
    a[i].dirx = Dir(a[i].X2 - a[i].X1);
    a[i].diry = Dir(a[i].Y2 - a[i].Y1);
    if (a[i].dirx == 0 && a[i].diry == 0) n--, i--;
  }
  rd(Q);
  for (int i = 1; i <= Q; i++) {
    rd(a[i + n].X1);
    rd(a[i + n].Y1);
    char c;
    a[i + n].is = 0;
    a[i + n].id = i + n;
    getc(c);
    if (c == 'R')
      a[i + n].dirx = 1, a[i + n].diry = 0;
    else if (c == 'L')
      a[i + n].dirx = -1, a[i + n].diry = 0;
    else if (c == 'D')
      a[i + n].dirx = 0, a[i + n].diry = -1;
    else
      a[i + n].dirx = 0, a[i + n].diry = 1;
    a[i + n].X2 = a[i + n].X1;
    a[i + n].Y2 = a[i + n].Y1;
    rd(T[i]);
  }
  cnt = n + Q;
  for (int i = 1; i <= cnt; i++) a[i].init();
  sort(a + 1, a + n + Q + 1, cmp1);
  solve(1, 0);
  sort(a + 1, a + n + Q + 1, cmp2);
  solve(-1, 0);
  sort(a + 1, a + n + Q + 1, cmp3);
  solve(0, 1);
  sort(a + 1, a + n + Q + 1, cmp4);
  solve(0, -1);
  sort(a + 1, a + cnt + 1);
  for (int i = 1; i <= 50; i++)
    for (int j = 1; j <= cnt; j++) {
      fa[j][i] = fa[fa[j][i - 1]][i - 1];
      dis[j][i] = dis[j][i - 1] + dis[fa[j][i - 1]][i - 1];
      dis[j][i] = dis[j][i] < 2e18 ? dis[j][i] : 2e18;
    }
  for (int i = n + 1, Ansx, Ansy; i <= n + Q; i++) {
    int x = i;
    long long t = T[i - n];
    for (int j = 50; j >= 0; j--)
      if (t >= dis[x][j]) {
        t -= dis[x][j], x = fa[x][j];
      }
    Ansx = a[x].X2;
    Ansy = a[x].Y2;
    if (a[x].X1 < a[fa[x][0]].X2)
      Ansx += t;
    else if (a[x].X1 > a[fa[x][0]].X2)
      Ansx -= t;
    else if (a[x].Y1 < a[fa[x][0]].Y2)
      Ansy += t;
    else if (a[x].Y1 > a[fa[x][0]].Y2)
      Ansy -= t;
    print(Ansx);
    putc(' ');
    print(Ansy);
    putc('\n');
  }
  return 0;
}
