#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
struct SegTree {
  int tag[N * 4];
  void clear() { memset(tag, 0, sizeof tag); }
  void pt(int id) {
    if (!tag[id]) return;
    tag[((id) << 1)] = tag[(((id) << 1) | 1)] = tag[id];
    tag[id] = 0;
  }
  void _edit(int l, int r, int ll, int rr, int id, int v) {
    if (l <= ll && rr <= r) {
      tag[id] = v;
      return;
    }
    if (r < ll || rr < l) return;
    pt(id);
    int mid = (ll + rr) >> 1;
    _edit(l, r, ll, mid, ((id) << 1), v);
    _edit(l, r, mid + 1, rr, (((id) << 1) | 1), v);
  }
  void edit(int l, int r, int v) { _edit(l, r, 0, N, 1, v); }
  int _query(int pos, int ll, int rr, int id) {
    if (ll == rr) return tag[id];
    pt(id);
    int mid = (ll + rr) >> 1;
    return pos <= mid ? _query(pos, ll, mid, ((id) << 1))
                      : _query(pos, mid + 1, rr, (((id) << 1) | 1));
  }
  int query(int pos) {
    int ans = _query(pos, 0, N, 1);
    return ans;
  }
} tr;
struct Arrow {
  int x1, y1, x2, y2;
  bool isA;
  char der;
  void readA() {
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    isA = true;
    if (y2 > y1)
      der = 'U';
    else if (y2 < y1)
      der = 'D';
    else if (x2 > x1)
      der = 'R';
    else if (x2 < x1)
      der = 'L';
  }
  void readP() {
    scanf("%d%d %c", &x2, &y2, &der);
    isA = false;
    x1 = x2;
    y1 = y2;
    if (der == 'U')
      y1--;
    else if (der == 'D')
      y1++;
    else if (der == 'R')
      x1--;
    else if (der == 'L')
      x1++;
  }
  inline bool isX() { return y1 == y2; }
  inline int yl() { return min(y1, y2); }
  inline int yu() { return max(y1, y2); }
  inline int xl() { return min(x1, x2); }
  inline int xu() { return max(x1, x2); }
  inline int tt(int x, int y) { return abs(x - x2) + abs(y - y2); }
};
const int L = 51;
int nxt[N * 2][L];
long long dis[N * 2][L];
Arrow arr[N * 2];
Arrow *qes;
long long t[N];
bool cmpR(const int a, const int b) {
  if (arr[a].xu() != arr[b].xu())
    return arr[a].xu() > arr[b].xu();
  else
    return arr[a].isA && !arr[b].isA;
}
bool cmpU(const int a, const int b) {
  if (arr[a].yu() != arr[b].yu())
    return arr[a].yu() > arr[b].yu();
  else
    return arr[a].isA && !arr[b].isA;
}
bool cmpL(const int a, const int b) {
  if (arr[a].xl() != arr[b].xl())
    return arr[a].xl() < arr[b].xl();
  else
    return arr[a].isA && !arr[b].isA;
}
bool cmpD(const int a, const int b) {
  if (arr[a].yl() != arr[b].yl())
    return arr[a].yl() < arr[b].yl();
  else
    return arr[a].isA && !arr[b].isA;
}
int tmp[N * 2];
int main() {
  int n, b, q;
  scanf("%d%d", &n, &b);
  qes = arr + n;
  for (int i = 1; i <= n; i++) {
    arr[i].readA();
  }
  scanf("%d", &q);
  int tot = n + q;
  for (int i = 1; i <= q; i++) {
    qes[i].readP();
    scanf("%lld", &t[i]);
  }
  for (int i = 1; i <= tot; i++) {
    tmp[i] = i;
  }
  nxt[0][0] = 0;
  dis[0][0] = 0;
  tr.clear();
  sort(tmp + 1, tmp + tot + 1, cmpR);
  for (int i = 1; i <= tot; i++) {
    int id = tmp[i];
    Arrow &a = arr[id];
    if (a.isX()) {
      if (a.der == 'R') {
        int nn = nxt[id][0] = tr.query(a.y2);
        dis[id][0] = nn == 0 ? b - a.x2 : arr[nn].tt(a.x2, a.y2);
      }
    }
    if (a.isA) {
      tr.edit(a.yl(), a.yu(), id);
    }
  }
  tr.clear();
  sort(tmp + 1, tmp + tot + 1, cmpL);
  for (int i = 1; i <= tot; i++) {
    int id = tmp[i];
    Arrow &a = arr[id];
    if (a.isX()) {
      if (a.der == 'L') {
        int nn = nxt[id][0] = tr.query(a.y2);
        dis[id][0] = nn == 0 ? a.x2 : arr[nn].tt(a.x2, a.y2);
      }
    }
    if (a.isA) {
      tr.edit(a.yl(), a.yu(), id);
    }
  }
  tr.clear();
  sort(tmp + 1, tmp + tot + 1, cmpU);
  for (int i = 1; i <= tot; i++) {
    int id = tmp[i];
    Arrow &a = arr[id];
    if (!a.isX()) {
      if (a.der == 'U') {
        int nn = nxt[id][0] = tr.query(a.x2);
        dis[id][0] = nn == 0 ? b - a.y2 : arr[nn].tt(a.x2, a.y2);
      }
    }
    if (a.isA) {
      tr.edit(a.xl(), a.xu(), id);
    }
  }
  tr.clear();
  sort(tmp + 1, tmp + tot + 1, cmpD);
  for (int i = 1; i <= tot; i++) {
    int id = tmp[i];
    Arrow &a = arr[id];
    if (!a.isX()) {
      if (a.der == 'D') {
        int nn = nxt[id][0] = tr.query(a.x2);
        dis[id][0] = nn == 0 ? a.y2 : arr[nn].tt(a.x2, a.y2);
      }
    }
    if (a.isA) {
      tr.edit(a.xl(), a.xu(), id);
    }
  }
  for (int i = 1; i < L; i++) {
    for (int j = 0; j <= tot; j++) {
      int nn = nxt[j][i - 1];
      nxt[j][i] = nxt[nn][i - 1];
      dis[j][i] = dis[j][i - 1] + dis[nn][i - 1];
      if (dis[j][i] > 1e16) dis[j][i] = 1e16;
    }
  }
  for (int i = 1; i <= q; i++) {
    int cur = n + i;
    int x = qes[i].x2, y = qes[i].y2;
    for (int j = L - 1; j >= 0; j--) {
      if (nxt[cur][j] != 0 && t[i] >= dis[cur][j]) {
        t[i] -= dis[cur][j];
        cur = nxt[cur][j];
        x = arr[cur].x2;
        y = arr[cur].y2;
      }
    }
    int nn = nxt[cur][0];
    int vv;
    if (nn == 0) {
      if (arr[cur].der == 'R')
        vv = b - x;
      else if (arr[cur].der == 'L')
        vv = x;
      else if (arr[cur].der == 'U')
        vv = b - y;
      else if (arr[cur].der == 'D')
        vv = y;
    } else {
      if (arr[cur].der == 'R')
        vv = arr[nn].xl() - x;
      else if (arr[cur].der == 'L')
        vv = x - arr[nn].xu();
      else if (arr[cur].der == 'U')
        vv = arr[nn].yl() - y;
      else if (arr[cur].der == 'D')
        vv = y - arr[nn].yu();
    }
    vv = max(vv, 0);
    if (t[i] >= vv) {
      if (arr[cur].der == 'R')
        x += vv;
      else if (arr[cur].der == 'L')
        x -= vv;
      else if (arr[cur].der == 'U')
        y += vv;
      else if (arr[cur].der == 'D')
        y -= vv;
      cur = nn;
      t[i] -= vv;
    }
    if (cur != 0) {
      if (arr[cur].der == 'R')
        x += t[i];
      else if (arr[cur].der == 'L')
        x -= t[i];
      else if (arr[cur].der == 'U')
        y += t[i];
      else if (arr[cur].der == 'D')
        y -= t[i];
    }
    printf("%d %d\n", x, y);
  }
  return 0;
}
