#include <bits/stdc++.h>
using namespace std;

template<typename T>
std::vector<T> table(int n, T v) { return std::vector<T>(n, v); }

template <class... Args>
auto table(int n, Args... args) {
    auto val = table(args...);
    return std::vector<decltype(val)>(n, std::move(val));
}

constexpr int max_a = 30;
constexpr int max_k = 10;

auto calc_can_make(vector<int> const& a, vector<vector<int>> const& xs, vector<int> const& ys) {
    const int n = a.size() / 2, r_sz = xs.size();
    auto can_make = table(a.size(), a.size(), max_a + 1, false);
    // r_dp[i][j][k][l] := can match a[i..j) with xs[k][0..l) ?
    auto r_dp = table(a.size(), a.size(), r_sz, max_k + 1, false);
    for(int i = 0; i + 1 < n * 2; ++i) {
        can_make[i][i + 1][a[i]] = true;
    }
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < r_sz; ++j) {
            r_dp[i][i][j][0] = true;
        }
    }
    for(int len = 1; len <= n; ++len) {
        for(int l = 0; l + len < 2 * n; ++l) {
            const int r = l + len;
            for(int i = 0; i < r_sz; ++i) {
                for(int j = 1; j <= (int)xs[i].size(); ++j) {
                    for(int k = l; k < r; ++k) {
                        r_dp[l][r][i][j] = r_dp[l][r][i][j] | (r_dp[l][k][i][j - 1] && can_make[k][r][xs[i][j - 1]]);
                    }
                }
            }
            for(int i = 0; i < r_sz; ++i) {
                if(r_dp[l][r][i][xs[i].size()]) {
                    can_make[l][r][ys[i]] = true;
                }
            }
            for(int i = 0; i < r_sz; ++i) {
                if(can_make[l][r][xs[i][0]]) {
                    r_dp[l][r][i][1] = true;
                }
            }
        }
    }
    return can_make;
}

int main() {
    int n, m, r;
    while(cin >> n >> m >> r, n) {
        vector<int> a(n * 2), b(m * 2);
        vector<vector<int>> xs(r);
        vector<int> ys(r);
        for(int i = 0; i < n; ++i) {
            cin >> a[i];
            a[i + n] = a[i];
        }
        for(int i = 0; i < m; ++i) {
            cin >> b[i];
            b[i + m] = b[i];
        }
        for(int i = 0; i < r; ++i) {
            int k; cin >> k;
            xs[i].resize(k);
            for(auto& x : xs[i]) cin >> x;
            cin >> ys[i];
        }

        auto can_make_a = calc_can_make(a, xs, ys);
        auto can_make_b = calc_can_make(b, xs, ys);
        int ans = -1;
        for(int s1 = 0; s1 < n; ++s1) {
            for(int s2 = 0; s2 < m; ++s2) {
                vector<vector<int>> dp(n + 1, vector<int>(m + 1, -1));
                dp[0][0] = 0;
                for(int i = 0; i < n; ++i) {
                    for(int j = 0; j < m; ++j) {
                        if(dp[i][j] == -1) continue;
                        for(int k = 1; k <= 30; ++k) {
                            for(int l1 = 1; l1 <= n - i; ++l1) {
                                if(!can_make_a[s1 + i][s1 + i + l1][k]) continue;
                                for(int l2 = 1; l2 <= m - j; ++l2) {
                                    if(can_make_a[s1 + i][s1 + i + l1][k] && can_make_b[s2 + j][s2 + j + l2][k]) {
                                        dp[i + l1][j + l2] = max(dp[i + l1][j + l2], dp[i][j] + 1);
                                    }
                                }
                            }
                        }
                    }
                }
                ans = max(ans, dp[n][m]);
            }
        }

        cout << ans << endl;
    }
}



Measured memory_bytes: 14220