#include <bits/stdc++.h>
using namespace std;
struct point {
  int x1, x2, y1, y2, kind, id, dir;
  int mxx, mnx, mxy, mny;
  inline int get_dir() {
    if (x1 ^ x2)
      dir = (x1 < x2 ? 0 : 2);
    else
      dir = (y1 < y2 ? 3 : 1);
    return 0;
  }
  inline int init() {
    return mxx = max(x1, x2), mnx = min(x1, x2), mxy = max(y1, y2),
           mny = min(y1, y2);
  }
  void show() {
    cerr << "kind"
         << "=" << kind << " ",
        cerr << "x1"
             << "=" << x1 << " ",
        cerr << "y1"
             << "=" << y1 << " ",
        cerr << "x2"
             << "=" << x2 << " ",
        cerr << "y2"
             << "=" << y2 << " ",
        cerr << "dir"
             << "=" << dir << " ",
        cerr << "id"
             << "=" << id << endl;
  }
} a[400010];
inline int get_dir(int x1, int y1, int x2, int y2) {
  if (x1 ^ x2)
    return x1 < x2 ? 0 : 2;
  else
    return y1 < y2 ? 3 : 1;
}
inline bool xcmp1(const point &p1, const point &p2) {
  if (p1.mnx ^ p2.mnx)
    return p1.mnx < p2.mnx;
  else
    return p1.kind < p2.kind;
}
inline bool xcmp2(const point &p1, const point &p2) {
  if (p1.mxx ^ p2.mxx)
    return p1.mxx > p2.mxx;
  else
    return p1.kind < p2.kind;
}
inline bool ycmp1(const point &p1, const point &p2) {
  if (p1.mxy ^ p2.mxy)
    return p1.mxy > p2.mxy;
  else
    return p1.kind < p2.kind;
}
inline bool ycmp2(const point &p1, const point &p2) {
  if (p1.mny ^ p2.mny)
    return p1.mny < p2.mny;
  else
    return p1.kind < p2.kind;
}
inline int gabs(int x) { return x < 0 ? -x : x; }
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, -1, 0, 1};
int to[400010], state[400010], up[400010][24], val[400010];
bool on_cir[400010];
long long dis[400010][24];
int X[400010], Y[400010];
long long qt[400010], cl[400010], ansx[400010], ansy[400010];
struct edges {
  int to, pre, wgt;
} e[400010 << 1];
int h[400010], etop;
inline int add_edge(int u, int v, int w) {
  e[++etop].to = v, e[etop].pre = h[u];
  return e[etop].wgt = w, h[u] = etop;
}
struct segment {
  int l, r, v;
  bool pt;
  segment *ch[2];
} * rt;
int build(segment *&rt, int l, int r) {
  rt = new segment;
  rt->l = l, rt->r = r;
  int mid = (l + r) >> 1;
  rt->pt = true, rt->v = 0;
  if (l == r) return rt->v = 0;
  build(rt->ch[0], l, mid), build(rt->ch[1], mid + 1, r);
  return 0;
}
inline int push_down(segment *&rt) {
  return rt->ch[0]->v = rt->ch[1]->v = rt->v,
         rt->ch[0]->pt = rt->ch[1]->pt = true, rt->pt = false;
}
int update(segment *&rt, int s, int t, int v) {
  int l = rt->l, r = rt->r, mid = (l + r) >> 1;
  if (s <= l && r <= t) return rt->v = v, rt->pt = true;
  if (rt->pt) push_down(rt);
  if (s <= mid) update(rt->ch[0], s, t, v);
  if (mid < t) update(rt->ch[1], s, t, v);
  return 0;
}
int query(segment *&rt, int p) {
  int l = rt->l, r = rt->r, mid = (l + r) >> 1;
  if (l == r) return rt->v;
  if (rt->pt) push_down(rt);
  if (p <= mid)
    return query(rt->ch[0], p);
  else
    return query(rt->ch[1], p);
}
int get_tree(int x, int f) {
  if (!on_cir[x])
    for (int i = 1; i < 24; i++)
      up[x][i] = up[up[x][i - 1]][i - 1],
      dis[x][i] = dis[x][i - 1] + dis[up[x][i - 1]][i - 1];
  for (int i = h[x], y; i; i = e[i].pre)
    if ((y = e[i].to) ^ f) up[y][0] = x, dis[y][0] = e[i].wgt, get_tree(y, x);
  return 0;
}
vector<int> vc;
bool vis[400010];
stack<int> stc;
int get_val(int x) {
  vc.clear(), vc.push_back(0);
  long long ccl = 0;
  while (!vis[x]) vis[x] = true, ccl += val[x], vc.push_back(x = to[x]);
  for (int i = 1; i < (int)vc.size(); i++)
    up[vc[i]][0] = to[vc[i]], dis[vc[i]][0] = val[vc[i]], cl[vc[i]] = ccl;
  for (int j = 1; j < 24; j++)
    for (int i = 1; i < (int)vc.size(); i++)
      up[vc[i]][j] = up[up[vc[i]][j - 1]][j - 1],
      dis[vc[i]][j] = dis[vc[i]][j - 1] + dis[up[vc[i]][j - 1]][j - 1];
  for (int i = 1; i < (int)vc.size(); i++) get_tree(vc[i], to[vc[i]]);
  return 0;
}
int main() {
  int m, n;
  scanf("%d%d", &m, &n);
  build(rt, 1, n + 1);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d%d", &a[i].x1, &a[i].y1, &a[i].x2, &a[i].y2);
    a[i].id = i, a[i].get_dir(), a[i].kind = 0;
  }
  int q, pc;
  scanf("%d", &q), pc = m + q;
  for (int i = 1; i <= q; i++) {
    char ch;
    scanf("%d%d %c%lld", &a[i + m].x1, &a[i + m].y1, &ch, &qt[i]);
    a[i + m].id = i + m, a[i + m].kind = 1;
    a[i + m].x2 = a[i + m].x1, a[i + m].y2 = a[i + m].y1;
    if (ch == 'R') a[i + m].dir = 0;
    if (ch == 'D') a[i + m].dir = 1;
    if (ch == 'L') a[i + m].dir = 2;
    if (ch == 'U') a[i + m].dir = 3;
  }
  for (int i = 1; i <= m + q; i++) a[i].init();
  sort(a + 1, a + m + q + 1, xcmp1), update(rt, 1, n + 1, 0);
  for (int i = 1; i <= m + q; i++) {
    if (!a[i].kind) {
      if (a[i].dir == 3)
        update(rt, a[i].y1 + 1, a[i].y2 + 1, i);
      else if (a[i].dir == 1)
        update(rt, a[i].y2 + 1, a[i].y1 + 1, i);
      else if (a[i].dir == 0)
        update(rt, a[i].y1 + 1, a[i].y2 + 1, i);
    }
    if (a[i].dir == 2) {
      int t = query(rt, a[i].y1 + 1);
      pc++, a[pc].id = pc, a[pc].y1 = a[i].y1;
      if (t) {
        if (a[t].dir != 0)
          to[pc] = a[t].id, val[pc] = gabs(a[i].y2 - a[t].y2),
          a[pc].x1 = a[t].x1;
        else
          to[pc] = a[t].id, val[pc] = max(0, a[t].x2 - a[i].x2),
          a[pc].x1 = min(a[t].x2, a[i].x2);
        to[a[i].id] = pc, val[a[i].id] = max(0, a[i].x2 - a[t].x2);
      } else
        to[pc] = pc, val[pc] = 1, to[a[i].id] = pc, val[a[i].id] = a[i].x2,
        a[pc].x1 = 0;
    }
  }
  sort(a + 1, a + m + q + 1, xcmp2), update(rt, 1, n + 1, 0);
  for (int i = 1; i <= m + q; i++) {
    if (!a[i].kind) {
      if (a[i].dir == 3)
        update(rt, a[i].y1 + 1, a[i].y2 + 1, i);
      else if (a[i].dir == 1)
        update(rt, a[i].y2 + 1, a[i].y1 + 1, i);
      else if (a[i].dir == 2)
        update(rt, a[i].y1 + 1, a[i].y2 + 1, i);
    }
    if (a[i].dir == 0) {
      int t = query(rt, a[i].y1 + 1);
      pc++, a[pc].id = pc, a[pc].y1 = a[i].y1;
      if (t) {
        if (a[t].dir != 2)
          to[pc] = a[t].id, val[pc] = gabs(a[i].y2 - a[t].y2),
          a[pc].x1 = a[t].x1;
        else
          to[pc] = a[t].id, val[pc] = max(0, a[i].x2 - a[t].x2),
          a[pc].x1 = max(a[t].x2, a[i].x2);
        to[a[i].id] = pc, val[a[i].id] = max(0, a[t].x2 - a[i].x2);
      } else
        to[pc] = pc, val[pc] = 1, to[a[i].id] = pc, val[a[i].id] = n - a[i].x2,
        a[pc].x1 = n;
    }
  }
  sort(a + 1, a + m + q + 1, ycmp1), update(rt, 1, n + 1, 0);
  for (int i = 1; i <= m + q; i++) {
    if (!a[i].kind) {
      if (a[i].dir == 2)
        update(rt, a[i].x2 + 1, a[i].x1 + 1, i);
      else if (a[i].dir == 0)
        update(rt, a[i].x1 + 1, a[i].x2 + 1, i);
      else if (a[i].dir == 1)
        update(rt, a[i].x1 + 1, a[i].x2 + 1, i);
    }
    if (a[i].dir == 3) {
      int t = query(rt, a[i].x1 + 1);
      pc++, a[pc].id = pc, a[pc].x1 = a[i].x1;
      if (t) {
        if (a[t].dir != 1)
          to[pc] = a[t].id, val[pc] = gabs(a[i].x2 - a[t].x2),
          a[pc].y1 = a[t].y1;
        else
          to[pc] = a[t].id, val[pc] = max(0, a[i].y2 - a[t].y2),
          a[pc].y1 = max(a[t].y2, a[i].y2);
        to[a[i].id] = pc, val[a[i].id] = max(0, a[t].y2 - a[i].y2);
      } else
        to[pc] = pc, val[pc] = 1, to[a[i].id] = pc, val[a[i].id] = n - a[i].y2,
        a[pc].y1 = n;
    }
  }
  sort(a + 1, a + m + q + 1, ycmp2), update(rt, 1, n + 1, 0);
  for (int i = 1; i <= m + q; i++) {
    if (!a[i].kind) {
      if (a[i].dir == 2)
        update(rt, a[i].x2 + 1, a[i].x1 + 1, i);
      else if (a[i].dir == 0)
        update(rt, a[i].x1 + 1, a[i].x2 + 1, i);
      else if (a[i].dir == 3)
        update(rt, a[i].x1 + 1, a[i].x2 + 1, i);
    }
    if (a[i].dir == 1) {
      int t = query(rt, a[i].x1 + 1);
      pc++, a[pc].id = pc, a[pc].x1 = a[i].x1;
      if (t) {
        if (a[t].dir != 3)
          to[pc] = a[t].id, val[pc] = gabs(a[i].x2 - a[t].x2),
          a[pc].y1 = a[t].y1;
        else
          to[pc] = a[t].id, val[pc] = max(0, a[t].y2 - a[i].y2),
          a[pc].y1 = min(a[t].y2, a[i].y2);
        to[a[i].id] = pc, val[a[i].id] = max(0, a[i].y2 - a[t].y2);
      } else
        to[pc] = pc, val[pc] = 1, to[a[i].id] = pc, val[a[i].id] = a[i].y2,
        a[pc].y1 = 0;
    }
  }
  for (int i = m + q + 1; i <= pc; i++)
    a[i].x2 = a[i].x1, a[i].y2 = a[i].y1, a[i].init();
  for (int i = 1, x, f, t; i <= pc; i++)
    if (state[i] != 2) {
      stc.push(x = i), state[x] = 1, f = true, t = 0;
      while (state[to[x]] != 2)
        if (!state[to[x]])
          stc.push(x = to[x]), state[x] = 1;
        else
          on_cir[to[x]] = true, t = to[x], state[to[x]] = 2;
      while (!stc.empty())
        on_cir[stc.top()] = (f && t), f &= ((t == stc.top()) ^ 1),
        state[stc.top()] = 2, stc.pop();
    }
  for (int i = 1; i <= pc; i++)
    if (on_cir[i])
      add_edge(i, to[i], val[i]);
    else
      add_edge(to[i], i, val[i]);
  for (int i = 1; i <= pc; i++)
    if (on_cir[i] && !vis[i]) get_val(i);
  for (int i = 1; i <= pc; i++) X[a[i].id] = a[i].x2, Y[a[i].id] = a[i].y2;
  for (int i = 1; i <= pc; i++)
    if (a[i].kind) {
      int k = a[i].id, c = k - m;
      long long t = qt[c];
      for (int j = 24 - 1; j >= 0; j--)
        if (!on_cir[up[k][j]] && t >= dis[k][j]) t -= dis[k][j], k = up[k][j];
      if (t >= val[k]) t -= val[k], k = to[k];
      if (on_cir[k]) t %= cl[k];
      for (int j = 24 - 1; j >= 0; j--)
        if (t >= dis[k][j]) t -= dis[k][j], k = up[k][j];
      int d = get_dir(X[k], Y[k], X[to[k]], Y[to[k]]);
      ansx[c] = X[k] + dx[d] * t, ansy[c] = Y[k] + dy[d] * t;
    }
  for (int i = 1; i <= q; i++) printf("%lld %lld\n", ansx[i], ansy[i]);
  return 0;
}
