#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <tuple>
#include <unordered_set>
#include <vector>
using namespace std;

template<class T> inline void chmax(T& a, const T& b) { if(b > a) a = b; }
template<class T> inline void chmin(T& a, const T& b) { if(b < a) a = b; }

vector<vector<unordered_set<int>>> cyk(const vector<int>& sequence, const vector<tuple<int, int, int>>& rules) {
	const int n = static_cast<int>(sequence.size());
	vector<vector<unordered_set<int>>> res(n, vector<unordered_set<int>>(n));
	for(int i = 0; i < n; ++i) {
		res[i][(i + 1) % n].insert(sequence[i]);
	}

	for(int len = 2; len <= n; ++len) {
		for(int start = 0; start < n; ++start) {
			const int end = (start + len) % n;
			for(int i = 1; i < len; ++i) {
				const int middle = (start + i) % n;
				for(const auto& rule : rules) {
					int from1, from2, to;
					tie(from1, from2, to) = rule;
					if(res[start][middle].count(from1) && res[middle][end].count(from2)) {
						res[start][end].insert(to);
					}
				}
			}
		}
	}

	return res;
}

bool intersection(const unordered_set<int>& a, const unordered_set<int>& b) {
	for(const int& element_a : a) {
		if(element_a <= 0) continue;
		if(b.find(element_a) != b.end()) return true;
	}
	return false;
}

int calc(const vector<vector<unordered_set<int>>>& analysisA, int offsetA, const vector<vector<unordered_set<int>>>& analysisB, int offsetB) {
	const int n = static_cast<int>(analysisA.size());
	const int m = static_cast<int>(analysisB.size());

	constexpr int INF = 0xfffffff;
	vector<vector<int>> dp(n + 1, vector<int>(m + 1, -INF));

	dp[0][0] = 0;
	for(int startA = 0; startA < n; ++startA) {
		for(int startB = 0; startB < m; ++startB) {
			if(dp[startA][startB] < 0) continue;

			for(int endA = startA + 1; endA <= n; ++endA) {
				for(int endB = startB + 1; endB <= m; ++endB) {
					if(intersection(analysisA[(startA + offsetA) % n][(endA + offsetA) % n], analysisB[(startB + offsetB) % m][(endB + offsetB) % m])) {
						chmax(dp[endA][endB], dp[startA][startB] + 1);
					}
				}
			}
		}
	}

	return dp[n][m];
}

int solve(const vector<int>& A, const vector<int>& B, const vector<tuple<int, int, int>>& rules) {
	const vector<vector<unordered_set<int>>> analysisA = cyk(A, rules);
	const vector<vector<unordered_set<int>>> analysisB = cyk(B, rules);

	int ans = -1;
	for(int offsetA = 0; offsetA < static_cast<int>(A.size()); ++offsetA) {
		for(int offsetB = 0; offsetB < static_cast<int>(B.size()); ++offsetB) {
			chmax(ans, calc(analysisA, offsetA, analysisB, offsetB));
		}
	}

	return ans;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	constexpr int MAX_K = 10;

	for(int n, m, r; cin >> n >> m >> r && n;) {
		vector<int> A(n);
		for(int i = 0; i < n; ++i) {
			cin >> A[i];
		}

		vector<int> B(m);
		for(int i = 0; i < m; ++i) {
			cin >> B[i];
		}

		vector<tuple<int, int, int>> rules;
		rules.reserve(r * (MAX_K - 1));
		int idx = -1;
		for(int i = 0; i < r; ++i) {
			int k;
			cin >> k;

			int prev, x, y;
			cin >> prev;

			for(int j = 1; j < k - 1; ++j) {
				cin >> x;
				rules.emplace_back(prev, x, idx);
				prev = idx--;
			}

			cin >> x >> y;
			rules.emplace_back(prev, x, y);
		}

		cout << solve(A, B, rules) << endl;
	}

	return EXIT_SUCCESS;
}

Measured memory_bytes: 1636