#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long int linf = 0x3f3f3f3f3f3f3f3f;
const long double pi = acos(-1.0);
const long double eps = 1e-9;
const int mod = 1e9 + 7;
int mh[] = {1, -1, 0, 0};
int mv[] = {0, 0, 1, -1};
int dis[31][31][4];
int grafo[31][31];
bool saiu;
int sz, b;
long long int D;
vector<pair<int, int> > resp;
void anda(int x, int y, int dir, int d) {
  if (x < 0 or y < 0 or x > b or y > b)
    saiu = true;
  else if (dis[x][y][dir] != -1) {
    sz = d - dis[x][y][dir];
    D = dis[x][y][dir];
  } else {
    dis[x][y][dir] = d;
    resp.push_back(make_pair(x, y));
    if (grafo[x][y] != -1) dir = grafo[x][y];
    anda(x + mh[dir], y + mv[dir], dir, d + 1);
  }
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n >> b;
  memset(grafo, -1, sizeof(grafo));
  while (n--) {
    int x0, y0, x1, y1, dir;
    cin >> x0 >> y0 >> x1 >> y1;
    if (x1 == x0) {
      if (y1 > y0)
        for (int y = y0; y <= y1; y++) grafo[x0][y] = 2;
      else
        for (int y = y0; y >= y1; y--) grafo[x0][y] = 3;
    } else {
      if (x1 > x0)
        for (int x = x0; x <= x1; x++) grafo[x][y0] = 0;
      else
        for (int x = x0; x >= x1; x--) grafo[x][y0] = 1;
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int x, y, d;
    char dir;
    long long int t;
    cin >> x >> y >> dir >> t;
    if (dir == 'U')
      d = 2;
    else if (dir == 'D')
      d = 3;
    else if (dir == 'L')
      d = 1;
    else
      d = 0;
    memset(dis, -1, sizeof(dis));
    saiu = false;
    resp.clear();
    anda(x, y, d, 0);
    if (t < resp.size())
      cout << resp[t].first << ' ' << resp[t].second << endl;
    else if (saiu) {
      cout << resp.rbegin()->first << ' ' << resp.rbegin()->second << endl;
    } else {
      t -= D;
      t %= sz;
      cout << resp[D + t].first << ' ' << resp[D + t].second << endl;
    }
  }
  return 0;
}
