#include <bits/stdc++.h>
using namespace std;
typedef struct G {
  int x, y;
  char d;
  long long time;
} GG;
GG g[40][40];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
int gw(char c) {
  if (c == 'U') return 0;
  if (c == 'D') return 1;
  if (c == 'L') return 2;
  if (c == 'R') return 3;
  return 0;
}
int main() {
  for (int i = 0; i < 40; i++)
    for (int j = 0; j < 40; j++) {
      g[i][j].x = i;
      g[i][j].y = j;
      g[i][j].d = 'N';
    }
  int n = 0, b = 0;
  scanf("%d%d", &n, &b);
  for (int i = 0; i < n; i++) {
    int sx, sy, ex, ey;
    scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
    if (sx == ex) {
      if (sy > ey)
        for (int j = sy; j >= ey; j--) g[sx][j].d = 'D';
      else
        for (int j = sy; j <= ey; j++) g[sx][j].d = 'U';
    } else if (sy == ey) {
      if (sx > ex)
        for (int j = sx; j >= ex; j--) g[j][sy].d = 'L';
      else
        for (int j = sx; j <= ex; j++) g[j][sy].d = 'R';
    }
  }
  int q = 0;
  scanf("%d", &q);
  int xx, yy;
  for (int i = 0; i < q; i++) {
    for (int i = 0; i < 40; i++)
      for (int j = 0; j < 40; j++) g[i][j].time = 0;
    int ix, iy;
    char cc;
    long long t;
    scanf("%d%d", &ix, &iy);
    scanf("%c", &cc);
    scanf("%c", &cc);
    scanf("%I64d", &t);
    if (g[ix][iy].d != 'N') cc = g[ix][iy].d;
    int triger = 0;
    int rnd = 0;
    while (t != 0) {
      xx = ix + dx[gw(cc)];
      yy = iy + dy[gw(cc)];
      if (xx < 0 || yy < 0 || xx > b || yy > b) break;
      if (g[xx][yy].d == 'N') {
        ix = xx;
        iy = yy;
        t--;
        if (t == 0) break;
      } else {
        ix = xx;
        iy = yy;
        cc = g[xx][yy].d;
        if (triger == 0) triger = 1;
        t--;
        if (t == 0) break;
        if (g[xx][yy].time != 0) {
          rnd = g[xx][yy].time - t;
          triger = 2;
          break;
        }
        if (triger == 1) g[xx][yy].time = t;
      }
    }
    long long rem;
    if (triger == 2) {
      rem = g[xx][yy].time % rnd;
      t = rem;
    }
    while (t != 0) {
      xx = ix + dx[gw(cc)];
      yy = iy + dy[gw(cc)];
      if (xx < 0 || yy < 0 || xx > b || yy > b) break;
      if (g[xx][yy].d == 'N') {
        ix = xx;
        iy = yy;
        t--;
        if (t == 0) break;
      } else {
        ix = xx;
        iy = yy;
        cc = g[xx][yy].d;
        t--;
        if (t == 0) break;
      }
    }
    printf("%d %d\n", ix, iy);
  }
  return 0;
}
