#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <stack>
#include <set>
#define  STATE  30
using namespace std;
int dp[STATE][STATE][STATE][STATE];
vector <int> A, B, Y, Z, X;
stack <int> stk;
int n, m, r, state;
inline void init (void)
{
    Y.clear();
    Z.clear();
    X.clear();      
    A.resize(n);
    B.resize(m);
    for (int i=0;i<n;++i) cin>>A[i];
    for (int i=0;i<m;++i) cin>>B[i];
    state = STATE;
}
inline void toChomskyForm (int y)
{
    for (;;)
    {
        Z.push_back(stk.top()); stk.pop();
        Y.push_back(stk.top()); stk.pop();      
        if (stk.empty())
        {
           X.push_back(y);
           return ;
        }
        else
        {
            X.push_back(++state);     
            stk.push(state); 
        }
    }          
}
inline void makeGrammar (void)
{
    int k, y, tmp;
    for (int i=0;i<r;++i)          
    {
        cin>>k;    
        for (int j=0;j<k;++j)
        {
            cin>>tmp;    
            stk.push(tmp);
        }
        cin>>y;
        toChomskyForm (y);
    }   
}
inline vector < vector < set<int> > > CYK ( const vector <int> &A )
{
    int n = A.size();          
    vector < vector< set<int> > > dp(n,vector< set<int> >(n+1));
    for (int i=0;i<n;++i) dp[i][1].insert(A[i]);
    
    for (int len=2;len<=n;++len)
    for (int i=0;i<n;++i)
    for (int j=0;j<X.size();++j)
    for (int k=1;k<len;++k)
    {
        int ii = (i+k)%n;    
        if (dp[i][k].count(Y[j])&&dp[ii][len-k].count(Z[j])) 
           dp[i][len].insert(X[j]);
    }      
    return dp;   
}
inline vector < vector<int> > makeBitset ( const vector < vector < set<int> > > &A )
{
    int n = A.size();
    vector < vector<int> > ans(n,vector<int>(n+1,0));
    for (int i=0;i<n;++i)
    for (int w=0;w<=n;++w)
    for (int k=0;k<=STATE;++k)
        if (A[i][w].count(k)) ans[i][w] |= 1<<k;
    return ans;
}

inline void solve (void)
{
    vector < vector<int> > ai = makeBitset( CYK(A) );
    vector < vector<int> > bi = makeBitset( CYK(B) );
    memset (dp,-1,sizeof(dp));
    for (int i=0;i<n;++i)
    for (int j=0;j<m;++j) dp[i][0][j][0] = 0;
    
    for (int nn=0;nn<n;++nn)
    for (int mm=0;mm<m;++mm)
    for (int i=0;i<n;++i)
    for (int j=0;j<m;++j)
    {
        if (dp[i][nn][j][mm] == -1) continue;    
        for (int p=nn+1;p<=n;++p)
        for (int q=mm+1;q<=m;++q)
        {
            int ii = (i+nn)%n;
            int jj = (j+mm)%m;
            if (ai[ii][p-nn] & bi[jj][q-mm])
               dp[i][p][j][q] = max(dp[i][p][j][q],dp[i][nn][j][mm]+1);
        }
    }
    int ans = -1;
    for (int i=0;i<n;++i)
    for (int j=0;j<m;++j) ans = max(ans,dp[i][n][j][m]);   
    cout<<ans<<endl;
}
int main(void)
{
    ios_base :: sync_with_stdio(0);
    //cin.open("PF.cin.txt",ios::in);
    //out.open("PF.out.txt",ios::out);
    while (cin>>n>>m>>r && n && m)
    {
          init ();
          makeGrammar ();
          solve ();
    }
    return 0;    
}