#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair <int, int> PII;
typedef pair <ll, ll> PLL;
const int N = 50005;
int c, n, m;
PII share[N];
PLL packs[N];

ll gcd(ll x, ll y) {
	return y ? gcd(y, x % y) : x;
}

int main() {
	bool first = 1;
	while (scanf("%d", &c) != EOF) {
		if (!first) puts("");
		first = 0;
		scanf("%d%d", &n, &m);
		rep (i, n) scanf("%d%d", &share[i].first, &share[i].second);
		n = 0;
		rep (i, m) {
			packs[i] = PLL(0, 0);
			int num;
			scanf("%d", &num);
			rep (j, num) {
				int id, value;
				scanf("%d%d", &id, &value);
				id--;
				packs[i].first += share[id].first * value;
				packs[i].second += share[id].second * value;
			}
			if (packs[i].first < packs[i].second) {
				packs[n] = packs[i];
				packs[n].second = packs[n].second - packs[n].first;
				n++;
			}
		}
//		printf("n = %d\n", n);
		ll g = packs[0].first;
		rep (i, n) g = gcd(g, packs[i].first);
		ll C = c;
		cout << "g = " << g << endl;
		cout << "c = " << c << endl;
		cout << "n = " << n << endl;
		c = c / g + 1;
		cout << "current c = " << c << endl;
		vector <ll> f;
		f.resize(c + 1);
		fill(f.begin(), f.end(), -1);
		m = 0;
		f[0] = 0;
		rep (i, n) {
			packs[i].first /= g;
			int newm = m;
			for (int j = min((ll)m, c - packs[i].first); j >= 0; j--) {
				if (f[j] != -1 && f[j + packs[i].first] < f[j] + packs[i].second) {
					f[j + packs[i].first] = f[j] + packs[i].second;
					if (j + packs[i].first > newm)
						newm = j + packs[i].first;
				}
			}
			m = newm;
		}
		cout << "m = " << m << endl;
		ll ans = 0;
		rep (i, m + 1) if (i * g <= C && f[i] > ans) ans = f[i];
		cout << ans << "\n";
	}
}
