#include <iostream>
#include <string>
#include <cmath>
#include <vector>

using namespace std;




int main() {
    // unsync stdin and stdout
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    // N monkeys who climb and pick coconuts
    // takes A_k seconds to find coconuts on the true
    // takes B_k more seconds to actually procure a coconut
    
    
    // M monkeys who break them open
    // takes C_k seconds to find a good tool for opening the coconuts
    // takes another D_k seconds to actually open up a coconut
    
    // the two different kinds of monkeys cannot be working at the same time
    
    // once ALL coconuts picked, all N monkeys are removed
    
    // unknown time it takes for M monkeys to arrive
    
    // total time consumed by all N + M monkeys is known
    
    // NEGLIGIBLE time it takes to leave and arrive to the garden
    
    // 1 <= t <= 10^9
    unsigned long long t;
    cin >> t;
    
    // num monkey that pick coconuts
    int n;
    cin >> n;
    
    vector<unsigned long long> timeNVec(n);
    
    vector<unsigned long long> timeAVec(n);
    
    for (int i = 0; i < n; i++) {
        unsigned long long a_k;
        cin >> a_k;
        timeAVec[i] = a_k;
        
        unsigned long long b_k;
        cin >> b_k;
        timeNVec[i] = b_k;
        
    }
    
    // num monkey that crack open coconuts
    int m;
    cin >> m;
    
    vector<unsigned long long> timeMVec(m);
    vector<unsigned long long> timeCVec(m);
    
    for (int i = 0; i < m; i++) {
        unsigned long long c_k;
        cin >> c_k;
        timeCVec[i] = c_k;
        
        unsigned long long d_k;
        cin >> d_k;
        timeMVec[i] = d_k;
    }
    
    unsigned long long hi = t;
    unsigned long long lo = 1;
    for(int i = 0; i < 50; i++) {
        // time it takes for N
        unsigned long long mid = (hi + lo) / 2;
        
        // coconuts prepared by N monkeys in the experimental time
        unsigned long long totalCoconutsN = 0;
        // iterate through times taken of each N monkey and compute how many coconuts they can create;
        for (int j = 0; j < n; j++) {
            long long tExpectedN = mid;
            // subtract time it takes to process first coconut
            if (tExpectedN - (timeAVec[j] + timeNVec[j]) >= 0) {
                totalCoconutsN += 1;
            }
            else {
                continue;
            }
            tExpectedN -= timeAVec[j] + timeNVec[j];
            totalCoconutsN += (tExpectedN / timeNVec[j]);
        }
        
        // coconuts prepared by M monkeys in the other chunk of time
        unsigned long long totalCoconutsM = 0;
        
        for (int j = 0; j < m; j++) {
            // time it SHOULD take for m monkeys
            long long tExpectedM = t - mid;
            // subtract time it takes to process first coconut
            if (tExpectedM - (timeCVec[j] + timeMVec[j]) >= 0) {
                totalCoconutsM += 1;
            }
            else {
                continue;
            }
            tExpectedM -= timeCVec[j] + timeMVec[j];
            totalCoconutsM += (tExpectedM / timeMVec[j]);
        }
        if (totalCoconutsN < totalCoconutsM) {
            lo = mid + 1;
        }
        else if (totalCoconutsN > totalCoconutsM) {
            hi = mid;
        }
        else {
            cout << mid;
            break;
        }
    }
}
    
    
    
    // sum A_k with B_k, and C_k with D_k to figure out the total time it takes for a monkey to actually do anything
    // Probably want to b-search on the time consumed, figure out time consumed
    // for the N monkeys to do their job with those coconuts
    