#include <bits/stdc++.h>
#pragma G++ optimize(3)
using namespace std;
set<pair<int, int> > fnd[2][1010];
int tp[1010];
int des[1010][2];
int n, b;
int q;
int dir[1010];
int typ, dr;
int x, y;
bool vis[1010];
int nxt[1010][61];
long long len[1010][61];
int lst;
int myabs(int x) {
  if (x < 0) return -x;
  return x;
}
void build() {
  int cur, tm;
  while (1) {
    if (vis[lst]) return;
    vis[lst] = true;
    if (!typ) {
      set<pair<int, int> >::iterator it =
          fnd[0][x].lower_bound(make_pair(y, 0));
      if (dr == -1 && (*it).first != y) it--;
      tm = myabs((*it).first - y);
      y = (*it).first;
      if (y == -1 || y == b + 1) {
        nxt[lst][0] = 0;
        len[lst][0] = tm;
        return;
      }
      cur = (*it).second;
      typ = tp[cur];
      dr = dir[cur];
    } else {
      set<pair<int, int> >::iterator it =
          fnd[1][y].lower_bound(make_pair(x, 0));
      if (dr == -1 && (*it).first != x) it--;
      tm = myabs((*it).first - x);
      x = (*it).first;
      if (x == -1 || x == b + 1) {
        nxt[lst][0] = 0;
        len[lst][0] = tm;
        return;
      }
      cur = (*it).second;
      typ = tp[cur];
      dr = dir[cur];
    }
    if (!typ) {
      int nd = (des[cur][1] - y) / dr + 1;
      y += dr * nd;
      tm += nd;
    } else {
      int nd = (des[cur][0] - x) / dr + 1;
      x += dr * nd;
      tm += nd;
    }
    nxt[lst][0] = cur;
    len[lst][0] = tm;
    lst = cur;
  }
}
int gt(long long &t, int cur) {
  int w = 0;
  for (int i = 0; i <= 60; i++) {
    if (len[cur][i] > t || !nxt[cur][i]) {
      w = i;
      break;
    }
  }
  if (!w) return cur;
  t -= len[cur][w - 1];
  return gt(t, nxt[cur][w - 1]);
}
void solve(long long t) {
  int cur;
  if (!typ) {
    set<pair<int, int> >::iterator it = fnd[0][x].lower_bound(make_pair(y, 0));
    if (dr == -1 && (*it).first != y) it--;
    if (myabs((*it).first - y) > t) {
      y += t * dr;
      return;
    }
    t -= myabs((*it).first - y);
    y = (*it).first;
    if (y == -1 || y == b + 1) {
      if (y == -1)
        y = 0;
      else
        y = b;
      return;
    }
    cur = (*it).second;
    typ = tp[cur];
    dr = dir[cur];
  } else {
    set<pair<int, int> >::iterator it = fnd[1][y].lower_bound(make_pair(x, 0));
    if (dr == -1 && (*it).first != x) it--;
    if (myabs((*it).first - x) > t) {
      x += t * dr;
      return;
    }
    t -= myabs((*it).first - x);
    x = (*it).first;
    if (x == -1 || x == b + 1) {
      if (x == -1)
        x = 0;
      else
        x = b;
      return;
    }
    cur = (*it).second;
    typ = tp[cur];
    dr = dir[cur];
  }
  if (!typ) {
    int nd = (des[cur][1] - y) / dr;
    if (nd > t) {
      y += t * dr;
      return;
    }
    t -= nd;
    y += dr * nd;
  } else {
    int nd = (des[cur][0] - x) / dr;
    if (nd > t) {
      x += t * dr;
      return;
    }
    t -= nd;
    x += dr * nd;
  }
  cur = gt(t, cur);
  x = des[cur][0];
  y = des[cur][1];
  if (!t) return;
  dr = dir[cur];
  typ = tp[cur];
  if (!typ)
    y += dr;
  else
    x += dr;
  t--;
  if (!typ) {
    set<pair<int, int> >::iterator it = fnd[0][x].lower_bound(make_pair(y, 0));
    if (dr == -1 && (*it).first != y) it--;
    if (myabs((*it).first - y) > t) {
      y += t * dr;
      return;
    }
    t -= myabs((*it).first - y);
    y = (*it).first;
    if (y == -1 || y == b + 1) {
      if (y == -1)
        y = 0;
      else
        y = b;
      return;
    }
    cur = (*it).second;
    typ = tp[cur];
    dr = dir[cur];
  } else {
    set<pair<int, int> >::iterator it = fnd[1][y].lower_bound(make_pair(x, 0));
    if (dr == -1 && (*it).first != x) it--;
    if (myabs((*it).first - x) > t) {
      x += t * dr;
      return;
    }
    t -= myabs((*it).first - x);
    x = (*it).first;
    if (x == -1 || x == b + 1) {
      if (x == -1)
        x = 0;
      else
        x = b;
      return;
    }
    cur = (*it).second;
    typ = tp[cur];
    dr = dir[cur];
  }
  if (!typ) {
    int nd = (des[cur][1] - y) / dr;
    if (nd > t) {
      y += t * dr;
      return;
    }
    t -= nd;
    y += dr * nd;
  } else {
    int nd = (des[cur][0] - x) / dr;
    if (nd > t) {
      x += t * dr;
      return;
    }
    t -= nd;
    x += dr * nd;
  }
}
int main() {
  scanf("%d%d", &n, &b);
  for (int i = 1; i <= n; i++) {
    int x1, y1, x2, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    des[i][0] = x2;
    des[i][1] = y2;
    if (x1 == x2) {
      tp[i] = 0;
      if (y1 < y2)
        dir[i] = 1;
      else
        dir[i] = -1;
      for (int j = y1; j != y2 + dir[i]; j += dir[i])
        fnd[0][x1].insert(make_pair(j, i)), fnd[1][j].insert(make_pair(x1, i));
    } else {
      tp[i] = 1;
      if (x1 < x2)
        dir[i] = 1;
      else
        dir[i] = -1;
      for (int j = x1; j != x2 + dir[i]; j += dir[i])
        fnd[0][j].insert(make_pair(y1, i)), fnd[1][y1].insert(make_pair(j, i));
    }
  }
  for (int i = 0; i <= b; i++)
    fnd[0][i].insert(make_pair(-1, 0)), fnd[0][i].insert(make_pair(b + 1, 0)),
        fnd[1][i].insert(make_pair(-1, 0)),
        fnd[1][i].insert(make_pair(b + 1, 0));
  for (int i = 1; i <= n; i++) {
    if (vis[i]) continue;
    lst = i;
    x = des[i][0];
    y = des[i][1];
    dr = dir[i];
    typ = tp[i];
    if (!tp[i])
      y += dir[i];
    else
      x += dir[i];
    build();
  }
  for (int i = 1; i <= 60; i++) {
    for (int j = 1; j <= n; j++) {
      nxt[j][i] = nxt[nxt[j][i - 1]][i - 1];
      len[j][i] = len[j][i - 1] + len[nxt[j][i - 1]][i - 1];
    }
  }
  scanf("%d", &q);
  while (q--) {
    char D;
    long long t;
    scanf("%d%d%c%c%lld", &x, &y, &D, &D, &t);
    if (D == 'L' || D == 'R') {
      typ = 1;
      if (D == 'L')
        dr = -1;
      else
        dr = 1;
    } else {
      typ = 0;
      if (D == 'U')
        dr = 1;
      else
        dr = -1;
    }
    solve(t);
    printf("%d %d\n", x, y);
  }
  return 0;
}
