#include <bits/stdc++.h>   using namespace std;   const int step[8][2] = {{1, 0}, {-1, 0}, {0, 1},  {0, -1},                           {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};   template <class T>   inline T abs1(T a) {     return a < 0 ? -a : a;   }   const int maxn = 100110;   template <class t>   struct segment_node {     int be, en;     t num, add;   };   template <class t>   struct segment_tree {     int l;     segment_node<t> tree[maxn * 4];     inline int gleft(int no) { return no << 1; }     inline int gright(int no) { return (no << 1) + 1; }     inline int gfa(int no) { return no >> 1; }     inline segment_tree() { l = 0; }     void build(int no, int l, int r) {       if (l == r) {         tree[no].be = tree[no].en = l;         tree[no].num = -1;         tree[no].add = -1;         return;       }       tree[no].be = l;       tree[no].en = r;       int mid = (l + r) / 2;       build(gleft(no), l, mid);       build(gright(no), mid + 1, r);       tree[no].add = -1;     }     inline void relax(int no) {       tree[gleft(no)].add = tree[no].add;       tree[gright(no)].add = tree[no].add;       tree[no].add = -1;     }     void down(int l, int r, int no, t ranadd) {       if (tree[no].be != tree[no].en && tree[no].add != -1) relax(no);       if (l <= tree[no].be && r >= tree[no].en) {         tree[no].add = ranadd;         return;       }       int mid = (tree[no].be + tree[no].en) / 2;       if (r >= tree[no].be && l <= mid) down(l, r, gleft(no), ranadd);       if (r >= mid + 1 && l <= tree[no].en) down(l, r, gright(no), ranadd);     }     t getnum(int loc, int no) {       if (tree[no].add != -1) {         int ans = tree[no].add;         if (tree[no].be != tree[no].en) relax(no);         return ans;       }       if (tree[no].be == tree[no].en) return tree[no].num;       int mid = (tree[no].be + tree[no].en) / 2;       if (loc <= mid)         return getnum(loc, gleft(no));       else         return getnum(loc, gright(no));     }   };   segment_tree<int> sgt[4];   struct segment {     int from, to, no;     segment(int a = 0, int b = 0, int no1 = 0) {       from = min(a, b);       to = max(a, b);       no = no1;     }   };   vector<segment> orig[2][maxn];   pair<int, int> orig1[maxn * 2], origto[maxn];   long long origdir[maxn * 2], origstep[maxn];   struct vec {     int no, loc;     vec(int a = 0, int b = 0) : no(a), loc(b) {}   };   vector<vec> she[4][maxn];   map<pair<int, int>, int> trans;   long long narrow, b, nq;   struct node {     int fa, delta;     long long dis;     node(int a = 0, int b = 0, int c = 0) { fa = a, dis = b, delta = c; }   } fa[60][maxn * 2];   inline int finddir(pair<int, int> from, pair<int, int> to) {     to.first -= from.first;     to.second -= from.second;     if (to.first) to.first /= abs(to.first);     if (to.second) to.second /= abs(to.second);     for (int j = 0; j < 4; j++)       if (to.first == step[j][0] && to.second == step[j][1]) return j;     return 0;   }   inline long long cntdis(pair<int, int> co, pair<int, int> co1) {     return abs(co.first - co1.first) + abs(co.second - co1.second);   }   inline pair<long long, long long> finddis(int from, int dir, int to) {     pair<long long, long long> ans;     if (to == -1) {       ans = orig1[from];       if (dir > 1) swap(ans.first, ans.second);       if (!(dir & 1)) ans.first = b - ans.first;       ans.second = 0;       return ans;     }     if ((origdir[to] ^ dir) < 2) {       long long dis1 = orig1[from].first - orig1[to].first,                 dis2 = orig1[from].first - origto[to].first;       if (orig1[to].first == orig1[from].first &&           orig1[to].first == origto[to].first)         dis1 = orig1[from].second - orig1[to].second,         dis2 = orig1[from].second - origto[to].second;       if (dis1 * dis2 < 0) {         ans.first = 0;         ans.second = abs1(dis1);       } else {         ans.first = min(abs1(dis1), abs1(dis2));         ans.second = 0;         if (abs1(dis1) > abs1(dis2)) ans.second = abs1(dis1) - abs1(dis2);       }       return ans;     }     ans = orig1[to];     ans.first -= orig1[from].first;     ans.second -= orig1[from].second;     if (dir > 1) swap(ans.first, ans.second);     ans.first = abs1(ans.first);     ans.second = abs1(ans.second);     return ans;   }   int main() {     scanf( %I64d%I64d , &narrow, &b);     for (int i = 0; i < narrow; i++) {       pair<int, int> from, to;       scanf( %d%d%d%d , &from.first, &from.second, &to.first, &to.second);       trans[from] = i;       orig1[i] = from;       origto[i] = to;       if (from.first == to.first) {         orig[0][from.first].push_back(segment(from.second, to.second, i));         orig[1][max(from.second, to.second)].push_back(             segment(from.first, from.first, i));         orig[1][min(from.second, to.second)].push_back(             segment(from.first, from.first, i));       } else {         orig[1][from.second].push_back(segment(from.first, to.first, i));         orig[0][max(from.first, to.first)].push_back(             segment(from.second, from.second, i));         orig[0][min(from.first, to.first)].push_back(             segment(from.second, from.second, i));       }       int randir = finddir(from, to);       origdir[i] = randir;       if (randir > 1) swap(to.first, to.second);       she[randir][to.first].push_back(vec(i, to.second));     }     scanf( %I64d , &nq);     for (int i = 0; i < nq; i++) {       pair<int, int> st;       char randirc;       long long step1, randir;       scanf( %d %d %c %I64d , &st.first, &st.second, &randirc, &step1);       orig1[i + narrow] = st;       trans[st] = i + narrow;       if (randirc ==  L ) randir = 1;       if (randirc ==  R ) randir = 0;       if (randirc ==  U ) randir = 2;       if (randirc ==  D ) randir = 3;       origdir[i + narrow] = randir, origstep[i] = step1;       if (randir > 1) swap(st.first, st.second);       she[randir][st.first].push_back(vec(i + narrow, st.second));     }     for (int i = 0; i < 4; i++) {       sgt[i].build(1, 0, b + 1);       int from = 0, add = 1, to = b + 1;       if (!(i & 1)) from = b, add = -1, to = -1;       int biao = i / 2;       for (int j = from; j != to; j += add) {         for (int t = 0; t < (int)she[i][j].size(); t++)           if (she[i][j][t].no < narrow) {             fa[0][she[i][j][t].no].fa = sgt[i].getnum(she[i][j][t].loc, 1);             pair<long long, long long> randis =                 finddis(she[i][j][t].no, i, fa[0][she[i][j][t].no].fa);             fa[0][she[i][j][t].no].dis = randis.first;             fa[0][she[i][j][t].no].delta = randis.second;           }         for (int t = 0; t < (int)orig[biao][j].size(); t++)           sgt[i].down(orig[biao][j][t].from, orig[biao][j][t].to, 1,                       orig[biao][j][t].no);         for (int t = 0; t < (int)she[i][j].size(); t++)           if (she[i][j][t].no >= narrow) {             fa[0][she[i][j][t].no].fa = sgt[i].getnum(she[i][j][t].loc, 1);             pair<long long, long long> randis =                 finddis(she[i][j][t].no, i, fa[0][she[i][j][t].no].fa);             fa[0][she[i][j][t].no].dis = randis.first;             fa[0][she[i][j][t].no].delta = randis.second;           }       }     }     for (int i = 1; i < 60; i++) {       for (int j = 0; j < narrow + nq; j++) {         if (fa[i - 1][j].fa != -1) {           fa[i][j].fa = fa[i - 1][fa[i - 1][j].fa].fa;           fa[i][j].delta = fa[i - 1][fa[i - 1][j].fa].delta;           fa[i][j].dis = fa[i - 1][fa[i - 1][j].fa].dis + fa[i - 1][j].dis -                          fa[i - 1][j].delta;         } else           fa[i][j] = fa[i - 1][j];         if (fa[i][j].dis > 1000000000000000ll) fa[i][j].dis = 1000000000000001ll;       }     }     for (long long i1 = 0; i1 < nq; i1++) {       long long no = i1 + narrow;       int en = 59;       for (; en != -1;) {         int ranbe = -1, ranen = en;         while (ranbe < ranen) {           int mid = (ranbe + ranen + 1) / 2;           if (fa[mid][no].dis > origstep[i1] || fa[mid][no].fa == -1)             ranen = mid - 1;           else             ranbe = mid;         }         en = ranbe;         if (en == -1) break;         origstep[i1] -= fa[ranbe][no].dis - fa[ranbe][no].delta;         no = fa[ranbe][no].fa;       }       printf(            %I64d %I64d n ,           max(0ll, min(b, orig1[no].first + origstep[i1] * step[origdir[no]][0])),           max(0ll,               min(b, orig1[no].second + origstep[i1] * step[origdir[no]][1])));     }     return 0;   }