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

public class towers_bm_wa {
    FastScanner in;
    PrintWriter out;
    
    class BestX {
        int[] ids;
        int curHave;
        long[] w;
        int MAX_CNT = 5;
        
        BestX(long[] w) {
            this.w = w;
            curHave = 0;
            ids = new int[MAX_CNT + 1];
            for (int i = 0; i < w.length; i++) {
                int cur = (int) w[i];
                int place = curHave;
                while (place != 0 && w[ids[place - 1]] < cur)
                    place--;
                for (int j = curHave; j > place; j--)
                    ids[j] = ids[j - 1];
                ids[place] = i;
                curHave++;
                if (curHave == MAX_CNT + 1)
                    curHave--;
            }
        }
    }

    void solve() {
        int n = in.nextInt();
        int m = in.nextInt();
        int k = in.nextInt();
        HashSet<Integer>[] g = new HashSet[n];
        for (int i = 0; i < n; i++) {
            g[i] = new HashSet<>();
        }
        for (int i = 0; i < k; i++) {
            g[in.nextInt() - 1].add(in.nextInt() - 1);
        }
        int test = in.nextInt();
        long[] w1 = new long[n];
        long[] w2 = new long[m];
        for (int t = 0; t < test; t++) {
            long st = System.currentTimeMillis();
            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;
            BestX first = new BestX(w1);
            BestX second = new BestX(w2);
            for (int i = 0; i <first.curHave; i++)
                for (int j = 0; j < second.curHave; j++) {
                    int id1 = first.ids[i];
                    int id2 = second.ids[i];
                    if (!g[id1].contains(id2)) {
                        int cur = (int) (w1[id1] + w2[id2]);
                        if (cur > best) {
                            best = cur;
                            bestId1 = id1;
                            bestId2 = id2;
                        }
                    }
                }
            if (best == -1) {
                out.println(-1);
            } else {
                out.println((bestId1 + 1) + " " + (bestId2 + 1));
            }
            System.err.println(System.currentTimeMillis() - st);
        }
    }

    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_wa().run();
    }
}