import java.io.*;
import java.util.*;

public class towers_bm {
    FastScanner in;
    PrintWriter out;

    void solve() {
        int n = in.nextInt();
        int m = in.nextInt();
        int k = in.nextInt();
        ArrayList<Integer>[] g = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            g[i] = new ArrayList<>();
        }
        for (int i = 0; i < k; i++) {
            g[in.nextInt() - 1].add(in.nextInt() - 1);
        }
        for (int i = 0; i < n; i++) {
            Collections.sort(g[i]);
        }
        int test = in.nextInt();
        long[] w1 = new long[n];
        long[] w2 = new long[m];
        long[] prefMax = new long[m + 1];
        long[] sufMax = new long[m];
        int[] prefMaxID = new int[m + 1];
        int[] sufMaxID = new int[m];
        for (int t = 0; t < test; t++) {
            long a = in.nextInt();
            long b = in.nextInt();
            long c = in.nextInt();
            long d = in.nextInt();
            w1[0] = a;
            for (int i = 1; i < n; i++) {
                a = w1[i] = (a * b + c) % d;
            }
            for (int i = 0; i < m; i++) {
                a = w2[i] = (a * b + c) % d;
            }
            long best = -1;
            int bestId1 = -1, bestId2 = -1;
            Arrays.fill(prefMax, -1);
            Arrays.fill(sufMax, -1);
            for (int i = 1; i < m + 1; i++) {
                if (w2[i - 1] > prefMax[i - 1]) {
                    prefMax[i] = w2[i - 1];
                    prefMaxID[i] = i - 1;
                } else {
                    prefMax[i] = prefMax[i - 1];
                    prefMaxID[i] = prefMaxID[i - 1];
                }
            }
            for (int i = m - 2; i >= 0; i--) {
                if (w2[i + 1] > sufMax[i + 1]) {
                    sufMax[i] = w2[i + 1];
                    sufMaxID[i] = i + 1;
                } else {
                    sufMax[i] = sufMax[i + 1];
                    sufMaxID[i] = sufMaxID[i + 1];
                }
            }
            for (int i = 0; i < n; i++) {
                if (g[i].size() == 0) {
                    int can = (int) (w1[i] + prefMax[m]);
                    if (prefMax[m] != -1)
                        if (can > best) {
                            best = can;
                            bestId1 = i;
                            bestId2 = prefMaxID[m];
                        }
                } else {
                    int first = g[i].get(0);
                    int last = g[i].get(g[i].size() - 1);
                    int can = (int) (w1[i] + prefMax[first]);
                    if (prefMax[first] != -1)
                        if (can > best) {
                            best = can;
                            bestId1 = i;
                            bestId2 = prefMaxID[first];
                        }
                    can = (int) (w1[i] + sufMax[last]);
                    if (sufMax[last] != -1)
                        if (can > best) {
                            best = can;
                            bestId1 = i;
                            bestId2 = sufMaxID[last];
                        }
                    int it = 0;
                    for (int j = first; j <= last; j++) {
                        while (it != g[i].size() && g[i].get(it) < j)
                            it++;
                        if (it == g[i].size() || g[i].get(it) == j)
                            continue;
                        can = (int) (w1[i] + w2[j]);
                        if (can > best) {
                            best = can;
                            bestId1 = i;
                            bestId2 = j;
                        }
                    }
                }
            }
            if (best == -1) {
                out.println(-1);
            } else {
                out.println((bestId1 + 1) + " " + (bestId2 + 1));
            }
        }
    }

    void run() {
        try {
            in = new FastScanner(new File("towers.in"));
            out = new PrintWriter(new File("towers.out"));

            solve();

            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    void runIO() {

        in = new FastScanner(System.in);
        out = new PrintWriter(System.out);

        solve();

        out.close();
    }

    class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(File f) {
            try {
                br = new BufferedReader(new FileReader(f));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        public FastScanner(InputStream f) {
            br = new BufferedReader(new InputStreamReader(f));
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return null;
                st = new StringTokenizer(s);
            }
            return st.nextToken();
        }

        boolean hasMoreTokens() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return false;
                st = new StringTokenizer(s);
            }
            return true;
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }
    }

    public static void main(String[] args) {
        new towers_bm().run();
    }
}