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>
#pragma comment(linker, "/STACK:16000000")
using namespace std;
const int Maxn = 100005;
const int Maxm = 524288;
const int Maxp = 50;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
string let = "LDRU";
struct event {
  int typ;
  int a1, a2, b1, b2;
  int id;
  event(int typ = 0, int a1 = 0, int a2 = 0, int b1 = 0, int b2 = 0, int id = 0)
      : typ(typ), a1(a1), a2(a2), b1(b1), b2(b2), id(id) {}
  bool operator<(const event &e) const {
    if (a1 != e.a1) return a1 < e.a1;
    if (id != e.id) return id < e.id;
    if (b1 != e.b1) return b1 < e.b1;
    return b2 < e.b2;
  }
};
struct parent {
  int dr, lft, arr;
  parent(int dr = 0, int lft = 0, int arr = 0) : dr(dr), lft(lft), arr(arr) {}
};
int n, b;
int X0[Maxn], Y0[Maxn], X1[Maxn], Y1[Maxn];
int my[Maxn];
int q;
int X[Maxn], Y[Maxn];
char dr[Maxn];
long long tim[Maxn];
vector<event> E[4];
pair<int, int> st[Maxm];
pair<int, int> ares[Maxn], qres[Maxn];
pair<int, int> cango[Maxn][Maxp];
parent P[Maxn][Maxp];
void Clear(int v, int a, int b) {
  st[v] = pair<int, int>(-1, 0);
  if (a < b) {
    int m = a + b >> 1;
    Clear(2 * v, a, m);
    Clear(2 * v + 1, m + 1, b);
  }
}
void Insert(int v, int l, int r, int a, int b, pair<int, int> p) {
  if (a == l && b == r)
    st[v] = p;
  else {
    int m = l + r >> 1;
    if (a <= m) Insert(2 * v, l, m, a, min(m, b), p);
    if (m + 1 <= b) Insert(2 * v + 1, m + 1, r, max(m + 1, a), b, p);
  }
}
pair<int, int> Get(int v, int l, int r, int x) {
  pair<int, int> best = st[v];
  if (l < r) {
    pair<int, int> got;
    int m = l + r >> 1;
    if (x <= m)
      got = Get(2 * v, l, m, x);
    else
      got = Get(2 * v + 1, m + 1, r, x);
    best = pair<int, int>(best.second, best.first) >=
                   pair<int, int>(got.second, got.first)
               ? best
               : got;
  }
  return best;
}
void Solve(int side, vector<event> &E) {
  sort(E.begin(), E.end());
  Clear(1, 0, b);
  for (int i = 0; i < E.size(); i++)
    if (E[i].typ == 0) {
      if (my[E[i].id] == side) {
        ares[E[i].id] = Get(1, 0, b, E[i].b1);
        ares[E[i].id].second = E[i].a1 - ares[E[i].id].second;
      }
      Insert(1, 0, b, E[i].b1, E[i].b2, pair<int, int>(E[i].id, E[i].a2));
    } else {
      qres[E[i].id] = Get(1, 0, b, E[i].b1);
      qres[E[i].id].second = max(E[i].a1 - qres[E[i].id].second, 0);
    }
}
void getDist(int row, int col, int arrow, pair<int, int> c, long long need,
             int rec) {
  if (arrow == -1) {
    cango[row][col] = c;
    P[row][col] = parent(0, 0, -1);
    return;
  }
  long long togo = min(
      need, long long(abs(X1[arrow] - c.first) + abs(Y1[arrow] - c.second)));
  need -= togo;
  c = pair<int, int>(c.first + togo * dx[my[arrow]],
                     c.second + togo * dy[my[arrow]]);
  if (need == 0) {
    cango[row][col] = c;
    P[row][col] = parent(my[arrow], 0, arrow);
  } else {
    while (1ll << rec > need) rec--;
    c = cango[arrow][rec];
    parent tmp = P[arrow][rec];
    need -= 1ll << rec;
    togo = min(need, long long(tmp.lft));
    need -= togo;
    c = pair<int, int>(c.first + togo * dx[tmp.dr],
                       c.second + togo * dy[tmp.dr]);
    if (need == 0) {
      cango[row][col] = c;
      P[row][col] = parent(tmp.dr, tmp.lft - togo, tmp.arr);
    } else
      getDist(row, col, tmp.arr, c, need, rec);
  }
}
pair<int, int> calcDist(int arrow, pair<int, int> c, long long need, int rec) {
  if (arrow == -1) return c;
  long long togo = min(
      need, long long(abs(X1[arrow] - c.first) + abs(Y1[arrow] - c.second)));
  need -= togo;
  c = pair<int, int>(c.first + togo * dx[my[arrow]],
                     c.second + togo * dy[my[arrow]]);
  if (need) {
    while (1ll << rec > need) rec--;
    c = cango[arrow][rec];
    parent tmp = P[arrow][rec];
    need -= 1ll << rec;
    togo = min(need, long long(tmp.lft));
    need -= togo;
    c = pair<int, int>(c.first + togo * dx[tmp.dr],
                       c.second + togo * dy[tmp.dr]);
    if (need) c = calcDist(tmp.arr, c, need, rec);
  }
  return c;
}
int main() {
  scanf("%d %d", &n, &b);
  for (int i = 0; i < n; i++) {
    scanf("%d %d %d %d", &X0[i], &Y0[i], &X1[i], &Y1[i]);
    if (X0[i] == Y0[i] && X1[i] == Y1[i]) {
      n--;
      i--;
      continue;
    }
    if (X0[i] > X1[i])
      my[i] = 0;
    else if (Y0[i] > Y1[i])
      my[i] = 1;
    else if (X0[i] < X1[i])
      my[i] = 2;
    else
      my[i] = 3;
    E[0].push_back(event(0, min(X0[i], X1[i]), max(X0[i], X1[i]),
                         min(Y0[i], Y1[i]), max(Y0[i], Y1[i]), i));
    E[1].push_back(event(0, min(Y0[i], Y1[i]), max(Y0[i], Y1[i]),
                         min(X0[i], X1[i]), max(X0[i], X1[i]), i));
    E[2].push_back(event(0, b - max(X0[i], X1[i]), b - min(X0[i], X1[i]),
                         min(Y0[i], Y1[i]), max(Y0[i], Y1[i]), i));
    E[3].push_back(event(0, b - max(Y0[i], Y1[i]), b - min(Y0[i], Y1[i]),
                         min(X0[i], X1[i]), max(X0[i], X1[i]), i));
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d %c %I64d", &X[i], &Y[i], &dr[i], &tim[i]);
    if (dr[i] == 'L')
      E[0].push_back(event(1, X[i], X1[i], Y[i], Y[i], i));
    else if (dr[i] == 'D')
      E[1].push_back(event(1, Y[i], Y[i], X[i], X[i], i));
    else if (dr[i] == 'R')
      E[2].push_back(event(1, b - X[i], b - X[i], Y[i], Y[i], i));
    else if (dr[i] == 'U')
      E[3].push_back(event(1, b - Y[i], b - Y[i], X[i], X[i], i));
  }
  for (int i = 0; i < 4; i++) Solve(i, E[i]);
  for (int j = 0; j < Maxp; j++)
    for (int i = 0; i < n; i++) {
      long long need = 1ll << j;
      long long togo = min(need, long long(ares[i].second));
      need -= togo;
      pair<int, int> c =
          pair<int, int>(X1[i] + togo * dx[my[i]], Y1[i] + togo * dy[my[i]]);
      if (need == 0) {
        cango[i][j] = c;
        P[i][j] = parent(my[i], ares[i].second - togo, ares[i].first);
      } else
        getDist(i, j, ares[i].first, c, need, j);
    }
  for (int i = 0; i < q; i++) {
    long long need = tim[i];
    long long togo = min(need, long long(qres[i].second));
    need -= togo;
    int my = let.find(dr[i]);
    pair<int, int> c =
        pair<int, int>(X[i] + togo * dx[my], Y[i] + togo * dy[my]);
    if (need) c = calcDist(qres[i].first, c, need, Maxp - 1);
    printf("%d %d\n", c.first, c.second);
  }
  return 0;
}
