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;
const int N = 200005;
struct arrow {
  int x0, y0, x1, y1;
  int dx, dy, id;
  bool operator<(const arrow &R) const { return id < R.id; }
};
bool cmpX0(const arrow &A, const arrow &B) {
  return make_pair(max(A.x0, A.x1), -A.id) < make_pair(max(B.x0, B.x1), -B.id);
}
bool cmpX1(const arrow &A, const arrow &B) {
  return make_pair(-min(A.x0, A.x1), -A.id) <
         make_pair(-min(B.x0, B.x1), -B.id);
}
bool cmpY0(const arrow &A, const arrow &B) {
  return make_pair(max(A.y0, A.y1), -A.id) < make_pair(max(B.y0, B.y1), -B.id);
}
bool cmpY1(const arrow &A, const arrow &B) {
  return make_pair(-min(A.y0, A.y1), -A.id) <
         make_pair(-min(B.y0, B.y1), -B.id);
}
long long T[N];
arrow a[N];
int n, m, q;
int nxt[51][N];
long long t[51][N];
const long long inf = 1e16;
void work() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d%d%d", &a[i].x0, &a[i].y0, &a[i].x1, &a[i].y1);
    a[i].dx = a[i].x1 - a[i].x0;
    a[i].dy = a[i].y1 - a[i].y0;
    int len = abs(a[i].dx) + abs(a[i].dy);
    a[i].dx /= len, a[i].dy /= len;
    a[i].id = i;
  }
  scanf("%d", &q);
  char dir[10];
  for (int i = n + 1; i <= n + q; ++i) {
    scanf("%d%d", &a[i].x0, &a[i].y0);
    a[i].x1 = a[i].x0, a[i].y1 = a[i].y0;
    a[i].id = i;
    scanf("%s%lld", dir, &T[i]);
    if (*dir == 'U') a[i].dy = 1;
    if (*dir == 'D') a[i].dy = -1;
    if (*dir == 'R') a[i].dx = 1;
    if (*dir == 'L') a[i].dx = -1;
  }
  multimap<int, arrow *> Hash;
  sort(a + 1, a + n + q + 1, cmpX0);
  Hash.clear();
  for (int i = 1; i <= n + q; ++i) {
    if (a[i].id <= n) {
      int L = min(a[i].y0, a[i].y1), R = max(a[i].y0, a[i].y1);
      auto it = Hash.lower_bound(L);
      auto itr = Hash.upper_bound(R);
      while (it != itr) {
        arrow *A = it->second;
        nxt[0][A->id] = a[i].id;
        t[0][A->id] = abs(a[i].x1 - A->x1) + abs(a[i].y1 - A->y1);
        Hash.erase(it++);
      }
    }
    if (a[i].dx == 1 && a[i].dy == 0) Hash.insert(make_pair(a[i].y0, a + i));
  }
  for (auto &it : Hash) {
    arrow *A = it.second;
    nxt[0][A->id] = 0;
    t[0][A->id] = m - A->x1;
  }
  sort(a + 1, a + n + q + 1, cmpX1);
  Hash.clear();
  for (int i = 1; i <= n + q; ++i) {
    if (a[i].id <= n) {
      int L = min(a[i].y0, a[i].y1), R = max(a[i].y0, a[i].y1);
      auto it = Hash.lower_bound(L);
      auto itr = Hash.upper_bound(R);
      while (it != itr) {
        arrow *A = it->second;
        nxt[0][A->id] = a[i].id;
        t[0][A->id] = abs(a[i].x1 - A->x1) + abs(a[i].y1 - A->y1);
        Hash.erase(it++);
      }
    }
    if (a[i].dx == -1 && a[i].dy == 0) Hash.insert(make_pair(a[i].y0, a + i));
  }
  for (auto &it : Hash) {
    arrow *A = it.second;
    nxt[0][A->id] = 0;
    t[0][A->id] = A->x1;
  }
  sort(a + 1, a + n + q + 1, cmpY0);
  Hash.clear();
  for (int i = 1; i <= n + q; ++i) {
    if (a[i].id <= n) {
      int L = min(a[i].x0, a[i].x1), R = max(a[i].x0, a[i].x1);
      auto it = Hash.lower_bound(L);
      auto itr = Hash.upper_bound(R);
      while (it != itr) {
        arrow *A = it->second;
        nxt[0][A->id] = a[i].id;
        t[0][A->id] = abs(a[i].x1 - A->x1) + abs(a[i].y1 - A->y1);
        Hash.erase(it++);
      }
    }
    if (a[i].dx == 0 && a[i].dy == 1) Hash.insert(make_pair(a[i].x0, a + i));
  }
  for (auto &it : Hash) {
    arrow *A = it.second;
    nxt[0][A->id] = 0;
    t[0][A->id] = m - A->y1;
  }
  sort(a + 1, a + n + q + 1, cmpY1);
  Hash.clear();
  for (int i = 1; i <= n + q; ++i) {
    if (a[i].id <= n) {
      int L = min(a[i].x0, a[i].x1), R = max(a[i].x0, a[i].x1);
      auto it = Hash.lower_bound(L);
      auto itr = Hash.upper_bound(R);
      while (it != itr) {
        arrow *A = it->second;
        nxt[0][A->id] = a[i].id;
        t[0][A->id] = abs(a[i].x1 - A->x1) + abs(a[i].y1 - A->y1);
        Hash.erase(it++);
      }
    }
    if (a[i].dx == 0 && a[i].dy == -1) Hash.insert(make_pair(a[i].x0, a + i));
  }
  for (auto &it : Hash) {
    arrow *A = it.second;
    nxt[0][A->id] = 0;
    t[0][A->id] = A->y1;
  }
  for (int j = 1; j <= 50; ++j)
    for (int i = 0; i <= n + q; ++i) {
      nxt[j][i] = nxt[j - 1][nxt[j - 1][i]];
      t[j][i] = min(inf, t[j - 1][i] + t[j - 1][nxt[j - 1][i]]);
    }
  sort(a + 1, a + n + q + 1);
  for (int i = n + 1; i <= n + q; ++i) {
    int x = i;
    long long rt = T[i];
    for (int j = 50; j >= 0; --j) {
      if (nxt[j][x] && t[j][x] <= rt) {
        rt -= t[j][x];
        x = nxt[j][x];
      }
    }
    int y = nxt[0][x];
    if (y && rt > (a[y].x1 - a[x].x1) * a[x].dx + (a[y].y1 - a[x].y1) * a[x].dy)
      rt -= t[0][x], x = y;
    long long X = min((long long)m, max(0LL, a[x].x1 + rt * a[x].dx));
    long long Y = min((long long)m, max(0LL, a[x].y1 + rt * a[x].dy));
    printf("%d %d\n", (int)X, (int)Y);
  }
}
int main() {
  work();
  return 0;
}
