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:1024000000,1024000000")
using namespace std;
const int Maxn = 200005, Maxm = 300005, Mo = 10007,
          sp[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
const long long oo = LLONG_MAX >> 2;
typedef int IArr[Maxn];
typedef long long LArr[Maxn];
typedef double DArr[Maxn];
struct Tree {
  pair<long long, int> v, mk;
};
struct point {
  int x, y, dir, id, mk = 0;
  point(int n_x, int n_y, int n_dir, int n_id) {
    x = n_x, y = n_y, dir = n_dir, id = n_id;
  }
  point(){};
};
struct node {
  int v, h, id;
  node(int n_v, int n_h, int n_id) { v = n_v, h = n_h, id = n_id; }
};
struct arrow {
  int l, r, h, id, dir;
  arrow(int n_l, int n_r, int n_h, int n_id, int n_dir) {
    l = n_l;
    r = n_r, h = n_h, id = n_id;
    dir = n_dir;
  }
  arrow() {}
};
struct query {
  int x, y, dir, id;
  long long t;
  query(int n_x, int n_y, long long n_t, int n_dir, int n_id) {
    x = n_x, y = n_y;
    t = n_t;
    dir = n_dir, id = n_id;
  }
  query() {}
};
int n, m, q, i, j;
pair<long long, int> to[Maxn];
Tree t[Maxn * 4];
query Q[Maxn];
point p_all[Maxn];
arrow s_all[Maxn];
vector<arrow> s_x, s_y;
vector<point> p_x, p_y;
vector<node> pt[4];
LArr dis[60];
IArr nt[60];
bool cmp_x(point a, point b) {
  if (a.y != b.y) return a.y < b.y;
  return a.x < b.x;
}
bool cmp_y(point a, point b) {
  if (a.x != b.x) return a.x <= b.x;
  return a.y <= b.y;
}
bool cmp_s(arrow a, arrow b) { return a.h < b.h; }
bool cmp_n(node a, node b) { return a.h < b.h; }
void input() {
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    int x0, y0, x1, y1;
    cin >> x0 >> y0 >> x1 >> y1;
    if (y0 == y1) {
      int dir = x0 < x1 ? 1 : -1;
      int l = min(x0, x1);
      int r = max(x0, x1);
      s_x.push_back((arrow(l, r, y0, i, dir - 1 ? 0 : 1)));
      p_x.push_back((point(x1, y1, dir, i)));
      if (dir + 1)
        pt[1].push_back((node(y1, x1, i)));
      else
        pt[0].push_back((node(y1, x1, i)));
      p_all[i] = point(x1, y1, dir - 1 ? 0 : 1, i);
      s_all[i] = arrow(l, r, y0, i, dir - 1 ? 0 : 1);
    } else {
      int l = min(y0, y1);
      int r = max(y0, y1);
      int dir = y0 < y1 ? 1 : -1;
      s_y.push_back((arrow(l, r, x0, i, dir - 1 ? 3 : 2)));
      p_y.push_back((point(x1, y1, dir, i)));
      if (dir + 1)
        pt[2].push_back((node(x1, y1, i)));
      else
        pt[3].push_back((node(x1, y1, i)));
      p_all[i] = point(x1, y1, dir - 1 ? 3 : 2, i);
      s_all[i] = arrow(l, r, x0, i, dir - 1 ? 3 : 2);
    }
  }
  cin >> q;
  for (i = 1; i <= q; i++) {
    int x, y, dir;
    long long t;
    char ch;
    cin >> x >> y >> ch >> t;
    if (ch == 'L') dir = 0;
    if (ch == 'R') dir = 1;
    if (ch == 'U') dir = 2;
    if (ch == 'D') dir = 3;
    if (dir <= 1)
      pt[dir].push_back((node(y, x, n + i)));
    else
      pt[dir].push_back((node(x, y, n + i)));
    Q[i] = query(x, y, t, dir, i);
    p_all[n + i] = point(x, y, dir, i + n);
  }
  sort(s_x.begin(), s_x.end(), cmp_s);
  sort(s_y.begin(), s_y.end(), cmp_s);
  sort(p_x.begin(), p_x.end(), cmp_x);
  sort(p_y.begin(), p_y.end(), cmp_y);
  for (i = 0; i < 4; i++) sort(pt[i].begin(), pt[i].end(), cmp_n);
}
bool ck(long long a, long long b, int d) {
  if (d + 1)
    return a >= b;
  else
    return a <= b;
}
void check(int k, int l, int r) {
  if (!t[k].mk.second) return;
  t[k].v = t[k].mk;
  if (l != r) t[k * 2].mk = t[k].mk, t[k * 2 + 1].mk = t[k].mk;
  t[k].mk = make_pair((0), (0));
}
void ins(int k, int l, int r, int x, int y, pair<long long, int> key, int d) {
  check(k, l, r);
  if (y < l || r < x) return;
  if (x <= l && r <= y) {
    t[k].mk = key;
    check(k, l, r);
    return;
  }
  int mid = (l + r) / 2;
  ins(k * 2, l, mid, x, y, key, d);
  ins(k * 2 + 1, mid + 1, r, x, y, key, d);
}
long long dist(point a, point b) { return abs(a.x - b.x) + abs(a.y - b.y); }
pair<long long, int> ask(int k, int l, int r, int pos, int id1) {
  check(k, l, r);
  if (l == r) {
    int id2 = t[k].v.second;
    return make_pair((dist(p_all[id1], p_all[id2])), (t[k].v.second));
  }
  int mid = (l + r) / 2;
  if (pos <= mid)
    return ask(k * 2, l, mid, pos, id1);
  else
    return ask(k * 2 + 1, mid + 1, r, pos, id1);
}
bool cover(int k, vector<point>& a, int v, int h) {
  if (k < 0 || k >= a.size()) return 0;
  int id = a[k].id;
  if (s_all[id].h == h)
    if (s_all[id].l <= v && v <= s_all[id].r) return 1;
  return 0;
}
pair<long long, int> find_same_axe(point& A) {
  point Q;
  Q.x = A.x;
  Q.y = A.y;
  if (A.dir <= 1) {
    int pos = lower_bound(p_x.begin(), p_x.end(), Q, cmp_x) - p_x.begin();
    if (cover(pos, p_x, Q.x, Q.y))
      return A.dir = s_all[p_x[pos].id].dir, A.mk = 1,
             make_pair((abs(Q.x - p_x[pos].x)), (p_x[pos].id));
    if (cover(pos - 1, p_x, Q.x, Q.y))
      return A.dir = s_all[p_x[pos - 1].id].dir, A.mk = 1,
             make_pair((abs(Q.x - p_x[pos - 1].x)), (p_x[pos - 1].id));
    if (A.dir == 0) pos--;
    if (pos < 0 || pos >= p_x.size() || p_x[pos].y != A.y)
      return make_pair((oo), (0));
    else
      return make_pair((abs(Q.x - p_x[pos].x)), (p_x[pos].id));
  } else {
    int pos = lower_bound(p_y.begin(), p_y.end(), Q, cmp_y) - p_y.begin();
    if (cover(pos, p_y, Q.y, Q.x))
      return A.dir = s_all[p_y[pos].id].dir, A.mk = 1,
             make_pair((abs(Q.y - p_y[pos].y)), (p_y[pos].id));
    if (cover(pos - 1, p_y, Q.y, Q.x))
      return A.dir = s_all[p_y[pos - 1].id].dir, A.mk = 1,
             make_pair((abs(Q.y - p_y[pos - 1].y)), (p_y[pos - 1].id));
    if (A.dir == 3) pos--;
    if (pos < 0 || pos >= p_y.size() || p_y[pos].x != A.x)
      return make_pair((oo), (0));
    else
      return make_pair((abs(Q.y - p_y[pos].y)), (p_y[pos].id));
  }
}
void work(vector<node>& p, vector<arrow>& s, int i, int j, int d,
          pair<long long, int>* ct) {
  memset((t), 0, sizeof((t)));
  int m1 = p.size(), m2 = s.size();
  for (; 0 <= i && i < m1; i += d)
    if (!p_all[p[i].id].mk) {
      for (; 0 <= j && j < m2 && ck(p[i].h, s[j].h, d); j += d) {
        ins(1, 0, m, s[j].l, s[j].r, make_pair((s[j].h), (s[j].id)), d);
      }
      pair<long long, int> ans = ask(1, 0, m, p[i].v, p[i].id);
      if (!ans.second) continue;
      int nd = abs(s_all[ans.second].h - p[i].h);
      if (!ct[p[i].id].second || nd < ct[p[i].id].first) ct[p[i].id] = ans;
      if (!nd) {
        ct[p[i].id] = ans;
        p_all[p[i].id].dir = s_all[ans.second].dir;
      }
    }
}
void construct_graph() {
  for (i = 0; i < p_x.size(); i++) {
    int p = i + p_x[i].dir;
    int id = p_x[i].id;
    if (p < 0 || p >= p_x.size())
      to[id] = make_pair((0), (0));
    else if (p_x[p].y == p_x[i].y)
      to[id] = make_pair((abs(p_x[i].x - p_x[p].x)), (p_x[p].id));
    else
      to[id] = make_pair((0), (0));
  }
  for (i = 0; i < p_y.size(); i++) {
    int p = i + p_y[i].dir;
    int id = p_y[i].id;
    if (p < 0 || p >= p_y.size())
      to[id] = make_pair((0), (0));
    else if (p_y[p].x == p_y[i].x)
      to[id] = make_pair((abs(p_y[i].y - p_y[p].y)), (p_y[p].id));
    else
      to[id] = make_pair((0), (0));
  }
  for (i = n + 1; i <= n + q; i++) to[i] = find_same_axe(p_all[i]);
  work(pt[0], s_y, 0, 0, 1, to);
  work(pt[1], s_y, pt[1].size() - 1, s_y.size() - 1, -1, to);
  work(pt[2], s_x, pt[2].size() - 1, s_x.size() - 1, -1, to);
  work(pt[3], s_x, 0, 0, 1, to);
}
void pre_query() {
  for (i = 1; i <= n + q; i++) dis[0][i] = to[i].first, nt[0][i] = to[i].second;
  for (i = 1; i <= 50; i++) {
    for (j = 1; j <= n + q; j++) {
      if (!nt[i - 1][j])
        nt[i][j] = nt[i - 1][j], dis[i][j] = dis[i - 1][j];
      else
        nt[i][j] = nt[i - 1][nt[i - 1][j]],
        dis[i][j] = dis[i - 1][j] + dis[i - 1][nt[i - 1][j]];
      dis[i][j] = min(dis[i][j], oo);
    }
  }
}
pair<long long, long long> calc(query cur) {
  int pos = cur.id + n;
  long long T = cur.t;
  for (int i = 50; i + 1; i--)
    if (T >= dis[i][pos]) {
      if (!nt[i][pos]) continue;
      T -= dis[i][pos];
      pos = nt[i][pos];
    }
  int dir = p_all[pos].dir;
  pair<long long, long long> ans = make_pair((p_all[pos].x), (p_all[pos].y));
  if (!to[pos].second) {
    ans.first += sp[dir][0] * T;
    ans.second += sp[dir][1] * T;
    return ans;
  }
  int go = to[pos].second;
  if (dir <= 1) {
    int len = abs(p_all[pos].x - p_all[go].x);
    if (len >= T) {
      ans.first += sp[dir][0] * T;
      return ans;
    }
    ans.first += sp[dir][0] * len;
    T -= len;
    ans.second += sp[p_all[go].dir][1] * T;
  } else {
    int len = abs(p_all[pos].y - p_all[go].y);
    if (len >= T) {
      ans.second += sp[dir][1] * T;
      return ans;
    }
    ans.second += sp[dir][1] * len;
    T -= len;
    ans.first += sp[p_all[go].dir][0] * T;
  }
  return ans;
}
void do_query() {
  pre_query();
  for (i = 1; i <= q; i++) {
    pair<long long, long long> ans = calc(Q[i]);
    if (ans.first > m) ans.first = m;
    if (ans.first < 0) ans.first = 0;
    if (ans.second > m) ans.second = m;
    if (ans.second < 0) ans.second = 0;
    cout << ans.first << ' ' << ans.second << endl;
  }
}
int main() {
  ios::sync_with_stdio(0);
  input();
  construct_graph();
  do_query();
}
