#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
int main(){
	while(1){
		int n[2],r;
		int a[25][2];
		vector<int>ch[65]; for(int i=0;i<65;i++) ch[i].clear();
		cin >> n[0] >> n[1] >> r;
		if(n[0] == 0) return 0;
		rep(q,2) for(int i=0;i<n[q];i++) cin >> a[i][q];
		for(int i=0;i<r;i++){
			int k; cin >> k;
			for(int j=0;j<k;j++){
				int x; cin >> x;
				ch[i].pb(x);
			}
			int y; cin >> y; ch[i].pb(y);
		}
		bool dp[30][30][35][2]={};
		bool ok[30][30][30][30]={};
		for(int q=0;q<2;q++){
			for(int i=0;i<n[q];i++) dp[i][i][a[i][q]][q] = 1;
			while(1){
				bool upd = 0;
				for(int i=0;i<r;i++){
					for(int j=1;j<=n[q];j++){
						bool ok[11][25]={};
						ok[0][j-1] = 1;
						for(int k=0;k<ch[i].size()-1;k++){
							for(int s=0;s<n[q];s++){
								if(!ok[k][s]) continue;
								for(int t=s+1;t<=s+n[q];t++){
									if( (t-j)%n[q] == 0 && k) break;
									if(dp[(s+1)%n[q]][t%n[q]][ch[i][k]][q] && !ok[k+1][t%n[q]]){
										ok[k+1][t%n[q]] = 1;
									}
								}
							}
						}
						for(int k=0;k<n[q];k++){
							if(ok[ch[i].size()-1][k] && !dp[j%n[q]][k][ch[i][ch[i].size()-1]][q]){
								dp[j%n[q]][k][ch[i][ch[i].size()-1]][q] = 1;
								upd = 1;
							}
						}
					}
				}
				if(!upd) break;
			}
		}
		for(int i=0;i<n[0];i++) for(int j=0;j<n[0];j++) for(int k=0;k<n[1];k++) for(int l=0;l<n[1];l++) for(int x=1;x<=30;x++){
			if(dp[i][j][x][0] && dp[k][l][x][1]) ok[i][j][k][l] = 1;
		}
		int L[30][30][30][30];
		rep(a,30)rep(b,30)rep(c,30)rep(d,30)L[a][b][c][d]=INF;
		//syokika
		for(int x=1;x<=30;x++){
		    for(int ii=0;ii<n[0];ii++){
			    for(int i=0;i<n[1];i++){
					bool G = 0;
					for(int a=ii;(a!=ii||!G);a=(a+1)%n[0]){
						G = 1;
						if(!dp[ii][a][x][0]) continue;
						bool F = 0;
						for(int b=i;(b!=i||!F);b=(b+1)%n[1]){
							F = 1;
							if(!dp[i][b][x][1]) continue;
							L[ii][a][i][b] = 1;
						}
					}
				}
			}
		}//if(n[0]==16)return 0;
		//if(n[0]==16) cout << L[14][4][5][5] << endl;
		int ans = -1;
		for(int ii=0;ii<n[0];ii++){
			for(int i=0;i<n[1];i++){
			   // if(ii==14&&i==5) cout << L[14][4][5][5] << endl;
				bool G = 0;
				for(int j=ii;(j!=ii||!G);j=(j+1)%n[0]){//cout << "K" << endl;
					G = 1;
					bool F = 0;
					for(int k=i;(k!=i||!F);k=(k+1)%n[1]){
						F = 1;
						if(L[ii][j][i][k] > 1000) continue;
						//if(ii==14&&i==5&&j==4&&k==5) cout << "j" << endl;
						{
							for(int J=(j+1)%n[0];(J!=ii);J=(J+1)%n[0]){
								for(int K=(k+1)%n[1];(K!=i);K=(K+1)%n[1]){
									if(ok[(j+1)%n[0]][J][(k+1)%n[1]][K]){
										if(L[ii][J][i][K]==INF) L[ii][J][i][K]=-1;
										L[ii][J][i][K] = max(L[ii][J][i][K],L[ii][j][i][k]+1);
										//if(ii==14&&J==5&&i==5&&K==10)cout<<L[ii][J][i][K]<<endl;
									}
								}
							}
						}
					}
				}
				if(L[ii][(ii+n[0]-1)%n[0]][i][(i+n[1]-1)%n[1]] <= 1000) ans = max(ans,L[ii][(ii+n[0]-1)%n[0]][i][(i+n[1]-1)%n[1]]);
			}
		}
		cout << ans << endl;
	}
}

Measured memory_bytes: 7016