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 GCC optimize(2)
using namespace std;
const int inf = 0x3f3f3f3f;
const long long linf = 1LL << 60;
const long double eps = 1e-9;
int read() {
  int x = 0, f = 1, c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f ^= 1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - 48;
  return f ? x : -x;
}
void write(int x) {
  if (!x) {
    putchar('0');
    return;
  }
  if (x < 0) putchar('-'), x = -x;
  int stk[10], tp = 0;
  for (; x; x /= 10) stk[tp++] = x - x / 10 * 10;
  for (; tp; putchar(48 + stk[--tp]))
    ;
}
const int N = 1 << 17;
const char *Dir = "UDLR";
const int dx[] = {0, 0, -1, 1};
const int dy[] = {1, -1, 0, 0};
struct SegT {
  pair<int, int> mn[N << 1];
  void init() {
    for (int i = 0; i < (N << 1); ++i) mn[i] = make_pair(inf, 0);
  }
  void chkmin(pair<int, int> &x, pair<int, int> y) {
    if (y < x) x = y;
  }
  void upd(int l, int r, pair<int, int> v) {
    for (l += N, r += N; l < r; l >>= 1, r >>= 1) {
      if (l & 1) chkmin(mn[l++], v);
      if (r & 1) chkmin(mn[--r], v);
    }
  }
  pair<int, int> qry(int x) {
    pair<int, int> ans = make_pair(inf, 0);
    for (x += N; x; x >>= 1) {
      chkmin(ans, mn[x]);
    }
    return ans;
  }
} segt;
int n, b, q;
vector<pair<pair<int, int>, pair<int, int> > > arrow;
vector<pair<pair<int, int>, pair<int, long long> > > qry;
struct Arrow {
  int x1, y1;
  int x2, y2;
  int dir, id;
  int getLowCol() { return min(y1, y2); }
  int getHighCol() { return max(y1, y2); }
  int getLowRow() { return min(x1, x2); }
  int getHighRow() { return max(x1, x2); }
} a[N + N];
bool cmp1(Arrow a, Arrow b) {
  return a.getHighCol() > b.getHighCol() ||
         (a.getHighCol() == b.getHighCol() && a.id < b.id);
}
bool cmp2(Arrow a, Arrow b) {
  return a.getLowCol() < b.getLowCol() ||
         (a.getLowCol() == b.getLowCol() && a.id < b.id);
}
bool cmp3(Arrow a, Arrow b) {
  return a.getHighRow() > b.getHighRow() ||
         (a.getHighRow() == b.getHighRow() && a.id < b.id);
}
bool cmp4(Arrow a, Arrow b) {
  return a.getLowRow() < b.getLowRow() ||
         (a.getLowRow() == b.getLowRow() && a.id < b.id);
}
map<pair<int, int>, pair<pair<int, int>, long long> > go;
int dir[N];
map<pair<int, int>, int> belong;
vector<pair<int, int> > vec;
vector<pair<int, long long> > Go[50];
vector<int> has[N];
pair<pair<int, int>, long long> fstGo[N];
void print(int a, int b) {
  write(a);
  putchar(' ');
  write(b);
  putchar('\n');
}
void mov(int x, int y, int tp, long long t) {
  if (tp < 2) {
    if (tp == 0) {
      if (y + t >= b)
        print(x, b);
      else
        print(x, y + t);
    } else {
      if (y - t <= 0)
        print(x, 0);
      else
        print(x, y - t);
    }
  } else {
    if (tp == 2) {
      if (x - t <= 0)
        print(0, y);
      else
        print(x - t, y);
    } else {
      if (x + t >= b)
        print(b, y);
      else
        print(x + t, y);
    }
  }
}
int main() {
  n = read(), b = read();
  for (int i = 0; i < n; ++i) {
    int x1 = read(), y1 = read();
    int x2 = read(), y2 = read();
    arrow.push_back(make_pair(make_pair(x1, y1), make_pair(x2, y2)));
  }
  q = read();
  for (int i = 0; i < q; ++i) {
    int x = read(), y = read();
    char c = getchar();
    int dir = strchr(Dir, c) - Dir;
    long long t;
    scanf("%lld", &t);
    qry.push_back(make_pair(make_pair(x, y), make_pair(dir, t)));
  }
  for (int i = 0; i < n; ++i) {
    int x1, x2, y1, y2, tp;
    x1 = arrow[i].first.first, y1 = arrow[i].first.second;
    x2 = arrow[i].second.first, y2 = arrow[i].second.second;
    tp = (x1 == x2 ? y1 < y2 ? 0 : 1 : x1 < x2 ? 3 : 2);
    dir[i] = tp;
    a[i] = (Arrow){x1, y1, x2, y2, tp, i};
  }
  for (int i = 0; i < q; ++i) {
    int x, y, dir;
    x = qry[i].first.first, y = qry[i].first.second;
    dir = qry[i].second.first;
    a[i + n] = (Arrow){x, y, x, y, dir, i + n};
    fstGo[i] = make_pair(make_pair(inf, inf), linf);
  }
  sort(a, a + n + q, cmp1);
  segt.init();
  for (int i = 0; i < n + q; ++i) {
    if (a[i].dir == 0) {
      pair<int, int> p = segt.qry(a[i].x2);
      if (p.first != inf) {
        pair<pair<int, int>, long long> np =
            make_pair(make_pair(a[i].x2, max(a[i].y2, p.first)),
                      max(0, p.first - a[i].y2));
        if (a[i].id < n) {
          go[make_pair(a[i].x2, a[i].y2)] = np;
          belong[make_pair(a[i].x2, a[i].y2)] = a[i].id;
        } else {
          fstGo[a[i].id - n] = np;
        }
        belong[make_pair(a[i].x2, max(a[i].y2, p.first))] = p.second;
      }
    }
    if (a[i].id < n)
      segt.upd(min(a[i].x1, a[i].x2), max(a[i].x1, a[i].x2) + 1,
               make_pair(a[i].getLowCol(), a[i].id));
  }
  sort(a, a + n + q, cmp2);
  segt.init();
  for (int i = 0; i < n + q; ++i) {
    if (a[i].dir == 1) {
      pair<int, int> p = segt.qry(a[i].x2);
      if (p.first != inf) {
        pair<pair<int, int>, long long> np =
            make_pair(make_pair(a[i].x2, min(a[i].y2, -p.first)),
                      max(0, a[i].y2 + p.first));
        if (a[i].id < n) {
          go[make_pair(a[i].x2, a[i].y2)] = np;
          belong[make_pair(a[i].x2, a[i].y2)] = a[i].id;
        } else {
          fstGo[a[i].id - n] = np;
        }
        belong[make_pair(a[i].x2, min(a[i].y2, -p.first))] = p.second;
      }
    }
    if (a[i].id < n)
      segt.upd(min(a[i].x1, a[i].x2), max(a[i].x1, a[i].x2) + 1,
               make_pair(-a[i].getHighCol(), a[i].id));
  }
  sort(a, a + n + q, cmp3);
  segt.init();
  for (int i = 0; i < n + q; ++i) {
    if (a[i].dir == 3) {
      pair<int, int> p = segt.qry(a[i].y2);
      if (p.first != inf) {
        pair<pair<int, int>, long long> np =
            make_pair(make_pair(max(a[i].x2, p.first), a[i].y2),
                      max(0, p.first - a[i].x2));
        if (a[i].id < n) {
          go[make_pair(a[i].x2, a[i].y2)] = np;
          belong[make_pair(a[i].x2, a[i].y2)] = a[i].id;
        } else {
          fstGo[a[i].id - n] = np;
        }
        belong[make_pair(max(a[i].x2, p.first), a[i].y2)] = p.second;
      }
    }
    if (a[i].id < n)
      segt.upd(min(a[i].y1, a[i].y2), max(a[i].y1, a[i].y2) + 1,
               make_pair(a[i].getLowRow(), a[i].id));
  }
  sort(a, a + n + q, cmp4);
  segt.init();
  for (int i = 0; i < n + q; ++i) {
    if (a[i].dir == 2) {
      pair<int, int> p = segt.qry(a[i].y2);
      if (p.first != inf) {
        pair<pair<int, int>, long long> np =
            make_pair(make_pair(min(a[i].x2, -p.first), a[i].y2),
                      max(0, a[i].x2 + p.first));
        if (a[i].id < n) {
          go[make_pair(a[i].x2, a[i].y2)] = np;
          belong[make_pair(a[i].x2, a[i].y2)] = a[i].id;
        } else {
          fstGo[a[i].id - n] = np;
        }
        belong[make_pair(min(a[i].x2, -p.first), a[i].y2)] = p.second;
      }
    }
    if (a[i].id < n)
      segt.upd(min(a[i].y1, a[i].y2), max(a[i].y1, a[i].y2) + 1,
               make_pair(-a[i].getHighRow(), a[i].id));
  }
  for (int i = 0; i < n; ++i) belong[arrow[i].second] = i;
  for (auto p : belong) vec.push_back(p.first);
  for (int i = 0; i < vec.size(); ++i) has[belong[vec[i]]].push_back(i);
  for (int i = 0; i < 50; ++i) Go[i].resize(vec.size());
  for (int i = 0; i < vec.size(); ++i) Go[0][i] = make_pair(i, 0);
  for (int i = 0; i < n; ++i) {
    int x1 = arrow[i].first.first, y1 = arrow[i].first.second;
    int x2 = arrow[i].second.first, y2 = arrow[i].second.second;
    int tp = (x1 == x2 ? y1 < y2 ? 0 : 1 : x1 < x2 ? 3 : 2);
    sort(has[i].begin(), has[i].end(), [&](int a, int b) {
      return (vec[a].first - vec[b].first + vec[a].second - vec[b].second) /
                 (dx[tp] + dy[tp]) <
             0;
    });
    for (int j = 0; j + 1 < has[i].size(); ++j) {
      Go[0][has[i][j]] =
          make_pair(has[i][j + 1],
                    abs(vec[has[i][j]].first - vec[has[i][j + 1]].first +
                        vec[has[i][j]].second - vec[has[i][j + 1]].second));
    }
    if (!go.count(vec[has[i].back()])) continue;
    pair<pair<int, int>, long long> p = go[vec[has[i].back()]];
    Go[0][has[i].back()] = make_pair(
        lower_bound(vec.begin(), vec.end(), p.first) - vec.begin(), p.second);
  }
  for (int j = 1; j < 50; ++j) {
    for (int i = 0; i < vec.size(); ++i) {
      pair<int, long long> p = Go[j - 1][i];
      pair<int, long long> np = Go[j - 1][p.first];
      Go[j][i] = make_pair(np.first, np.second + p.second);
      if (Go[j][i].second > linf) Go[j][i].second = linf;
    }
  }
  for (int i = 0; i < q; ++i) {
    pair<pair<int, int>, long long> p = fstGo[i];
    int x = qry[i].first.first, y = qry[i].first.second;
    int tp = qry[i].second.first;
    long long t = qry[i].second.second;
    if (p.first.first == inf || t < p.second) {
      mov(x, y, tp, t);
    } else {
      int nw = lower_bound(vec.begin(), vec.end(), p.first) - vec.begin();
      t -= p.second;
      for (int j = 50 - 1; ~j; --j) {
        pair<int, long long> P = Go[j][nw];
        if (P.second <= t) {
          t -= P.second;
          nw = P.first;
        }
      }
      mov(vec[nw].first, vec[nw].second, dir[belong[vec[nw]]], t);
    }
  }
  return 0;
}
