#include <bits/stdc++.h>
using namespace std;
const int inf = ~0U >> 1;
const long long INF = ~0ULL >> 1;
const int maxn = 120000;
int x2[maxn], y2[maxn], nxtid[maxn];
int Dir[maxn];
int M, N;
int f[maxn][52];
long long g[maxn][52];
set<pair<int, int> > S[4][maxn];
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
inline int inside(int x, int y) { return x > 0 && y > 0 && x <= N && y <= N; }
struct Segment_Tree {
  struct node {
    int l, r;
    set<pair<int, int> > s;
  } t[maxn * 5];
  void build(int p, int l, int r) {
    t[p].l = l;
    t[p].r = r;
    if (l != r) {
      build(p << 1, l, l + r >> 1);
      build(p << 1 | 1, (l + r >> 1) + 1, r);
    }
  }
  pair<int, int> query(int p, int target, int now, int Dir) {
    pair<int, int> ret;
    if (Dir == 0 || Dir == 2) {
      ret = (make_pair(inf, -1));
      set<pair<int, int> >::iterator it =
          t[p].s.lower_bound((make_pair(now, 0)));
      if (it != t[p].s.end()) ret = min(ret, *it);
    } else {
      ret = (make_pair(-inf, -1));
      set<pair<int, int> >::iterator it =
          t[p].s.upper_bound((make_pair(now, inf)));
      if (it != t[p].s.begin()) {
        --it;
        ret = max(ret, *it);
      }
    }
    if (t[p].l == t[p].r) return ret;
    int m = t[p].l + t[p].r >> 1;
    if (target <= m) {
      pair<int, int> tmp = query(p << 1, target, now, Dir);
      if (Dir == 0 || Dir == 2)
        return min(tmp, ret);
      else
        return max(tmp, ret);
    } else {
      pair<int, int> tmp = query(p << 1 | 1, target, now, Dir);
      if (Dir == 0 || Dir == 2)
        return min(tmp, ret);
      else
        return max(tmp, ret);
    }
  }
  void modify(int p, int l, int r, int x, int y) {
    if (t[p].l == l && t[p].r == r) {
      t[p].s.insert((make_pair(x, y)));
      return;
    }
    int m = t[p].l + t[p].r >> 1;
    if (r <= m)
      modify(p << 1, l, r, x, y);
    else if (l > m)
      modify(p << 1 | 1, l, r, x, y);
    else
      modify(p << 1, l, m, x, y), modify(p << 1 | 1, m + 1, r, x, y);
  }
} T[2];
pair<int, int> walk(int cur, long long maxt) {
  for (int i = (50); i >= (0); --i)
    if (f[cur][i] && maxt >= g[cur][i]) {
      maxt -= g[cur][i];
      cur = f[cur][i];
    }
  int curx = x2[cur], cury = y2[cur];
  if (Dir[cur] == 0) {
    pair<int, int> tmp = T[1].query(1, x2[cur], y2[cur], Dir[cur]);
    set<pair<int, int> >::iterator it =
        S[1][curx].lower_bound((make_pair(cury, 0)));
    if (it != S[1][curx].end() && max(y2[it->second], cury) < tmp.first) {
      long long step = max(y2[it->second], cury) - cury;
      if (step >= maxt)
        return (make_pair(int(curx + dx[Dir[cur]] * maxt),
                          int(cury + dy[Dir[cur]] * maxt)));
      cury += step;
      maxt -= step;
      step = cury - y2[it->second];
      if (step >= maxt)
        return (make_pair(int(curx + dx[Dir[it->second]] * maxt),
                          int(cury + dy[Dir[it->second]] * maxt)));
    }
    if (tmp.second == -1) {
      if (maxt <= N - y2[cur])
        return (make_pair(x2[cur], y2[cur] + maxt));
      else
        return (make_pair(x2[cur], N));
    } else {
      if (maxt <= tmp.first - y2[cur])
        return (make_pair(x2[cur], y2[cur] + maxt));
      maxt -= (tmp.first - y2[cur]);
      return (make_pair(x2[cur] + dx[Dir[tmp.second]] * maxt,
                        tmp.first + dy[Dir[tmp.second]] * maxt));
    }
  } else if (Dir[cur] == 1) {
    pair<int, int> tmp = T[1].query(1, x2[cur], y2[cur], Dir[cur]);
    set<pair<int, int> >::iterator it =
        S[0][curx].upper_bound((make_pair(cury, inf)));
    if (it != S[0][curx].begin() && min(y2[(--it)->second], cury) > tmp.first) {
      long long step = cury - min(y2[it->second], cury);
      if (step >= maxt)
        return (make_pair(int(curx + dx[Dir[cur]] * maxt),
                          int(cury + dy[Dir[cur]] * maxt)));
      cury -= step;
      maxt -= step;
      step = y2[it->second] - cury;
      if (step >= maxt)
        return (make_pair(int(curx + dx[Dir[it->second]] * maxt),
                          int(cury + dy[Dir[it->second]] * maxt)));
    }
    if (tmp.second == -1) {
      if (maxt <= y2[cur])
        return (make_pair(x2[cur], y2[cur] - maxt));
      else
        return (make_pair(x2[cur], 0));
    } else {
      if (maxt <= y2[cur] - tmp.first)
        return (make_pair(x2[cur], y2[cur] - maxt));
      maxt -= (y2[cur] - tmp.first);
      return (make_pair(x2[cur] + dx[Dir[tmp.second]] * maxt,
                        tmp.first + dy[Dir[tmp.second]] * maxt));
    }
  } else if (Dir[cur] == 2) {
    pair<int, int> tmp = T[0].query(1, y2[cur], x2[cur], Dir[cur]);
    set<pair<int, int> >::iterator it =
        S[3][cury].lower_bound((make_pair(curx, 0)));
    if (it != S[3][cury].end() && max(x2[it->second], curx) < tmp.first) {
      long long step = abs(curx - max(curx, x2[it->second]));
      if (step >= maxt)
        return (make_pair(int(curx + dx[Dir[cur]] * maxt),
                          int(cury + dy[Dir[cur]] * maxt)));
      curx += step;
      maxt -= step;
      step = abs(x2[it->second] - curx);
      if (step >= maxt)
        return (make_pair(int(curx + dx[Dir[it->second]] * maxt),
                          int(cury + dy[Dir[it->second]] * maxt)));
    }
    if (tmp.second == -1) {
      if (maxt <= N - x2[cur])
        return (make_pair(x2[cur] + maxt, y2[cur]));
      else
        return (make_pair(N, y2[cur]));
    } else {
      if (maxt <= tmp.first - x2[cur])
        return (make_pair(x2[cur] + maxt, y2[cur]));
      maxt -= (tmp.first - x2[cur]);
      return (make_pair(tmp.first + dx[Dir[tmp.second]] * maxt,
                        y2[cur] + dy[Dir[tmp.second]] * maxt));
    }
  } else {
    pair<int, int> tmp = T[0].query(1, y2[cur], x2[cur], Dir[cur]);
    set<pair<int, int> >::iterator it =
        S[2][cury].upper_bound((make_pair(curx, inf)));
    if (it != S[2][cury].begin() && min(x2[(--it)->second], curx) > tmp.first) {
      long long step = abs(curx - min(x2[it->second], curx));
      if (step >= maxt)
        return (make_pair(int(curx + dx[Dir[cur]] * maxt),
                          int(cury + dy[Dir[cur]] * maxt)));
      curx -= step;
      maxt -= step;
      step = abs(x2[it->second] - curx);
      if (step >= maxt)
        return (make_pair(int(curx + dx[Dir[it->second]] * maxt),
                          int(cury + dy[Dir[it->second]] * maxt)));
    }
    if (tmp.second == -1) {
      if (maxt <= x2[cur])
        return (make_pair(x2[cur] - maxt, y2[cur]));
      else
        return (make_pair(0, y2[cur]));
    } else {
      if (maxt <= x2[cur] - tmp.first)
        return (make_pair(x2[cur] - maxt, y2[cur]));
      maxt -= (x2[cur] - tmp.first);
      return (make_pair(tmp.first + dx[Dir[tmp.second]] * maxt,
                        y2[cur] + dy[Dir[tmp.second]] * maxt));
    }
  }
}
int main() {
  scanf("%d%d", &M, &N);
  T[0].build(1, 0, N);
  T[1].build(1, 0, N);
  for (int i = (1); i <= (M); ++i) {
    int x1, y1;
    scanf("%d%d%d%d", &x1, &y1, &x2[i], &y2[i]);
    if (x1 == x2[i]) {
      if (y1 < y2[i]) {
        T[0].modify(1, y1, y2[i], x1, i);
        Dir[i] = 0;
        S[0][x1].insert((make_pair(y1, i)));
      } else {
        T[0].modify(1, y2[i], y1, x2[i], i);
        Dir[i] = 1;
        S[1][x1].insert((make_pair(y1, i)));
      }
    } else if (x1 < x2[i]) {
      T[1].modify(1, x1, x2[i], y1, i);
      Dir[i] = 2;
      S[2][y1].insert((make_pair(x1, i)));
    } else {
      T[1].modify(1, x2[i], x1, y1, i);
      Dir[i] = 3;
      S[3][y1].insert((make_pair(x1, i)));
    }
  }
  for (int i = (1); i <= (M); ++i) {
    int curx = x2[i], cury = y2[i];
    if (Dir[i] == 0) {
      pair<int, int> tmp = T[1].query(1, x2[i], y2[i], Dir[i]);
      set<pair<int, int> >::iterator it =
          S[1][curx].lower_bound((make_pair(cury, 0)));
      if (it != S[1][curx].end() && max(y2[it->second], cury) < tmp.first) {
        long long step = max(y2[it->second], cury) - cury;
        g[i][0] += step;
        cury += step;
        step = cury - y2[it->second];
        curx += dx[Dir[it->second]] * step;
        cury += dy[Dir[it->second]] * step;
        g[i][0] += step;
        f[i][0] = it->second;
        continue;
      }
      if (tmp.second == -1)
        f[i][0] = 0, g[i][0] = N - y2[i];
      else {
        f[i][0] = tmp.second;
        g[i][0] = tmp.first - y2[i] + abs(x2[tmp.second] - x2[i]);
      }
    } else if (Dir[i] == 1) {
      pair<int, int> tmp = T[1].query(1, x2[i], y2[i], Dir[i]);
      set<pair<int, int> >::iterator it =
          S[0][curx].upper_bound((make_pair(cury, inf)));
      if (it != S[0][curx].begin() &&
          min(y2[(--it)->second], cury) > tmp.first) {
        long long step = cury - min(y2[it->second], cury);
        g[i][0] += step;
        cury -= step;
        step = y2[it->second] - cury;
        g[i][0] += step;
        curx += dx[Dir[it->second]] * step;
        cury += dy[Dir[it->second]] * step;
        f[i][0] = it->second;
        continue;
      }
      if (tmp.second == -1)
        f[i][0] = 0, g[i][0] = y2[i];
      else {
        f[i][0] = tmp.second;
        g[i][0] = y2[i] - tmp.first + abs(x2[tmp.second] - x2[i]);
      }
    } else if (Dir[i] == 2) {
      pair<int, int> tmp = T[0].query(1, y2[i], x2[i], Dir[i]);
      set<pair<int, int> >::iterator it =
          S[3][cury].lower_bound((make_pair(curx, 0)));
      if (it != S[3][cury].end() && max(x2[it->second], curx) < tmp.first) {
        long long step = abs(curx - max(curx, x2[it->second]));
        g[i][0] += step;
        curx += step;
        step = abs(x2[it->second] - curx);
        g[i][0] += step;
        curx += dx[Dir[it->second]] * step;
        cury += dy[Dir[it->second]] * step;
        f[i][0] = it->second;
        continue;
      }
      if (tmp.second == -1)
        f[i][0] = 0, g[i][0] = N - x2[i];
      else {
        f[i][0] = tmp.second;
        g[i][0] = tmp.first - x2[i] + abs(y2[tmp.second] - y2[i]);
      }
    } else {
      pair<int, int> tmp = T[0].query(1, y2[i], x2[i], Dir[i]);
      set<pair<int, int> >::iterator it =
          S[2][cury].upper_bound((make_pair(curx, inf)));
      if (it != S[2][cury].begin() &&
          min(x2[(--it)->second], curx) > tmp.first) {
        long long step = abs(curx - min(x2[it->second], curx));
        g[i][0] += step;
        curx -= step;
        step = abs(x2[it->second] - curx);
        g[i][0] += step;
        curx += dx[Dir[it->second]] * step;
        cury += dy[Dir[it->second]] * step;
        f[i][0] = it->second;
        continue;
      }
      if (tmp.second == -1)
        f[i][0] = 0, g[i][0] = x2[i];
      else {
        f[i][0] = tmp.second;
        g[i][0] = x2[i] - tmp.first + abs(y2[tmp.second] - y2[i]);
      }
    }
  }
  for (int j = (1); j <= (50); ++j)
    for (int i = (1); i <= (M); ++i) {
      if (g[i][j - 1] < INF && g[f[i][j - 1]][j - 1] < INF)
        g[i][j] = g[i][j - 1] + g[f[i][j - 1]][j - 1];
      else
        g[i][j] = INF;
      if (g[i][j] > (long long)1e16) g[i][j] = INF;
      f[i][j] = f[f[i][j - 1]][j - 1];
    }
  int Q;
  scanf("%d", &Q);
  for (int _ = (1); _ <= (Q); ++_) {
    char is[5];
    int curx, cury, dir;
    long long maxt;
    scanf("%d%d%s%lld", &curx, &cury, is, &maxt);
    if (is[0] == 'U')
      dir = 0;
    else if (is[0] == 'D')
      dir = 1;
    else if (is[0] == 'R')
      dir = 2;
    else
      dir = 3;
    if (dir == 0) {
      pair<int, int> tmp = T[1].query(1, curx, cury, dir);
      set<pair<int, int> >::iterator it =
          S[1][curx].lower_bound((make_pair(cury, 0)));
      if (it != S[1][curx].end() && max(y2[it->second], cury) < tmp.first) {
        long long step = max(y2[it->second], cury) - cury;
        if (step >= maxt) {
          printf("%d %d\n", int(curx + dx[dir] * maxt),
                 int(cury + dy[dir] * maxt));
          continue;
        }
        cury += step;
        maxt -= step;
        step = cury - y2[it->second];
        if (step >= maxt) {
          printf("%d %d\n", int(curx + dx[Dir[it->second]] * maxt),
                 int(cury + dy[Dir[it->second]] * maxt));
          continue;
        }
        curx += dx[Dir[it->second]] * step;
        cury += dy[Dir[it->second]] * step;
        maxt -= step;
        pair<int, int> ans = walk(it->second, maxt);
        printf("%d %d\n", int(ans.first), int(ans.second));
        continue;
      }
      if (tmp.second == -1) {
        long long step = min((long long)N - cury, maxt);
        printf("%d %d\n", int(curx + dx[dir] * step),
               int(cury + dy[dir] * step));
        continue;
      }
      long long step = tmp.first - cury;
      if (step >= maxt) {
        printf("%d %d\n", int(curx + dx[dir] * maxt),
               int(cury + dy[dir] * maxt));
        continue;
      }
      curx += dx[dir] * step;
      cury += dy[dir] * step;
      maxt -= step;
      step = abs(x2[tmp.second] - curx) + abs(y2[tmp.second] - cury);
      if (step >= maxt) {
        printf("%d %d\n", int(curx + dx[Dir[tmp.second]] * maxt),
               int(cury + dy[Dir[tmp.second]] * maxt));
        continue;
      }
      curx += dx[Dir[tmp.second]] * step;
      cury += dy[Dir[tmp.second]] * step;
      maxt -= step;
      pair<int, int> ans = walk(tmp.second, maxt);
      printf("%d %d\n", int(ans.first), int(ans.second));
    } else if (dir == 1) {
      pair<int, int> tmp = T[1].query(1, curx, cury, dir);
      set<pair<int, int> >::iterator it =
          S[0][curx].upper_bound((make_pair(cury, inf)));
      if (it != S[0][curx].begin() &&
          min(y2[(--it)->second], cury) > tmp.first) {
        long long step = cury - min(y2[it->second], cury);
        if (step >= maxt) {
          printf("%d %d\n", int(curx + dx[dir] * maxt),
                 int(cury + dy[dir] * maxt));
          continue;
        }
        cury -= step;
        maxt -= step;
        step = y2[it->second] - cury;
        if (step >= maxt) {
          printf("%d %d\n", int(curx + dx[Dir[it->second]] * maxt),
                 int(cury + dy[Dir[it->second]] * maxt));
          continue;
        }
        curx += dx[Dir[it->second]] * step;
        cury += dy[Dir[it->second]] * step;
        maxt -= step;
        pair<int, int> ans = walk(it->second, maxt);
        printf("%d %d\n", int(ans.first), int(ans.second));
        continue;
      }
      if (tmp.second == -1) {
        long long step = min((long long)cury, maxt);
        printf("%d %d\n", int(curx + dx[dir] * step),
               int(cury + dy[dir] * step));
        continue;
      }
      long long step = cury - tmp.first;
      if (step >= maxt) {
        printf("%d %d\n", int(curx + dx[dir] * maxt),
               int(cury + dy[dir] * maxt));
        continue;
      }
      curx += dx[dir] * step;
      cury += dy[dir] * step;
      maxt -= step;
      step = abs(x2[tmp.second] - curx) + abs(y2[tmp.second] - cury);
      if (step >= maxt) {
        printf("%d %d\n", int(curx + dx[Dir[tmp.second]] * maxt),
               int(cury + dy[Dir[tmp.second]] * maxt));
        continue;
      }
      curx += dx[Dir[tmp.second]] * step;
      cury += dy[Dir[tmp.second]] * step;
      maxt -= step;
      pair<int, int> ans = walk(tmp.second, maxt);
      printf("%d %d\n", int(ans.first), int(ans.second));
    } else if (dir == 2) {
      pair<int, int> tmp = T[0].query(1, cury, curx, dir);
      set<pair<int, int> >::iterator it =
          S[3][cury].lower_bound((make_pair(curx, 0)));
      if (it != S[3][cury].end() && max(x2[it->second], curx) < tmp.first) {
        long long step = abs(curx - max(curx, x2[it->second]));
        if (step >= maxt) {
          printf("%d %d\n", int(curx + dx[dir] * maxt),
                 int(cury + dy[dir] * maxt));
          continue;
        }
        curx += step;
        maxt -= step;
        step = abs(x2[it->second] - curx);
        if (step >= maxt) {
          printf("%d %d\n", int(curx + dx[Dir[it->second]] * maxt),
                 int(cury + dy[Dir[it->second]] * maxt));
          continue;
        }
        curx += dx[Dir[it->second]] * step;
        cury += dy[Dir[it->second]] * step;
        maxt -= step;
        pair<int, int> ans = walk(it->second, maxt);
        printf("%d %d\n", int(ans.first), int(ans.second));
        continue;
      }
      if (tmp.second == -1) {
        long long step = min((long long)N - curx, maxt);
        printf("%d %d\n", int(curx + dx[dir] * step),
               int(cury + dy[dir] * step));
        continue;
      }
      long long step = tmp.first - curx;
      if (step >= maxt) {
        printf("%d %d\n", int(curx + dx[dir] * maxt),
               int(cury + dy[dir] * maxt));
        continue;
      }
      curx += dx[dir] * step;
      cury += dy[dir] * step;
      maxt -= step;
      step = abs(x2[tmp.second] - curx) + abs(y2[tmp.second] - cury);
      if (step >= maxt) {
        printf("%d %d\n", int(curx + dx[Dir[tmp.second]] * maxt),
               int(cury + dy[Dir[tmp.second]] * maxt));
        continue;
      }
      curx += dx[Dir[tmp.second]] * step;
      cury += dy[Dir[tmp.second]] * step;
      maxt -= step;
      pair<int, int> ans = walk(tmp.second, maxt);
      printf("%d %d\n", int(ans.first), int(ans.second));
    } else {
      pair<int, int> tmp = T[0].query(1, cury, curx, dir);
      set<pair<int, int> >::iterator it =
          S[2][cury].upper_bound((make_pair(curx, inf)));
      if (it != S[2][cury].begin() &&
          min(x2[(--it)->second], curx) > tmp.first) {
        long long step = abs(curx - min(x2[it->second], curx));
        if (step >= maxt) {
          printf("%d %d\n", int(curx + dx[dir] * maxt),
                 int(cury + dy[dir] * maxt));
          continue;
        }
        curx -= step;
        maxt -= step;
        step = abs(x2[it->second] - curx);
        if (step >= maxt) {
          printf("%d %d\n", int(curx + dx[Dir[it->second]] * maxt),
                 int(cury + dy[Dir[it->second]] * maxt));
          continue;
        }
        curx += dx[Dir[it->second]] * step;
        cury += dy[Dir[it->second]] * step;
        maxt -= step;
        pair<int, int> ans = walk(it->second, maxt);
        printf("%d %d\n", int(ans.first), int(ans.second));
        continue;
      }
      if (tmp.second == -1) {
        long long step = min((long long)curx, maxt);
        printf("%d %d\n", int(curx + dx[dir] * step),
               int(cury + dy[dir] * step));
        continue;
      }
      long long step = curx - tmp.first;
      if (step >= maxt) {
        printf("%d %d\n", int(curx + dx[dir] * maxt),
               int(cury + dy[dir] * maxt));
        continue;
      }
      curx += dx[dir] * step;
      cury += dy[dir] * step;
      maxt -= step;
      step = abs(x2[tmp.second] - curx) + abs(y2[tmp.second] - cury);
      if (step >= maxt) {
        printf("%d %d\n", int(curx + dx[Dir[tmp.second]] * maxt),
               int(cury + dy[Dir[tmp.second]] * maxt));
        continue;
      }
      curx += dx[Dir[tmp.second]] * step;
      cury += dy[Dir[tmp.second]] * step;
      maxt -= step;
      pair<int, int> ans = walk(tmp.second, maxt);
      printf("%d %d\n", int(ans.first), int(ans.second));
    }
  }
  return 0;
}
