#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e16 + 7;
const int maxn = 1e5 + 5;
const int logn = 51;
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int n, q, col;
int g[maxn], wgh[maxn], deg[maxn], c[maxn], bel[maxn], qx[maxn], qy[maxn],
    qdir[maxn];
int par[maxn][logn];
bool vis[maxn];
long long b;
long long t[maxn], len[maxn];
long long st[maxn][logn];
map<int, int> mp;
inline void qwrite(long long x, long long y) {
  int tmp[10], tp = 0;
  if (!x) putchar('0');
  for (; x; x /= 10) tmp[++tp] = x - x / 10 * 10;
  for (; tp; --tp) putchar(tmp[tp] + 48);
  putchar(' ');
  tp = 0;
  if (!y) putchar('0');
  for (; y; y /= 10) tmp[++tp] = y - y / 10 * 10;
  for (; tp; --tp) putchar(tmp[tp] + 48);
  putchar('\n');
  return;
}
class Arrow {
 public:
  long long x0123456789, y0123456789, x1234567890, y1234567890;
  int dir;
  void calcDir() {
    if (x0123456789 > x1234567890)
      dir = 0;
    else if (x0123456789 < x1234567890)
      dir = 1;
    else if (y0123456789 > y1234567890)
      dir = 2;
    else
      dir = 3;
    return;
  }
} a[maxn];
class Point {
 public:
  int tp, id, x, y;
  Point(int _tp, int _id, int _x, int _y) : tp(_tp), id(_id), x(_x), y(_y) {}
};
class Event {
 public:
  int id, t, l, r;
  Event(int _id, int _t, int _l, int _r) : id(_id), t(_t), l(_l), r(_r) {}
  bool operator<(const Event &e) const { return t < e.t; }
};
vector<Point> pnt[4];
vector<Event> all[4];
class SegmentTree {
 private:
  int siz;
  int tag[maxn << 2];
  void pushDown(int u) {
    if (~tag[u]) tag[u << 1] = tag[u << 1 | 1] = tag[u];
    tag[u] = -1;
    return;
  }

 public:
  int size() { return siz; }
  void init() {
    for (siz = 1; siz < b; siz <<= 1)
      ;
    for (int i = (0); i < (maxn << 2); ++i) tag[i] = n;
    return;
  }
  void update(int x, int l, int r, int s, int t, int k) {
    if (s > t) swap(s, t);
    if (l >= s && r <= t) {
      tag[x] = k;
      return;
    }
    int md = l + r >> 1;
    pushDown(x);
    if (s <= md) update(x << 1, l, md, s, t, k);
    if (t > md) update(x << 1 | 1, md + 1, r, s, t, k);
    return;
  }
  int query(int x, int l, int r, int y) {
    if (l == r) return tag[x];
    int md = l + r >> 1;
    pushDown(x);
    if (y <= md)
      return query(x << 1, l, md, y);
    else
      return query(x << 1 | 1, md + 1, r, y);
  }
} seg;
inline bool cmpx(const Point &e, const Point &f) {
  return (make_pair(e.x, e.tp) < make_pair(f.x, f.tp));
}
inline bool cmpy(const Point &e, const Point &f) {
  return (make_pair(e.y, e.tp) < make_pair(f.y, f.tp));
}
inline bool cmp(const Point &e, const Event &f, int d) {
  if (!d) return make_pair(e.x, e.tp) >= make_pair(f.t, 1);
  if (d == 1) return make_pair(e.x, -e.tp) <= make_pair(f.t, -1);
  if (d == 2) return make_pair(e.y, e.tp) >= make_pair(f.t, 1);
  return make_pair(e.y, -e.tp) <= make_pair(f.t, -1);
}
inline bool inRange(int x, int l, int r) {
  return (x >= min(l, r) && x <= max(l, r));
}
inline int dis(int x, int y, int z, int d) {
  if (z == n) {
    if (!d)
      return x;
    else if (d == 1)
      return b - x;
    else if (d == 2)
      return y;
    else
      return b - y;
  }
  if (inRange(x, a[z].x0123456789, a[z].x1234567890) &&
      inRange(y, a[z].y0123456789, a[z].y1234567890))
    return 0;
  if (d < 2) {
    return min(abs(a[z].x0123456789 - x), abs(a[z].x1234567890 - x));
  } else {
    return min(abs(a[z].y0123456789 - y), abs(a[z].y1234567890 - y));
  }
}
void scan(int d) {
  seg.init();
  for (int i = 0, j = 0; i < pnt[d].size(); ++i) {
    for (; j < all[d].size() && cmp(pnt[d][i], all[d][j], d); ++j) {
      seg.update(1, 0, seg.size() - 1, all[d][j].l, all[d][j].r, all[d][j].id);
    }
    Point e = pnt[d][i];
    if (!e.tp) {
      if (d < 2)
        g[e.id] = seg.query(1, 0, seg.size() - 1, e.y);
      else
        g[e.id] = seg.query(1, 0, seg.size() - 1, e.x);
      if (g[e.id] != n)
        wgh[e.id] = abs(e.x - a[g[e.id]].x1234567890) +
                    abs(e.y - a[g[e.id]].y1234567890);
      else
        wgh[e.id] = dis(e.x, e.y, n, d);
    } else {
      if (d < 2) {
        bel[e.id] = seg.query(1, 0, seg.size() - 1, e.y);
      } else {
        bel[e.id] = seg.query(1, 0, seg.size() - 1, e.x);
      }
      t[e.id] -= dis(e.x, e.y, bel[e.id], d);
    }
  }
  return;
}
void calc() {
  sort(pnt[0].begin(), pnt[0].end(), cmpx);
  sort(pnt[1].begin(), pnt[1].end(), cmpx);
  reverse(pnt[1].begin(), pnt[1].end());
  sort(pnt[2].begin(), pnt[2].end(), cmpy);
  sort(pnt[3].begin(), pnt[3].end(), cmpy);
  reverse(pnt[3].begin(), pnt[3].end());
  for (int i = (0); i < (4); ++i) scan(i);
  return;
}
void calcSt() {
  for (int i = (0); i < (n); ++i)
    if (g[i] == n) {
      par[i][0] = -1;
      st[i][0] = 0;
    } else {
      par[i][0] = g[i];
      st[i][0] = wgh[i];
    }
  for (int i = (1); i < (logn); ++i)
    for (int u = (0); u < (n); ++u) {
      if (~par[u][i - 1]) {
        par[u][i] = par[par[u][i - 1]][i - 1];
        st[u][i] = st[par[u][i - 1]][i - 1] + st[u][i - 1];
      } else {
        par[u][i] = -1;
        st[u][i] = st[u][i - 1];
      }
      if (st[u][i] > INF) st[u][i] = INF;
    }
  return;
}
long long climb(int u, long long md) {
  long long ret = 0;
  int bound = logn;
  for (int i = bound - 1; i >= 0; --i) {
    if (~par[u][i] && (1ll << i) <= md) {
      ret += st[u][i];
      u = par[u][i];
      md -= 1ll << i;
    }
  }
  return ret;
}
void getAns(int u, long long tt) {
  if (g[u] == n) {
    long long rx = a[u].x1234567890 + dx[a[u].dir] * tt,
              ry = a[u].y1234567890 + dy[a[u].dir] * tt;
    qwrite(min(b - 1, max(rx, 0ll)), min(b - 1, max(ry, 0ll)));
    return;
  }
  bool f = (par[u][logn - 1] == -1);
  long long rt = 0, d = 0;
  if (!f) {
    int v = u;
    for (int i = logn - 1; i >= 0; --i) {
      if (~par[v][i] && !deg[par[v][i]]) {
        d += st[v][i];
        v = par[v][i];
      }
    }
    rt = 1e5 + 5;
    if (d + wgh[v] <= tt) {
      tt -= wgh[v];
      tt -= d;
      u = par[v][0];
      tt %= len[c[u]];
    }
  } else {
    int r = u;
    for (int i = logn - 1; i >= 0; --i) {
      if (~par[r][i]) {
        rt += 1ll << i;
        d += st[r][i];
        r = par[r][i];
      }
    }
    if (d <= tt) {
      tt -= d;
      long long rx = a[r].x1234567890 + tt * dx[a[r].dir],
                ry = a[r].y1234567890 + tt * dy[a[r].dir];
      qwrite(max(0ll, min(rx, b - 1)), max(0ll, min(ry, b - 1)));
      return;
    }
  }
  long long lb, rb;
  for (lb = -1, rb = rt; lb + 1 < rb;) {
    long long md = lb + rb >> 1ll;
    if (climb(u, md) <= tt)
      lb = md;
    else
      rb = md;
  }
  for (int i = logn - 1; i >= 0; --i) {
    if (~par[u][i] && (1ll << i) <= lb) {
      tt -= st[u][i];
      u = par[u][i];
      lb -= 1ll << i;
    }
  }
  long long nt = dis(a[u].x1234567890, a[u].y1234567890, g[u], a[u].dir);
  if (tt <= nt) {
    qwrite(a[u].x1234567890 + dx[a[u].dir] * tt,
           a[u].y1234567890 + dy[a[u].dir] * tt);
  } else {
    tt -= wgh[u];
    qwrite(a[g[u]].x1234567890 + dx[a[g[u]].dir] * tt,
           a[g[u]].y1234567890 + dy[a[g[u]].dir] * tt);
  }
  return;
}
void topoSort() {
  queue<int> q;
  memset(deg, 0, sizeof(deg));
  for (int i = (0); i < (n); ++i) ++deg[g[i]];
  for (int i = (0); i <= (n); ++i)
    if (!deg[i]) q.push(i);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (u == n) continue;
    --deg[g[u]];
    if (!deg[g[u]]) q.push(g[u]);
  }
  return;
}
void dfs(int u) {
  vis[u] = 1;
  len[col] += wgh[u];
  c[u] = col;
  if (u != n && !vis[g[u]]) dfs(g[u]);
  return;
}
int main() {
  scanf("%d%lld", &n, &b);
  ++b;
  for (int i = (0); i < (n); ++i) {
    scanf("%lld%lld%lld%lld", &a[i].x0123456789, &a[i].y0123456789,
          &a[i].x1234567890, &a[i].y1234567890);
    a[i].calcDir();
    pnt[a[i].dir].push_back(Point(0, i, a[i].x1234567890, a[i].y1234567890));
    all[0].push_back(Event(i, min(a[i].x0123456789, a[i].x1234567890),
                           a[i].y0123456789, a[i].y1234567890));
    all[1].push_back(Event(i, max(a[i].x0123456789, a[i].x1234567890),
                           a[i].y0123456789, a[i].y1234567890));
    all[2].push_back(Event(i, min(a[i].y0123456789, a[i].y1234567890),
                           a[i].x0123456789, a[i].x1234567890));
    all[3].push_back(Event(i, max(a[i].y0123456789, a[i].y1234567890),
                           a[i].x0123456789, a[i].x1234567890));
  }
  sort(all[0].begin(), all[0].end());
  sort(all[1].begin(), all[1].end());
  reverse(all[1].begin(), all[1].end());
  sort(all[2].begin(), all[2].end());
  sort(all[3].begin(), all[3].end());
  reverse(all[3].begin(), all[3].end());
  calc();
  scanf("%d", &q);
  for (int i = (0); i < (4); ++i) pnt[i].clear();
  for (int i = (0); i < (q); ++i) {
    char c;
    scanf("%d%d%*c%c%lld", qx + i, qy + i, &c, t + i);
    if (c == 'L')
      qdir[i] = 0;
    else if (c == 'R')
      qdir[i] = 1;
    else if (c == 'D')
      qdir[i] = 2;
    else if (c == 'U')
      qdir[i] = 3;
    pnt[qdir[i]].push_back(Point(1, i, qx[i], qy[i]));
  }
  calc();
  calcSt();
  topoSort();
  for (int i = (0); i < (n); ++i)
    if (deg[i] && !vis[i]) dfs(i), col++;
  for (int i = (0); i < (q); ++i) {
    int arr = bel[i];
    if (arr == n || t[i] <= 0) {
      t[i] += dis(qx[i], qy[i], bel[i], qdir[i]);
      long long rx = qx[i] + dx[qdir[i]] * t[i],
                ry = qy[i] + dy[qdir[i]] * t[i];
      qwrite(max(0ll, min(rx, 1ll * b - 1)), max(0ll, min(ry, 1ll * b - 1)));
      continue;
    }
    long long nx = qx[i], ny = qy[i];
    if (dis(qx[i], qy[i], bel[i], qdir[i])) {
      if (!qdir[i])
        nx = max(a[arr].x0123456789, a[arr].x1234567890);
      else if (qdir[i] == 1)
        nx = min(a[arr].x0123456789, a[arr].x1234567890);
      else if (qdir[i] == 2)
        ny = max(a[arr].y0123456789, a[arr].y1234567890);
      else
        ny = min(a[arr].y0123456789, a[arr].y1234567890);
    }
    long long nt = abs(nx - a[arr].x1234567890) + abs(ny - a[arr].y1234567890);
    if (t[i] <= nt) {
      long long rx = a[arr].x1234567890 + dx[a[arr].dir] * (t[i] - nt),
                ry = a[arr].y1234567890 + dy[a[arr].dir] * (t[i] - nt);
      qwrite(max(0ll, min(rx, b - 1)), max(0ll, min(ry, b - 1)));
    } else {
      t[i] -= nt;
      getAns(arr, t[i]);
    }
  }
  return 0;
}
