#include <bits/stdc++.h>
const int kMaxn = 1e5 + 10, kLgN = 18, kInf = 0x3f3f3f3f;
typedef int IAr[kMaxn];
struct Arrow {
  std::pair<int, int> s, t;
  int id, tp, minx, maxx, miny, maxy;
  Arrow() {}
  Arrow(const std::pair<int, int>& s_, const std::pair<int, int>& t_)
      : s(s_), t(t_) {}
  int type() const {
    return s.second == t.second ? 1 + (s.first > t.first)
                                : 3 + (s.second > t.second);
  }
  void output() const { ; }
} A[kMaxn << 1], Q[kMaxn];
int n, b, q, totl, top;
int F[kMaxn][kLgN];
std::pair<int, int> G[kMaxn];
IAr vis, S, on_loop, up, to, lg2, start;
long long dep[kMaxn], D[kMaxn][kLgN], len[kMaxn], T[kMaxn];
void Climb(int& v, long long& t);
void Dfs(int u, int r);
std::pair<int, int> Move(const Arrow& a, const Arrow& b, int d);
void PreWork(int tp);
inline bool CmpX1(const Arrow& a1, const Arrow& a2) {
  return a1.maxx == a2.maxx ? a1.id < a2.id : a1.maxx > a2.maxx;
}
inline bool CmpX2(const Arrow& a1, const Arrow& a2) {
  return a1.minx == a2.minx ? a1.id < a2.id : a1.minx < a2.minx;
}
inline bool CmpY1(const Arrow& a1, const Arrow& a2) {
  return a1.maxy == a2.maxy ? a1.id < a2.id : a1.maxy > a2.maxy;
}
inline bool CmpY2(const Arrow& a1, const Arrow& a2) {
  return a1.miny == a2.miny ? a1.id < a2.id : a1.miny < a2.miny;
}
inline bool CmpId(const Arrow& a1, const Arrow& a2) { return a1.id < a2.id; }
inline int DistToEdge(const Arrow& a) {
  return a.type() & 1 ? b - (a.type() < 3 ? a.t.first : a.t.second)
                      : (a.type() < 3 ? a.t.first : a.t.second);
}
inline bool In(int first, int lb, int ub) {
  if (lb > ub) std::swap(lb, ub);
  return lb <= first && first <= ub;
}
inline int Manhattan(const std::pair<int, int>& p,
                     const std::pair<int, int>& q) {
  return std::abs(p.first - q.first) + std::abs(p.second - q.second);
}
inline std::pair<int, int> Move(const Arrow& a, int d) {
  return a.s.second == a.t.second
             ? (a.s.first < a.t.first
                    ? std::pair<int, int>(a.t.first + d, a.t.second)
                    : std::pair<int, int>(a.t.first - d, a.t.second))
             : (a.s.second < a.t.second
                    ? std::pair<int, int>(a.t.first, a.t.second + d)
                    : std::pair<int, int>(a.t.first, a.t.second - d));
}
inline void Print(const std::pair<int, int>& p) {
  printf("%d %d\n", p.first, p.second);
}
inline void Read(std::pair<int, int>& p) { scanf("%d%d", &p.first, &p.second); }
int main() {
  scanf("%d%d", &n, &b);
  for (int i = 0; i < n; i++) {
    Read(A[i].s), Read(A[i].t);
    A[i].id = i;
  }
  scanf("%d", &q);
  char IN[5];
  for (int i = n; i < n + q; i++) {
    Read(A[i].t);
    scanf("%s%lld", IN, T + (i - n));
    A[i].id = i, A[i].s = A[i].t;
    if (IN[0] == 'U') --A[i].s.second;
    if (IN[0] == 'D') ++A[i].s.second;
    if (IN[0] == 'R') --A[i].s.first;
    if (IN[0] == 'L') ++A[i].s.first;
  }
  for (int i = 0; i < n; i++) {
    A[i].minx = std::min(A[i].s.first, A[i].t.first);
    A[i].maxx = std::max(A[i].s.first, A[i].t.first);
    A[i].miny = std::min(A[i].s.second, A[i].t.second);
    A[i].maxy = std::max(A[i].s.second, A[i].t.second);
  }
  for (int i = n; i < n + q; i++) {
    A[i].minx = A[i].maxx = A[i].t.first;
    A[i].miny = A[i].maxy = A[i].t.second;
  }
  memset(to, 0xff, sizeof to);
  std::sort(A, A + n + q, CmpX1);
  PreWork(1);
  std::sort(A, A + n + q, CmpX2);
  PreWork(2);
  std::sort(A, A + n + q, CmpY1);
  PreWork(3);
  std::sort(A, A + n + q, CmpY2);
  PreWork(4);
  for (int i = 0, r = 0; i < n; i++) {
    if (!vis[i]) Dfs(i, ++r);
  }
  memset(vis, 0, sizeof vis);
  memset(F, 0xff, sizeof F);
  on_loop[n] = ++totl;
  for (int i = 0, v; i < n; i++) {
    if (on_loop[i] || vis[i]) continue;
    for (top = 0, v = i; !on_loop[v]; v = G[v].first) S[top++] = v;
    F[S[top - 1]][0] = v, dep[v] = 0;
    for (int fa = v, u = v; top; fa = v) {
      vis[v = S[--top]] = 1;
      up[v] = u;
      dep[v] = dep[fa] + (D[v][0] = G[v].second);
      for (int j = 1; j < kLgN && ~F[v][j - 1]; j++) {
        F[v][j] = F[F[v][j - 1]][j - 1];
        D[v][j] = D[v][j - 1] + D[F[v][j - 1]][j - 1];
      }
      if (top) F[S[top - 1]][0] = v;
    }
  }
  lg2[0] = -1;
  for (int i = 1; i <= n; i++) lg2[i] = lg2[i >> 1] + 1;
  for (int i = 1; i <= totl; i++) {
    top = 0;
    for (int v = start[i]; !vis[v]; v = G[v].first) {
      vis[v] = 1, S[top++] = v, up[v] = v;
      F[v][0] = G[v].first, D[v][0] = G[v].second;
      len[i] += G[v].second;
    }
    for (int j = 1, lim = lg2[top]; j <= lim; j++) {
      for (int k = 0, u; k < top; k++) {
        u = S[k];
        F[u][j] = F[F[u][j - 1]][j - 1];
        D[u][j] = D[u][j - 1] + D[F[u][j - 1]][j - 1];
      }
    }
  }
  for (int i = 0, q0 = 0, n0 = 0; i < n + q; i++) {
    if (A[i].id >= n) {
      Q[q0] = A[i];
      Q[q0++].id -= n;
    } else {
      A[n0++] = A[i];
    }
  }
  std::sort(Q, Q + q, CmpId);
  std::sort(A, A + n, CmpId);
  A[18].output();
  for (int i = 0, v; i < q; i++) {
    long long dis;
    assert(~to[i]);
    ;
    A[to[i]].output();
    if ((v = to[i]) == kInf) {
      Print(Move(Q[i], std::min((long long)DistToEdge(Q[i]), T[i])));
      continue;
    } else
      dis = Manhattan(Q[i].t, A[v].t);
    ;
    if (dis < T[i]) {
      T[i] -= dis;
      ;
      if (T[i] >= dep[v]) {
        if (up[v] == n) {
          long long tmp = dep[v];
          Climb(v, tmp);
          ;
          ;
          ;
          Print(Move(A[v], DistToEdge(A[v])));
        } else {
          T[i] -= dep[v];
          v = up[v];
          T[i] %= len[on_loop[v]];
          Climb(v, T[i]);
          Print(Move(A[v], A[G[v].first], T[i]));
        }
      } else {
        Climb(v, T[i]);
        if (G[v].first != n)
          Print(Move(A[v], A[G[v].first], T[i]));
        else
          Print(Move(A[v], T[i]));
      }
    } else {
      if (In(Q[i].t.first, A[v].s.first, A[v].t.first) &&
          In(Q[i].t.second, A[v].s.second, A[v].t.second)) {
        Q[i].s = Q[i].t;
        if (A[v].type() == 1) --Q[i].s.first;
        if (A[v].type() == 2) ++Q[i].s.first;
        if (A[v].type() == 3) --Q[i].s.second;
        if (A[v].type() == 4) ++Q[i].s.second;
      }
      Print(Move(Q[i], A[v], T[i]));
    }
  }
  return 0;
}
std::pair<int, int> Move(const Arrow& a, const Arrow& b, int d) {
  int d0 = a.type() < 3 ? std::abs(b.t.first - a.t.first)
                        : std::abs(b.t.second - a.t.second);
  ;
  if (d <= d0) return Move(a, d);
  std::pair<int, int> corner = a.type() < 3
                                   ? std::pair<int, int>(b.t.first, a.t.second)
                                   : std::pair<int, int>(a.t.first, b.t.second);
  Arrow c(corner, corner);
  if (b.type() == 1) --c.s.first;
  if (b.type() == 2) ++c.s.first;
  if (b.type() == 3) --c.s.second;
  if (b.type() == 4) ++c.s.second;
  return Move(c, d - d0);
}
namespace smt {
int col[kMaxn << 2];
void PushDown(int rt) {
  if (col[rt] == -1) return;
  col[(rt << 1)] = col[(rt << 1 | 1)] = col[rt];
  col[rt] = -1;
}
void _U(int rt, int l, int r, int ql, int qr, int v) {
  if (ql <= l && r <= qr) {
    col[rt] = v;
    return;
  }
  PushDown(rt);
  int mid = l + r >> 1;
  if (ql <= mid) _U((rt << 1), l, mid, ql, qr, v);
  if (qr > mid) _U((rt << 1 | 1), mid + 1, r, ql, qr, v);
}
int Query(int rt, int l, int r, int qp) {
  if (~col[rt]) return col[rt];
  int mid = l + r >> 1;
  if (qp <= mid) return Query((rt << 1), l, mid, qp);
  return Query((rt << 1 | 1), mid + 1, r, qp);
}
inline void Modify(int lb, int ub, int val) {
  if (lb > ub) std::swap(lb, ub);
  _U(1, 0, b, lb, ub, val);
}
}  // namespace smt
void Climb(int& v, long long& t) {
  for (int j = kLgN - 1; j >= 0 && t; j--) {
    if (~F[v][j] && F[v][j] != n && D[v][j] <= t) {
      t -= D[v][j];
      v = F[v][j];
    }
  }
}
void FindNext(const Arrow& a) {
  int t, v = smt::Query(1, 0, b, a.type() < 3 ? a.t.second : a.t.first);
  if (a.id >= n) {
    to[a.id - n] = v == kInf ? v : A[v].id;
  } else {
    t = v == kInf ? DistToEdge(a) : Manhattan(a.t, A[v].t);
    G[a.id] = std::pair<int, int>(v == kInf ? n : A[v].id, t);
  }
}
void PreWork(int tp) {
  smt::Modify(0, b, kInf);
  for (int i = 0; i < n + q; i++) {
    if (A[i].type() == tp) FindNext(A[i]);
    if (A[i].id < n) {
      if (tp < 3)
        smt::Modify(A[i].s.second, A[i].t.second, i);
      else
        smt::Modify(A[i].s.first, A[i].t.first, i);
    }
  }
}
void Dfs(int u, int r) {
  if (u == n) return;
  vis[u] = r, S[top++] = u;
  if (vis[G[u].first] == vis[u]) {
    for (++totl; S[top - 1] != G[u].first;) on_loop[S[--top]] = totl;
    on_loop[S[--top]] = totl;
    start[totl] = u;
  } else if (!vis[G[u].first])
    Dfs(G[u].first, r);
  if (top && S[top - 1] == u) --top;
}
