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;
struct res {
  int x, y, z;
} xun[200010], arr[200010];
int i, j, k, n, m, K, t, TY, num;
int fa[200010][20], id[200010], tr[200010 * 4], f[200010], Id[200010],
    Se[200010], dep[200010];
long long ti[200010], len[200010][20], sum[200010];
vector<pair<int, long long> > c[200010], hu[200010];
set<pair<int, int> > Set[200010];
struct wen {
  int x, y, ty, di, id;
  inline void read() {
    char p[3];
    scanf("%d%d%s", &x, &y, p);
    if (p[0] == 'U' || p[0] == 'D')
      ty = 1;
    else
      ty = 2;
    if (p[0] == 'U' || p[0] == 'R')
      di = 1;
    else
      di = -1;
    id = i;
  }
} b[200010], q[200010], w[200010];
struct ww {
  int x, l, r, di, ty;
  inline void read() {
    int x1, y1, x2, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    if (x1 == x2) {
      x = x1, ty = 1;
      l = y1, r = y2;
    } else {
      x = y1, ty = 2;
      l = x1, r = x2;
    }
    if (l > r)
      swap(l, r), di = -1;
    else
      di = 1;
    if (ty == 1)
      y2 += di;
    else
      x2 += di;
    b[++t] = (wen){x2, y2, ty, di, -i};
    w[i] = (wen){x1, y1, ty, di};
  }
} a[200010];
inline bool cc(const wen &A, const wen &B) {
  if (TY == 1) return A.y > B.y;
  if (TY == 2) return A.y < B.y;
}
inline bool cc1(const int &A, const int &B) {
  if (TY == 1) return a[A].x > a[B].x;
  if (TY == 2) return a[A].x < a[B].x;
}
void cover(int q, int x, int y, int l, int r, int k) {
  if (l <= x && y <= r) {
    tr[q] = k;
    return;
  }
  int mid = (x + y) / 2;
  if (l <= mid) cover(q * 2, x, mid, l, r, k);
  if (mid < r) cover(q * 2 + 1, mid + 1, y, l, r, k);
}
int cal(int q, int x, int y, int k) {
  if (x == y) return tr[q];
  int mid = (x + y) / 2, an = tr[q], re;
  if (k <= mid)
    re = cal(q * 2, x, mid, k);
  else
    re = cal(q * 2 + 1, mid + 1, y, k);
  return max(an, re);
}
inline void get(wen O, int x, int Ty) {
  int s1, s2;
  s1 = s2 = 0;
  if (!Ty) {
    if (!x)
      s1 = m - O.y;
    else if (a[x].ty == 1)
      s1 = max(O.y, a[x].l) - O.y,
      s2 = a[x].di > 0 ? max(O.y - a[x].l, 0) : a[x].r - max(O.y, a[x].l);
    else
      s1 = a[x].x - O.y, s2 = abs(O.x - (a[x].di > 0 ? a[x].l : a[x].r));
  } else {
    if (!x)
      s1 = O.y;
    else if (a[x].ty == 1)
      s1 = O.y - min(O.y, a[x].r),
      s2 = a[x].di > 0 ? min(a[x].r, O.y) - a[x].l : max(a[x].r - O.y, 0);
    else
      s1 = O.y - a[x].x, s2 = abs(O.x - (a[x].di > 0 ? a[x].l : a[x].r));
  }
  int A = O.id;
  if (A > 0)
    xun[A] = (res){x, s1, s2};
  else
    arr[-A] = (res){x, s1 + a[-A].r - a[-A].l + 1, s2};
}
inline void work() {
  for (i = 0; i <= m; i++) Set[i].clear();
  int s = 0;
  for (i = 1; i <= n; i++)
    if (a[i].ty == 1) {
      Set[a[i].x].insert(make_pair(a[i].l, i));
      Set[a[i].x].insert(make_pair(a[i].r, i));
    } else
      id[++s] = i;
  {
    memset(tr, 0, sizeof(tr));
    TY = 1;
    sort(b + 1, b + t + 1, cc);
    sort(id + 1, id + s + 1, cc1);
    j = 1;
    for (i = 1; i <= t; i++)
      if (b[i].ty == 1 && b[i].di == 1) {
        wen O = b[i];
        for (; j <= s; j++) {
          int A = id[j];
          if (a[A].x < O.y) break;
          cover(1, 0, m, a[A].l, a[A].r, j);
        }
        int ans = 0;
        {
          set<pair<int, int> >::iterator A =
              Set[O.x].lower_bound(make_pair(O.y, 0));
          if (A != Set[O.x].end()) ans = A->second;
        }
        {
          int A = id[cal(1, 0, m, O.x)];
          if (!ans || A && a[A].x < a[ans].l) ans = A;
        }
        get(O, ans, 0);
      }
  }
  {
    memset(tr, 0, sizeof(tr));
    TY = 2;
    sort(b + 1, b + t + 1, cc);
    sort(id + 1, id + s + 1, cc1);
    j = 1;
    for (i = 1; i <= t; i++)
      if (b[i].ty == 1 && b[i].di == -1) {
        wen O = b[i];
        for (; j <= s; j++) {
          int A = id[j];
          if (a[A].x > O.y) break;
          cover(1, 0, m, a[A].l, a[A].r, j);
        }
        int ans = 0;
        {
          set<pair<int, int> >::iterator A =
              Set[O.x].lower_bound(make_pair(O.y, 200010));
          if (A != Set[O.x].begin()) ans = (--A)->second;
        }
        {
          int A = id[cal(1, 0, m, O.x)];
          if (!ans || A && a[A].x > a[ans].r) ans = A;
        }
        get(O, ans, 1);
      }
  }
}
void dfs(int x) {
  res A = arr[x];
  int B = A.x;
  if (!B) {
    hu[num].push_back(make_pair(x, 0));
    return;
  }
  f[x] = 1;
  if (f[B] == 1) {
    int s = 0;
    for (k = x;;) {
      s++;
      Id[k] = num;
      Se[k] = s;
      sum[num] += len[k][0];
      hu[num].push_back(make_pair(k, sum[num]));
      k = arr[k].x;
      if (k == x) break;
    }
  } else
    dfs(B);
}
void Dfs(int x) {
  int i, j;
  f[x] = 1;
  for (i = 0; i < c[x].size(); i++) {
    int A = c[x][i].first;
    if (Id[x] && Id[x] == Id[A]) continue;
    dep[A] = dep[x] + 1;
    fa[A][0] = x;
    for (j = 1; j <= 19; j++) {
      fa[A][j] = fa[fa[A][j - 1]][j - 1];
      len[A][j] = len[A][j - 1] + len[fa[A][j - 1]][j - 1];
    }
    Dfs(A);
  }
}
inline void Work() {
  for (i = 1; i <= n; i++) {
    res A = arr[i];
    if (A.x) {
      int L = arr[A.x].y - arr[i].z;
      c[A.x].push_back(make_pair(i, L));
      len[i][0] = L;
    }
  }
  for (i = 1; i <= n; i++)
    if (!f[i]) {
      num++;
      dfs(i);
      for (j = 0; j < hu[num].size(); j++) {
        int A = hu[num][j].first;
        Dfs(A);
      }
    }
}
inline void print(wen A, long long ti) {
  long long x = A.x, y = A.y;
  if (A.ty == 1)
    y += ti * A.di;
  else
    x += ti * A.di;
  if (x < 0) x = 0;
  if (y < 0) y = 0;
  if (x > m) x = m;
  if (y > m) y = m;
  printf("%d %d\n", (int)x, (int)y);
}
inline void suan() {
  for (i = 1; i <= K; i++) {
    res A = xun[i];
    long long tim = ti[i];
    if (!A.x || tim <= A.y)
      print(q[i], tim);
    else {
      tim -= A.y - A.z;
      int B = A.x;
      if (tim <= arr[B].y) {
        print(w[B], tim);
        continue;
      }
      tim -= arr[B].y;
      for (j = 19; j >= 0; j--)
        if (fa[B][j] && tim >= len[B][j]) {
          tim -= len[B][j];
          B = fa[B][j];
        }
      if (Id[B]) {
        int C = Id[B];
        tim %= sum[C];
        int D = Se[B] - 1, nn = hu[C].size();
        long long st = !D ? 0 : hu[C][D - 1].second;
        if (tim >= hu[C][nn - 1].second - st) {
          tim -= hu[C][nn - 1].second - st;
          D = 0;
        }
        st = !D ? 0 : hu[C][D - 1].second;
        int l = D, r = nn - 1, mid;
        for (; l <= r;) {
          mid = (l + r) / 2;
          if (hu[C][mid].second - st <= tim)
            l = mid + 1;
          else
            r = mid - 1;
        }
        if (l) tim -= hu[C][l - 1].second - st;
        B = hu[C][l].first;
      }
      if (arr[B].x)
        tim += arr[B].z, B = arr[B].x;
      else
        tim += arr[B].y;
      print(w[B], tim);
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) a[i].read();
  scanf("%d", &K);
  for (i = 1; i <= K; i++) b[++t].read(), scanf("%I64d", &ti[i]), q[i] = b[t];
  for (i = 1; i <= t; i++) b[i].ty = 3 - b[i].ty, swap(b[i].x, b[i].y);
  for (i = 1; i <= n; i++) a[i].ty = 3 - a[i].ty;
  work();
  for (i = 1; i <= t; i++) b[i].ty = 3 - b[i].ty, swap(b[i].x, b[i].y);
  for (i = 1; i <= n; i++) a[i].ty = 3 - a[i].ty;
  work();
  Work();
  suan();
  return 0;
}
