#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

#define dump(a) (cerr << (#a) << " = " << (a) << endl)

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; }

template<typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
	os << '(' << p.first << ", " << p.second << ')';
	return os;
}

template<class Tuple, unsigned Index>
void print_tuple(ostream& os, const Tuple& t) {}

template<class Tuple, unsigned Index, class Type, class... Types>
void print_tuple(ostream& os, const Tuple& t) {
	if(Index > 0) os << ", ";
	os << get<Index>(t);
	print_tuple<Tuple, Index + 1, Types...>(os, t);
}

template<class... Types>
ostream& operator<<(ostream& os, const tuple<Types...>& t) {
	os << '(';
	print_tuple<tuple<Types...>, 0, Types...>(os, t);
	return os << ')';
}

template<class Iterator>
ostream& dump_range(ostream&, Iterator, const Iterator&);

template<typename T>
ostream& operator<<(ostream& os, vector<T> c) {
	return dump_range(os, c.cbegin(), c.cend());
}

template<typename T>
ostream& operator<<(ostream& os, unordered_set<T> c) {
	return dump_range(os, c.cbegin(), c.cend());
}

template<class Iterator>
ostream& dump_range(ostream& os, Iterator first, const Iterator& last) {
	os << '[';
	for(int i = 0; first != last; ++i, ++first) {
		if(i) os << ",";
		os << *first;
	}
	return os << ']';
}

vector<vector<unordered_set<int>>> cyk(const vector<int>& sequence, const multimap<pair<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) {
					const auto& from = rule.first;
					const auto& to = rule.second;
					if(res[start][middle].count(from.first) && res[middle][end].count(from.second)) {
						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 multimap<pair<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);

	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];
		}

		multimap<pair<int, int>, int> rules;
		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.insert(make_pair(make_pair(prev, x), idx));
				prev = idx--;
			}

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

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

	return EXIT_SUCCESS;
}

Measured memory_bytes: 1648