3 8
2 4 4 4
7 2 5 2
6 5 6 3
20
3 2 U 1000000
3 2 U 1
3 2 U 2
3 2 U 3
3 2 U 4
3 2 U 5
3 2 U 6
3 2 U 7
3 2 U 8
3 2 U 9
3 2 U 10
3 2 U 11
3 2 U 12
3 2 U 13
3 2 U 14
3 2 U 15
3 2 U 16
3 2 U 17
3 2 U 18
3 2 U 19
 4 2
0 0 1 0
2 0 2 1
2 2 1 2
0 2 0 1
15
1 1 R 7
0 0 U 3
1 1 R 888888888888887
0 0 U 404040404040403
2 1 D 1000000000000000
2 0 R 324354768576473
2 0 L 325436475453245
2 0 U 764536452343654
2 0 D 943654765876545
1 1 U 1000000000000000
1 1 D 989898789898789
1 1 L 123456787654344
0 1 L 0
0 1 L 1
0 1 L 999999999999993
 1 10
1 5 9 5
1
5 5 L 1
 3 7
2 4 2 7
4 7 4 6
7 3 5 3
10
6 7 L 1
6 7 L 2
6 7 L 3
6 7 L 4
6 7 L 5
6 7 L 6
6 7 L 7
6 7 L 8
6 7 L 9
6 7 L 10

                                                               0                                                                2
                                                               3                                                                3
                                                               3                                                                4
                                                               4                                                                4
                                                               5                                                                4
                                                               6                                                                4
                                                               6                                                                3
                                                               6                                                                2
                                                               5                                                                2
                                                               4                                                                2
                                                               3                                                                2
                                                               2                                                                2
                                                               1                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                                1                                                                0
                                                               2                                                                1
                                                               1                                                                0
                                                               2                                                                1
                                                               2                                                                1
                                                               2                                                                1
                                                               0                                                                1
                                                               0                                                                0
                                                               2                                                                1
                                                               2                                                                2
                                                               1                                                                2
                                                               0                                                                2
                                                               0                                                                1
                                                               0                                                                0
                                                               0                                                                0
                                                                6                                                                5
                                                                5                                                                7
                                                               4                                                                7
                                                               4                                                                6
                                                               4                                                                5
                                                               4                                                                4
                                                               4                                                                3
                                                               4                                                                2
                                                               4                                                                1
                                                               4                                                                0
                                                               4                                                                0

#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool get_int(T &x) {
  char t = getchar();
  bool neg = false;
  x = 0;
  for (; (t > '9' || t < '0') && t != '-' && t != EOF; t = getchar())
    ;
  if (t == '-') neg = true, t = getchar();
  if (t == EOF) return false;
  for (; t <= '9' && t >= '0'; t = getchar()) x = x * 10 + t - '0';
  if (neg) x = -x;
  return true;
}
template <typename T>
void print_int(T x) {
  if (x < 0) putchar('-'), x = -x;
  short a[20] = {}, sz = 0;
  while (x > 0) a[sz++] = x % 10, x /= 10;
  if (sz == 0) putchar('0');
  for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
const int inf = 0x3f3f3f3f;
const long long Linf = 1ll << 61;
const double pi = acos(-1.0);
struct arrow {
  int type;
  int x, y, yy, i;
  void in(int i) {
    this->i = i;
    int x, y, xx, yy;
    (get_int(x) && get_int(y));
    (get_int(xx) && get_int(yy));
    if (x == xx) {
      this->x = x;
      this->y = y;
      this->yy = yy;
      type = 0;
    } else {
      this->x = y;
      this->y = x;
      this->yy = xx;
      type = 1;
    }
  }
};
arrow a[100111];
int n, q, qnxt[100111];
int b, x[100111], y[100111], dir[100111];
long long t[100111];
int nxt[50][100111];
long long len[50][100111];
double tlen[50][100111];
void work(int tp) {
  vector<pair<int, pair<int, int> > > v;
  set<pair<int, int> > st;
  for (int i = 1; i <= n; i++) {
    if (a[i].type == tp)
      v.push_back(make_pair(a[i].x, make_pair(1, i)));
    else if (a[i].y <= a[i].yy) {
      v.push_back(make_pair(a[i].y, make_pair(0, i)));
      v.push_back(make_pair(a[i].yy, make_pair(3, i)));
    } else {
      v.push_back(make_pair(a[i].yy, make_pair(0, i)));
      v.push_back(make_pair(a[i].y, make_pair(3, i)));
    }
  }
  for (int i = 1; i <= q; i++) {
    if (tp == 0 && dir[i] <= 1)
      v.push_back(make_pair(x[i], make_pair(2, i)));
    else if (tp == 1 && dir[i] >= 2)
      v.push_back(make_pair(y[i], make_pair(2, i)));
  }
  sort(v.begin(), v.end());
  for (int id = 0, nt = 0; id < (int)v.size(); id = nt) {
    for (; nt < (int)v.size() && v[id].first == v[nt].first; nt++) {
      if (v[nt].second.first == 1) {
        int i = v[nt].second.second;
        st.insert(make_pair(a[i].y, i));
        st.insert(make_pair(a[i].yy, i));
      }
    }
    for (int j = id; j < nt; j++) {
      pair<int, pair<int, int> > &now = v[j];
      int i = now.second.second;
      if (now.second.first == 0)
        st.insert(make_pair(a[i].x, i));
      else if (now.second.first == 3)
        st.erase(make_pair(a[i].x, i));
      else if (now.second.first == 1) {
        st.erase(make_pair(a[i].y, i));
        st.erase(make_pair(a[i].yy, i));
        auto p = st.lower_bound(make_pair(a[i].y, 0));
        if (a[i].y <= a[i].yy && p != st.end())
          nxt[0][i] = p->second;
        else if (a[i].y > a[i].yy && p != st.begin()) {
          --p;
          nxt[0][i] = p->second;
        }
        st.insert(make_pair(a[i].y, i));
        st.insert(make_pair(a[i].yy, i));
      } else {
        int pos;
        if (tp == 0)
          pos = y[i];
        else
          pos = x[i];
        if (dir[i] % 2 == 0) {
          auto p = st.lower_bound(make_pair(pos, 0));
          if (p != st.end()) qnxt[i] = p->second;
        } else if (dir[i] % 2) {
          auto p = st.lower_bound(make_pair(pos, inf));
          if (p != st.begin()) {
            --p;
            qnxt[i] = p->second;
          }
        }
      }
    }
    for (int j = id; j < nt; j++) {
      if (v[j].second.first == 1) {
        int i = v[j].second.second;
        st.erase(make_pair(a[i].y, i));
        st.erase(make_pair(a[i].yy, i));
      }
    }
  }
}
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
void find_pos(int x, int y, int d, int i, long long len) {
  int ansx, ansy;
  if (i == 0) {
    ansx = max(min(b + 0ll, x + len * dx[d]), 0ll);
    ansy = max(min(b + 0ll, y + len * dy[d]), 0ll);
  } else if (a[i].type == (d >> 1)) {
    if (a[i].type == 0) {
      ansx = a[i].x;
      if (a[i].yy > y)
        ansy = y + len;
      else
        ansy = y - len;
    } else {
      if (a[i].yy > x)
        ansx = x + len;
      else
        ansx = x - len;
      ansy = a[i].x;
    }
  } else {
    int l1;
    if (a[i].type == 0)
      l1 = abs(x - a[i].x);
    else
      l1 = abs(y - a[i].x);
    if (len <= l1) {
      if (a[i].type == 0) {
        if (x < a[i].x)
          ansx = x + len;
        else
          ansx = x - len;
        ansy = y;
      } else {
        ansx = x;
        if (y < a[i].x)
          ansy = y + len;
        else
          ansy = y - len;
      }
    } else {
      len -= l1;
      if (a[i].type == 0) {
        ansx = a[i].x;
        if (y < a[i].yy)
          ansy = y + len;
        else
          ansy = y - len;
      } else {
        if (x < a[i].yy)
          ansx = x + len;
        else
          ansx = x - len;
        ansy = a[i].x;
      }
    }
  }
  printf("%d %d\n", ansx, ansy);
}
int main() {
  (get_int(n) && get_int(b));
  for (int i = 1; i <= n; i++) a[i].in(i);
  get_int(q);
  char op[3];
  for (int i = 1; i <= q; i++) {
    (get_int(x[i]) && get_int(y[i]));
    scanf("%s", op);
    get_int(t[i]);
    if (op[0] == 'U')
      dir[i] = 0;
    else if (op[0] == 'D')
      dir[i] = 1;
    else if (op[0] == 'R')
      dir[i] = 2;
    else
      dir[i] = 3;
  }
  work(0);
  work(1);
  for (int i = 1; i <= n; i++) {
    int &j = nxt[0][i];
    if (!j) continue;
    if (a[i].type == a[j].type)
      len[0][i] = abs(a[i].yy - a[j].yy);
    else
      len[0][i] = abs(a[i].yy - a[j].x) + abs(a[j].yy - a[i].x);
    tlen[0][i] = len[0][i];
  }
  for (int i = 1; i < 50; i++)
    for (int j = 1; j <= n; j++) {
      nxt[i][j] = nxt[i - 1][nxt[i - 1][j]];
      len[i][j] = len[i - 1][j] + len[i - 1][nxt[i - 1][j]];
      tlen[i][j] = tlen[i - 1][j] + tlen[i - 1][nxt[i - 1][j]];
    }
  for (int i = 1; i <= q; i++) {
    int l1, j = qnxt[i];
    if (a[j].type == 0)
      l1 = abs(a[j].x - x[i]) + abs(a[j].yy - y[i]);
    else
      l1 = abs(a[j].x - y[i]) + abs(a[j].yy - x[i]);
    if (!j || t[i] <= l1)
      find_pos(x[i], y[i], dir[i], j, t[i]);
    else {
      t[i] -= l1;
      for (int d = 49; d >= 0; d--)
        if (nxt[d][j] && len[d][j] <= t[i] && tlen[d][j] <= 1e17) {
          t[i] -= len[d][j];
          j = nxt[d][j];
        }
      int tdir = a[j].type * 2 + (a[j].yy < a[j].y);
      if (a[j].type == 0)
        find_pos(a[j].x, a[j].yy, tdir, nxt[0][j], t[i]);
      else
        find_pos(a[j].yy, a[j].x, tdir, nxt[0][j], t[i]);
    }
  }
  return 0;
}
