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

public class towers_bm_tl {
    FastScanner in;
    PrintWriter out;
    
    class Tower implements Comparable<Tower>{
        int id, val;
        
        Tower(int id, int val) {
            this.id = id;
            this.val = val;
        }

        @Override
        public int compareTo(Tower o) {
            return Integer.compare(o.val, val);
        }
        
        
        
    }
    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];
        Tower[] towers = new Tower[m];
        for (int i = 0; i < m; i++)
            towers[i] = new Tower(0, 0);
        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;
            }
            for (int i = 0; i < m; i++) {
                towers[i].id = i;
                towers[i].val = (int)w2[i];
            }
            Arrays.sort(towers);
            long best = -1;
            int bestId1 = -1, bestId2 = -1;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++) {
                    int id1 = i;
                    int id2 = towers[j].id;
                    if (!g[id1].contains(id2)) {
                        int cur = (int) (w1[id1] + w2[id2]);
                        if (cur > best) {
                            best = cur;
                            bestId1 = id1;
                            bestId2 = id2;
                        }
                        break;
                    }
                }
            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_tl().run();
    }
}