#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const int MAX = 4e5 + 10;
const int MAXN = 1e3 + 10;
const double pi = acos(-1.0);
const int inf = 0x7f7f7ff;
const long long INF = 0x7f7f7f7f7f7f7f7f;
int dx[100] = {0, 0, 0, -1, 1};
int dy[100] = {0, 1, -1, 0, 0};
int mapp[1005][1005];
int judge(int a, int b, int c, int d) {
  if (a == c) {
    if (b > d) {
      return 2;
    }
    return 1;
  } else {
    if (a > c) {
      return 3;
    }
    return 4;
  }
}
bool flag;
int s, e;
int n, m;
long long xunhuan;
bool vis[205][205][10];
int zhongdianx, zhongdiany;
int xunhuanqi, xunhuanzhong;
long long mmm;
int ff;
void get_dfs(int ss, int ee, int zhongdianxx, int zhongdianyy, long long st,
             int fangxiang) {
  if (mapp[ss][ee] > 0) {
    fangxiang = mapp[ss][ee];
  }
  if (ss == zhongdianxx && ee == zhongdianyy && (ff == fangxiang)) {
    mmm = st;
    return;
  }
  get_dfs(ss + dx[fangxiang], ee + dy[fangxiang], zhongdianxx, zhongdianyy,
          st + 1, fangxiang);
  return;
}
void dfs(int ss, int ee, long long zong, long long st, int fangxiang) {
  if (st == zong) {
    zhongdianx = ss;
    zhongdiany = ee;
    return;
  }
  if (mapp[ss][ee] > 0) {
    fangxiang = mapp[ss][ee];
  }
  if (ss + dx[fangxiang] > m || ee + dy[fangxiang] > m ||
      ss + dx[fangxiang] < 0 || ee + dy[fangxiang] < 0) {
    zhongdianx = ss;
    zhongdiany = ee;
    return;
  }
  if (((vis[ss][ee][fangxiang]))) {
    flag = 1;
    xunhuanqi = ss;
    xunhuanzhong = ee;
    xunhuan = st;
    ff = fangxiang;
    return;
  }
  vis[ss][ee][fangxiang] = 1;
  dfs(ss + dx[fangxiang], ee + dy[fangxiang], zong, st + 1, fangxiang);
  return;
}
int main() {
  scanf("%d", &n);
  scanf("%d", &m);
  memset(mapp, 0, sizeof mapp);
  int a, b, c, d;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    scanf("%d", &b);
    scanf("%d", &c);
    scanf("%d", &d);
    int xx = judge(a, b, c, d);
    if (a == c) {
      if (b > d) {
        for (int i = d; i < b + 1; i++) {
          mapp[a][i] = xx;
        }
      } else {
        for (int i = b; i < d + 1; i++) {
          mapp[a][i] = xx;
        }
      }
    } else {
      if (a > c) {
        for (int i = c; i < a + 1; i++) {
          mapp[i][d] = xx;
        }
      } else {
        for (int i = a; i < c + 1; i++) {
          mapp[i][d] = xx;
        }
      }
    }
  }
  int q;
  scanf("%d", &q);
  char cc[3];
  for (int i = 0; i < q; i++) {
    scanf("%d", &s);
    scanf("%d", &e);
    scanf("%s", cc);
    int xx = 0;
    if (cc[0] == 'L') {
      xx = 3;
    } else if (cc[0] == 'R') {
      xx = 4;
    } else if (cc[0] == 'U') {
      xx = 1;
    } else {
      xx = 2;
    }
    long long tt;
    scanf("%lld", &tt);
    flag = 0;
    memset(vis, 0, sizeof vis);
    dfs(s, e, tt, 0, xx);
    if (flag) {
      get_dfs(s, e, xunhuanqi, xunhuanzhong, 0, xx);
      long long res = mmm;
      tt -= mmm;
      res = xunhuan - res;
      res = tt % res;
      memset(vis, 0, sizeof vis);
      dfs(xunhuanqi, xunhuanzhong, res, 0, ff);
      printf("%d %d\n", zhongdianx, zhongdiany);
    } else {
      printf("%d %d\n", zhongdianx, zhongdiany);
    }
  }
  return 0;
}
