Problem:
G: プログラミングコンテストの作問
問題
ユウちゃんとレイくんはYPC（ユウレイプログラミングコンテスト）の開催に向けて作問をしています。
YPCで使用されるプログラミング言語は「Ython」という言語です。Ythonは変数の宣言と代入、繰り返し文、print文をサポートする単純なプログラミング言語です。
ここで、構文の記述に用いる記号の意味を、以下の表のように定めます。
記号
意味
::=
定義する
(
\( G_{1} \)
|
\( G_{2} \)
|
\( \cdots \)
|
\( G_{n} \)
)
構文 \(G_{1}\) または構文 \(G_{2}\) または \(\cdots\) または構文 \(G_{n}\)
*
直前の規則の \(0\) 回以上の繰り返し
+
直前の規則の \(1\) 回以上の繰り返し
?
直前の規則の \(0\) 回か \(1\) 回の繰り返し
Ythonの構文は以下のような構文規則で表されます。
<program> ::= ( <declaration> | <assignment> | <print> | <for> )+

<declaration> ::= <value_name><EOL>

<assignment> ::= <value_name> "<-" <expression><EOL>

<print> ::= "print(" <expression> ")" <EOL>

<for> ::= <for_element>+( <assignment> | <print> )
<for_element> ::= "for(" <expression> ")"

<expression> ::= <term1><term2>*
<term1> ::= "-"?( <number> | <value_name> )
<term2> ::= ( "+" | "-" )( <number> | <value_name> )
<number> ::= 非負整数（
10
進数表記で、先頭に余分な "+" や "0" はない）

<value_name> ::=
1
文字の英小文字

<EOL> ::= 改行文字
プログラムは最初の行から \(1\) 行ずつ順に実行されます。
<expression>
は式を表し、数値、変数、演算記号からなります。変数はその時点で代入されている値で評価されます。演算記号は
+
または
-
のみで、それぞれ加算、減算を表します。式の計算結果を \(998244353\) で割った余り（すなわち、計算結果 \(m\) について、\(m = 998244353q + r\) かつ \(0 \leq r < 998244353\) を満たす整数 \(q, r\) の \(r\) ）で最終的に評価されます。
<declaration>
は
<value_name>
で表される変数を宣言します。宣言された変数は \(0\) で初期化されます。変数は、その変数が参照される前に \(1\) 度だけ宣言されます。宣言されていない変数が参照されることはありません。
<assignment>
は
<value_name>
で表される変数に
<expression>
の評価結果を代入します。
<print>
は
<expression>
の評価結果を
<number>
と同じ構文規則で画面に出力します。この出力のあと、加えて改行文字も出力されます。
以下に、変数の宣言と代入、画面出力の例を示します。
a
には
2
が、
b
には
998244352
が格納されます。画面には
998244350<EOF>
が出力されます。
a
a<-1+1
b
b<-a-3+b
print(-a+b)
<for>
は繰り返し文を表します。
<for>
で \(L\) 個の
<for_element>
が生成されているとき、\(i\) 番目の
<for_element>
の
<expression>
の評価結果を \(n_{i}\) とすると、\(\prod_{i=1}^{L} n_{i}\) 回だけ
<assignment>
または
<print>
を実行します。なお、\(1\) つの
<for>
で繰り返される
<assignment>
または
<print>
の実行回数は \(10^{9}\) を超えません。
以下に、繰り返し文を含んだ例を示します。
\(3\) 行目の繰り返し文
\(1\) 番目の
<for_element>
の
<expression>
の評価結果は \(10\) です。
\(2\) 番目の
<for_element>
の
<expression>
の評価結果は \(1\) です。
よって、
a
と改行文字、すなわち「
5
と改行文字」の画面出力が \(10\) 回実行されます。
\(4\) 行目の繰り返し文
\(1\) 番目の
<for_element>
の
<expression>
の評価結果は \(0\) です。
よって、この行では何も実行されません。
\(5\) 行目の繰り返し文
\(1\) 番目の
<for_element>
の
<expression>
の評価結果は \(a+a=10\) です。
\(2\) 番目の
<for_element>
の
<expression>
の評価結果は \(2\) です。
よって、
a
に
a+1
が代入される処理が \(20\) 回実行され、
a
は \(25\) になります。
繰り返し文の実行中、
a
の値が変化しますが、繰り返し回数には影響がないことに注意してください。
\(6\) 行目の繰り返し文
\(1\) 番目の
<for_element>
の
<expression>
の評価結果は \(10000\) です。
\(2\) 番目の
<for_element>
の
<expression>
の評価結果は \(100000\) です。
よって、「
1
と改行文字」の画面出力が \(1000000000\) 回実行されます。
<for_element>
の
<expression>
は \(998244353\) で割った余りで評価されますが、繰り返し回数全体は割った余りで考えないことに注意してください。
a
a<-5
for(10)for(1)print(a)
for(998244353)print(a)
for(a+a)for(2)a<-a+1
for(10000)for(100000)print(1)
YPCで出題予定のある問題について、ユウちゃんが書いた解答プログラム \(S\) とレイくんが書いた解答プログラム \(T\) が与えられます。\(S, T\) はそれぞれ \(N, M\) 行からなります。
どちらの解答プログラムについても、実行に必要な時間や資源は十分用意されており、最初の行から最後の行まで正しく実行されます。
\(2\) 人の書いたプログラムをそれぞれ実行したとき、画面出力の結果が一致するかを確かめてください。
なお、この問題では複数のデータ
...(truncated)

Submitted solutions:

code_snippet: #line 2 "library/KowerKoint/base.hpp"

#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif

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

#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for(ll i = a; i < (ll)(b); i++)
#define ALL(a) (a).begin(),(a).end()
#define END(...) { print(__VA_ARGS__); return; }

#ifdef ONLINE_JUDGE
#define DBG(...) {}
#else
#define DBG(a) { cerr << #a << ": "; dbg(a); }
#endif

using VI = vector<int>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
using ll = long long;
using VL = vector<ll>;
using VVL = vector<VL>;
using VVVL = vector<VVL>;
using VD = vector<double>;
using VVD = vector<VD>;
using VVVD = vector<VVD>;
using VS = vector<string>;
using VVS = vector<VS>;
using VVVS = vector<VVS>;
using VC = vector<char>;
using VVC = vector<VC>;
using VVVC = vector<VVC>;
using P = pair<int, int>;
using VP = vector<P>;
using VVP = vector<VP>;
using VVVP = vector<VVP>;
using LP = pair<ll, ll>;
using VLP = vector<LP>;
using VVLP = vector<VLP>;
using VVVLP = vector<VVLP>;

template <typename T>
using PQ = priority_queue<T>;
template <typename T>
using GPQ = priority_queue<T, vector<T>, greater<T>>;

constexpr int INF = 1001001001;
constexpr ll LINF = 1001001001001001001ll;
constexpr int DX[] = {1, 0, -1, 0};
constexpr int DY[] = {0, 1, 0, -1};

void print() { cout << '\n'; }
template<typename T>
void print(const T &t) { cout << t << '\n'; }
template<typename Head, typename... Tail>
void print(const Head &head, const Tail &... tail) {
    cout << head << ' ';
    print(tail...);
}

#ifdef ONLINE_JUDGE
template<typename... Args>
void dbg(const Args &... args) {}
#else
void dbg() { cerr << '\n'; }
template<typename T>
void dbg(const T &t) { cerr << t << '\n'; }
template<typename Head, typename... Tail>
void dbg(const Head &head, const Tail &... tail) {
    cerr << head << ' ';
    dbg(tail...);
}
#endif

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

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
    is >> p.first >> p.second;
    return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
    for(int i = 0; i < (int) v.size(); i++) {
        os << v[i] << (i + 1 != (int) v.size() ? " " : "");
    }
    return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
    for(T &in : v) is >> in;
    return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template <typename T>
pair<VI, vector<T>> compress(const vector<T> &a) {
    int n = a.size();
    vector<T> x;
    REP(i, n) x.push_back(a[i]);
    sort(ALL(x)); x.erase(unique(ALL(x)), x.end());
    VI res(n);
    REP(i, n) res[i] = lower_bound(ALL(x), a[i]) - x.begin();
    return make_pair(res, x);
}

template <typename T>
pair<vector<T>, vector<T>> factorial(int n) {
    vector<T> res(n+1), rev(n+1);
    res[0] = 1;
    REP(i, n) res[i+1] = res[i] * (i+1);
    rev[n] = 1 / res[n];
    for(int i = n; i > 0; i--) {
        rev[i-1] = rev[i] * i;
    }
    return make_pair(res, rev);
}
#line 1 "library/ei1333/math/matrix/matrix.cpp"
template< class T >
struct Matrix {
  vector< vector< T > > A;

  Matrix() {}

  Matrix(size_t n, size_t m) : A(n, vector< T >(m, 0)) {}

  Matrix(size_t n) : A(n, vector< T >(n, 0)) {};

  size_t size() const {
     if(A.empty()) return 0;
     assert(A.size() == A[0].size());
     return A.size();
  }

  size_t height() const {
    return (A.size());
  }

  size_t width() const {
    return (A[0].size());
  }

  inline const vector< T > &operator[](int k) const {
    return (A.at(k));
  }

  inline vector< T > &operator[](int k) {
    return (A.at(k));
  }

  static Matrix I(size_t n) {
    Matrix mat(n);
    for(int i = 0; i < n; i++) mat[i][i] = 1;
    return (mat);
  }

  Matrix &operator+=(const Matrix &B) {
    size_t n = height(), m = width();
    assert(n == B.height() && m == B.width());
    for(int i = 0; i < n; i++)
      for(int j = 0; j < m; j++)
        (*this)[i][j] += B[i][j];
    return (*this);
  }

  Matrix &operator-=(const Matrix &B) {
    size_t n = height(), m = width();
    assert(n == B.height() && m == B.width());
    for(int i = 0; i < n; i++)
      for(int j = 0; j < m; j++)
        (*this)[i][j] -= B[i][j];
    return (*this);
  }

  Matrix &operator*=(const Matrix &B) {
    size_t n = height(), m = B.width(), p = width();
    assert(p == B.height());
    vector< vector< T > > C(n, vector< T >(m, 0));
    for(int i = 0; i < n; i++)
      for(int j = 0; j < m; j++)
        for(int k = 0; k < p; k++)
          C[i][j] = (C[i][j] + (*this)[i][k] * B[k][j]);
    A.swap(C);
    return (*this);
  }

  Matrix &operator^=(long long k) {
    Matrix B = Matrix::I(height());
    while(k > 0) {
      if(k & 1) B *= *this;
      *this *= *this;
      k >>= 1LL;
    }
    A.swap(B.A);
    return (*this);
  }

  Matrix operator+(const Matrix &B) const {
    return (Matrix(*this) += B);
  }

  Matrix operator-(const Matrix &B) const {
    return (Matrix(*this) -= B);
  }

  Matrix operator*(const Matrix &B) const {
    return (Matrix(*this) *= B);
  }

  Matrix operator^(const long long k) const {
    return (Matrix(*this) ^= k);
  }

  friend ostream &operator<<(ostream &os, Matrix &p) {
    size_t n = p.height(), m = p.width();
    for(int i = 0; i < n; i++) {
      os << "[";
      for(int j = 0; j < m; j++) {
        os << p[i][j] << (j + 1 == m ? "]\n" : ",");
      }
    }
    return (os);
  }


  T determinant() {
    Matrix B(*this);
    assert(width() == height());
    T ret = 1;
    for(int i = 0; i < width(); i++) {
      int idx = -1;
      for(int j = i; j < width(); j++) {
        if(B[j][i] != 0) idx = j;
      }
      if(idx == -1) return (0);
      if(i != idx) {
        ret *= -1;
        swap(B[i], B[idx]);
      }
      ret *= B[i][i];
      T vv = B[i][i];
      for(int j = 0; j < width(); j++) {
        B[i][j] /= vv;
      }
      for(int j = i + 1; j < width(); j++) {
        T a = B[j][i];
        for(int k = 0; k < width(); k++) {
          B[j][k] -= B[i][k] * a;
        }
      }
    }
    return (ret);
  }
};
#line 3 "Contests/Dummy/HUPC2021-day2/g/main.cpp"

/* #include <atcoder/all> */
/* using namespace atcoder; */
/* #include "KowerKoint/expansion/ac-library/all.hpp" */
#line 1 "ac-library/atcoder/modint.hpp"



#line 6 "ac-library/atcoder/modint.hpp"
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif

#line 1 "ac-library/atcoder/internal_math.hpp"



#line 5 "ac-library/atcoder/internal_math.hpp"

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

// @param m `1 <= m`
// @return x mod m
constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

// Fast modular multiplication by barrett reduction
// Reference: https://en.wikipedia.org/wiki/Barrett_reduction
// NOTE: reconsider after Ice Lake
struct barrett {
    unsigned int _m;
    unsigned long long im;

    // @param m `1 <= m < 2^31`
    explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

    // @return m
    unsigned int umod() const { return _m; }

    // @param a `0 <= a < m`
    // @param b `0 <= b < m`
    // @return `a * b % m`
    unsigned int mul(unsigned int a, unsigned int b) const {
        // [1] m = 1
        // a = b = im = 0, so okay

        // [2] m >= 2
        // im = ceil(2^64 / m)
        // -> im * m = 2^64 + r (0 <= r < m)
        // let z = a*b = c*m + d (0 <= c, d < m)
        // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
        // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2
        // ((ab * im) >> 64) == c or c + 1
        unsigned long long z = a;
        z *= b;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(z, im, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
        unsigned int v = (unsigned int)(z - x * _m);
        if (_m <= v) v += _m;
        return v;
    }
};

// @param n `0 <= n`
// @param m `1 <= m`
// @return `(x ** n) % m`
constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
    if (m == 1) return 0;
    unsigned int _m = (unsigned int)(m);
    unsigned long long r = 1;
    unsigned long long y = safe_mod(x, m);
    while (n) {
        if (n & 1) r = (r * y) % _m;
        y = (y * y) % _m;
        n >>= 1;
    }
    return r;
}

// Reference:
// M. Forisek and J. Jancina,
// Fast Primality Testing for Integers That Fit into a Machine Word
// @param n `0 <= n`
constexpr bool is_prime_constexpr(int n) {
    if (n <= 1) return false;
    if (n == 2 || n == 7 || n == 61) return true;
    if (n % 2 == 0) return false;
    long long d = n - 1;
    while (d % 2 == 0) d /= 2;
    constexpr long long bases[3] = {2, 7, 61};
    for (long long a : bases) {
        long long t = d;
        long long y = pow_mod_constexpr(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = y * y % n;
            t <<= 1;
        }
        if (y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);

// @param b `1 <= b`
// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
    a = safe_mod(a, b);
    if (a == 0) return {b, 0};

    // Contracts:
    // [1] s - m0 * a = 0 (mod b)
    // [2] t - m1 * a = 0 (mod b)
    // [3] s * |m1| + t * |m0| <= b
    long long s = b, t = a;
    long long m0 = 0, m1 = 1;

    while (t) {
        long long u = s / t;
        s -= t * u;
        m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b

        // [3]:
        // (s - t * u) * |m1| + t * |m0 - m1 * u|
        // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
        // = s * |m1| + t * |m0| <= b

        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    // by [3]: |m0| <= b/g
    // by g != b: |m0| < b/g
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}

// Compile time primitive root
// @param m must be prime
// @return primitive root (and minimum in now)
constexpr int primitive_root_constexpr(int m) {
    if (m == 2) return 1;
    if (m == 167772161) return 3;
    if (m == 469762049) return 3;
    if (m == 754974721) return 11;
    if (m == 998244353) return 3;
    int divs[20] = {};
    divs[0] = 2;
    int cnt = 1;
    int x = (m - 1) / 2;
    while (x % 2 == 0) x /= 2;
    for (int i = 3; (long long)(i)*i <= x; i += 2) {
        if (x % i == 0) {
            divs[cnt++] = i;
            while (x % i == 0) {
                x /= i;
            }
        }
    }
    if (x > 1) {
        divs[cnt++] = x;
    }
    for (int g = 2;; g++) {
        bool ok = true;
        for (int i = 0; i < cnt; i++) {
            if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

// @param n `n < 2^32`
// @param m `1 <= m < 2^32`
// @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64)
unsigned long long floor_sum_unsigned(unsigned long long n,
                                      unsigned long long m,
                                      unsigned long long a,
                                      unsigned long long b) {
    unsigned long long ans = 0;
    while (true) {
        if (a >= m) {
            ans += n * (n - 1) / 2 * (a / m);
            a %= m;
        }
        if (b >= m) {
            ans += n * (b / m);
            b %= m;
        }

        unsigned long long y_max = a * n + b;
        if (y_max < m) break;
        // y_max < m * (n + 1)
        // floor(y_max / m) <= n
        n = (unsigned long long)(y_max / m);
        b = (unsigned long long)(y_max % m);
        std::swap(m, a);
    }
    return ans;
}

}  // namespace internal

}  // namespace atcoder


#line 1 "ac-library/atcoder/internal_type_traits.hpp"



#line 7 "ac-library/atcoder/internal_type_traits.hpp"

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder


#line 14 "ac-library/atcoder/modint.hpp"

namespace atcoder {

namespace internal {

struct modint_base {};
struct static_modint_base : modint_base {};

template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;

}  // namespace internal

template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
    using mint = static_modint;

  public:
    static constexpr int mod() { return m; }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    static_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    static_modint(T v) {
        long long x = (long long)(v % (long long)(umod()));
        if (x < 0) x += umod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    static_modint(T v) {
        _v = (unsigned int)(v % umod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v -= rhs._v;
        if (_v >= umod()) _v += umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        unsigned long long z = _v;
        z *= rhs._v;
        _v = (unsigned int)(z % umod());
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        if (prime) {
            assert(_v);
            return pow(umod() - 2);
        } else {
            auto eg = internal::inv_gcd(_v, m);
            assert(eg.first == 1);
            return eg.second;
        }
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static constexpr unsigned int umod() { return m; }
    static constexpr bool prime = internal::is_prime<m>;
};

template <int id> struct dynamic_modint : internal::modint_base {
    using mint = dynamic_modint;

  public:
    static int mod() { return (int)(bt.umod()); }
    static void set_mod(int m) {
        assert(1 <= m);
        bt = internal::barrett(m);
    }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    dynamic_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        long long x = (long long)(v % (long long)(mod()));
        if (x < 0) x += mod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        _v = (unsigned int)(v % mod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v += mod() - rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        _v = bt.mul(_v, rhs._v);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        auto eg = internal::inv_gcd(_v, mod());
        assert(eg.first == 1);
        return eg.second;
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static internal::barrett bt;
    static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt(998244353);

using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;

namespace internal {

template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;

template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;

template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};

template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;

}  // namespace internal

}  // namespace atcoder


#line 8 "Contests/Dummy/HUPC2021-day2/g/main.cpp"
using namespace atcoder;
#line 1 "library/KowerKoint/expansion/ac-library/modint.hpp"
using MI7 = modint1000000007;
using V7 = vector<MI7>;
using VV7 = vector<V7>;
using VVV7 = vector<VV7>;
using MI3 = modint998244353;
using V3 = vector<MI3>;
using VV3 = vector<V3>;
using VVV3 = vector<VV3>;

ostream &operator<<(ostream &os, const modint &x) {
    os << x.val();
    return os;
}

ostream &operator<<(ostream &os, const MI3 &x) {
    os << x.val();
    return os;
}

ostream &operator<<(ostream &os, const MI7 &x) {
    os << x.val();
    return os;
}

istream &operator>>(istream &is, modint &x) {
    int y; is >> y;
    x = y;
    return is;
}

istream &operator>>(istream &is, MI3 &x) {
    int y; is >> y;
    x = y;
    return is;
}

istream &operator>>(istream &is, MI7 &x) {
    int y; is >> y;
    x = y;
    return is;
}
#line 10 "Contests/Dummy/HUPC2021-day2/g/main.cpp"

vector<pair<MI3, ll>> ans;
map<char, MI3> variable;

MI3 number(string str) {
    MI3 res = 0;
    REP(i, str.length()) {
        res *= 10;
        res += str[i] - '0';
    }
    return res;
}
MI3 term(string str) {
    if('0' <= str[0] && str[0] <= '9') return number(str);
    else return variable[str[0]];
}
pair<MI3, ll> expression(string str, char c) {
    int i = 0;
    int minus = 0;
    if(str[0] == '-') {
        minus = 1;
        i++;
    }
    ll cntchar = 0;
    MI3 sum = 0;
    while(i < str.length()) {
        int j = i;
        while(j < str.length() && str[j] != '+' && str[j] != '-') {
            j++;
        }
        if(str[i] == c) {
            if(minus) cntchar--;
            else cntchar++;
        } else {
            if(minus) sum -= term(str.substr(i, j - i));
            else sum += term(str.substr(i, j - i));
        }
        i = j;
        if(i == str.length()) break;
        if(str[i] == '+') minus = 0;
        else minus = 1;
        i++;
    }
    return {sum, cntchar};
}
void print(string str, ll time) {
    ans.emplace_back(expression(str.substr(6, str.length()-7), '_').first, time);
}
void assign(string str, ll time) {
    char c = str[0];
    auto res = expression(str.substr(3), c);
    Matrix<MI3> mat(2, 2);
    mat[0][0] = res.second;
    mat[0][1] = res.first;
    mat[1][1] = 1;
    mat = mat ^ time;
    variable[c] = mat[0][0] * variable[c] + mat[0][1];
}
void loop(string str) {
    int i = 0;
    ll loopnum = 1;
    while(i+3 <= str.length() && str.substr(i, 3) == "for") {
        int tojikakko = i+3;
        while(str[tojikakko] != ')') tojikakko++;
        loopnum *= expression(str.substr(i+4, tojikakko - i - 4), '_').first.val();
        i = tojikakko + 1;
    }
    string nakami = str.substr(i);
    if(nakami.length() >= 6 && nakami.substr(0, 6) == "print(") {
        print(nakami, loopnum);
    } else {
        assign(nakami, loopnum);
    }
}

void solve(){
    auto run = []() -> bool {
        int n, m; cin>> n >> m;
        if(n == 0) return false;
        vector<vector<pair<MI3, ll>>> thisans(2);
        REP(_, 2) {
            variable.clear();
            ans.clear();
            REP(__, n) {
                string program; cin >> program;
                if(program.length() >= 6 && program.substr(0, 6) == "print(") {
                    print(program, 1LL);
                } else if(program.length() >= 3 && program.substr(0, 3) == "for") {
                    loop(program);
                } else if(program.length() >= 2 && program[1] == '<') {
                    assign(program, 1LL);
                }
            }
            int i = 0, j = 0;
            /* cerr << "ans:"; */
            /* REP(i, ans.size()) cerr << ' ' << ans[i].first.x << ans[i].second; */
            /* cerr << '\n'; */
            while(i < ans.size()) {
                ll sum = 0;
                while(j < ans.size() && ans[i].first == ans[j].first) {
                    sum += ans[j].second;
                    j++;
                }
                if(sum == 0) {
                    i = j;
                    continue;
                }
                thisans[_].emplace_back(ans[i].first, sum);
                i = j;
            }
            /* cerr << "thisans[_]:"; */
            /* REP(i, thisans[_].size()) cerr << ' ' << thisans[_][i].first.x << thisans[_][i].second; */
            /* cerr << '\n'; */
            swap(n, m);
        }
        cout << (thisans[0] == thisans[1]? "Yes" : "No") << '\n';
        return true;
    };
    while(run());
}

// generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator)
int main() {
    // Fasterize input/output script
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(100);
    // scanf/printf user should delete this fasterize input/output script

    int t = 1;
    //cin >> t; // comment out if solving multi testcase
    for(int testCase = 1;testCase <= t;++testCase){
        solve();
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3492
final_rank: 1

code_snippet: #include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}
template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}
int dx[4]={0,1,0,-1}, dy[4]={1,0,-1,0};
long double eps = 1e-9;
long double pi = acos(-1);

template< typename T > istream &operator>>(istream &is, vector< T > &v) {
	for(T &in : v) is >> in;
	return is;
}

template< typename T > ostream &operator<<(ostream &os, const vector< T > &v) {
	for(int i = 0; i < (int) v.size(); i++) {
		os << v[i] << (i + 1 != (int) v.size() ? " " : "");
	}
	return os;
}

template< int mod = 1000000007 >
struct ModInt {
	int x;
	ModInt() : x(0) {}
	ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
	ModInt &operator+=(const ModInt &p) {
		if((x += p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator-=(const ModInt &p) {
		if((x += mod - p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator*=(const ModInt &p) {
		x = (int) (1LL * x * p.x % mod);
		return *this;
	}
	ModInt &operator/=(const ModInt &p) {
		*this *= p.inverse();
		return *this;
	}

	ModInt operator-() const { return ModInt(-x); }
	ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
	ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
	ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
	ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
	bool operator==(const ModInt &p) const { return x == p.x; }
	bool operator!=(const ModInt &p) const { return x != p.x; }

	ModInt inverse() const {
		int a = x, b = mod, u = 1, v = 0, t;
		while(b > 0) {
			t = a / b;
			swap(a -= t * b, b);
			swap(u -= t * v, v);
		}
		return ModInt(u);
	}

	ModInt pow(int64_t n) const {
		ModInt ret(1), mul(x);
		while(n > 0) {
			if(n & 1) ret *= mul;
			mul *= mul;
			n >>= 1;
		}
		return ret;
	}

	friend ostream &operator<<(ostream &os, const ModInt &p) {
		return os << p.x;
	}

	friend istream &operator>>(istream &is, ModInt &a) {
		int64_t t;
		is >> t;
		a = ModInt< mod >(t);
		return (is);
	}

	static int get_mod() { return mod; }
};

// const int mod = 1000000007;
const int mod = 998244353;
using mint = ModInt< mod >;

typedef vector<ll> vec;
typedef vector<vec> mat;

mat mul(mat &a,mat&b){
	mat c(a.size(),vec(b[0].size()));
	for (int i = 0; i < (int)a.size(); i++){
		for (int k = 0; k < (int)b.size(); k++){
			if(a[i][k] != 0)
			for (int j = 0; j < (int)b[0].size(); j++){
				c[i][j] += a[i][k] * b[k][j] % mod;
				if(c[i][j] >= mod) c[i][j] -= mod;
			}
		}
	}
	return c;
}

mat matpow(mat a,ll m){
	mat b(a.size(),vec(a.size()));
	for (int i = 0; i < (int)a.size(); i++){
		b[i][i]=1;
	}
	while(m>0){
		if(m&1) b=mul(b,a);
		a=mul(a,a);
		m>>=1;
	}
	return b;
}

mat A(2,vec(2));

void make_mat(array<ll,2> f){
	auto [a,b] = f;
	A[0][0]=a, A[0][1]=b;
	A[1][0]=0, A[1][1]=1;
}

ll calc(ll m, ll x){
	A = matpow(A,m);
	return (A[0][0] * x % mod + A[0][1] ) % mod;
}


ll value[222];
set<char> st;
void declaration(char &c){
	assert(st.count(c) == false);
	value[c] = 0;
	st.insert(c);
}

ll expression(string &s, int p){
	mint ret = 0;
	mint now = 0;
	bool minus = false;
	for(int i=p;i<(int)s.size();i++){
		assert(s.substr(p, 3) != "for");
		assert(s.substr(p, 2) != "or");
		assert(s[i] != '(');
		if(s[i] == ')') break;
		if(s[i] == '-'){
			if(minus)ret-=now;
			else ret+=now;
			now = 0;
			minus = true;
		}
		else if(s[i] == '+'){
			if(minus)ret-=now;
			else ret+=now;
			now = 0;
			minus = false;
		}
		else if('0' <= s[i] and s[i] <= '9'){
			now *= 10;
			now += s[i] - '0';
		}
		else {
			assert(st.count(s[i]));
			now = value[s[i]];
		}
	}
	if(minus)ret-=now;
	else ret+=now;
	// cerr << "expression: " << s.substr(p) << " ::: " << ret << endl;
	return ret.x;
}

array<ll,2> expression2(string &s, int p, char c){
	// expression: a * value[c] + b -> return (a, b)
	array<ll,2> ret = {0, expression(s, p)};
	ll cnt = 0;
	bool minus = false;
	for(int i=p;i<(int)s.size();i++){
		if(s[i] == c) {
			if(minus) cnt--;
			else cnt++;
			minus = false;
		}
		if(s[i] == '-')minus=true;
		else minus=false;
	}
	ret[0] = cnt;
	ret[1] += mod - (value[c] * cnt) % mod;
	ret[1] %= mod;
	return ret;
}

void assignment(string &s){
	value[s[0]] = expression(s, 3);
}

void assignment(char c, ll x){
	value[c] = x;
}

vector<array<ll,2>> ans;
void print(string &s, int p, ll cnt){
	// print(<expression>)
	if(cnt != 0) ans.push_back({cnt, expression(s, p+6)});
}


void for2(string &s){
	ll cnt = 1;
	for(int i=0;i<(int)s.size();i++){
		if(s.substr(i, 4) == "for("){
			cnt *= expression(s, i+4);
			// これに引っかかるのでexpressionが悪いのかなぁとか思ってるけど、分からんくて終わりです
			assert(cnt <= 1000000000);
			while(s[i] != ')') i++;
		}
		else{
			if(s.substr(i, 6) == "print(") print(s, i, cnt);
			else{
				// <assignment>
				// x<-f(x) or x<-y
				// f(x) = (x+x+x+...+x)+b = ax + b
				array<ll, 2> f = expression2(s, i+3, s[i]);
				make_mat(f);
				ll nx = calc(cnt, value[s[i]]);
				// cerr << "AAAAAAAAAAA " << cnt << " " << nx << endl;
				// cerr << f[0] << " " << f[1] << endl;
				// cerr << A[0][0] << " " << A[0][1] << endl;
				// cerr << A[1][0] << " " << A[1][1] << endl;
				value[s[i]] = nx;
			}
			break;
		}
	}
}


vector<array<ll,2>> program(vector<string> &s){
	ans.clear();
	st.clear();
	for(int i=0;i<(int)s.size();i++){
		if(s[i].size() == 1){
			declaration(s[i][0]);
		}
		else if(s[i][1] == '<' and s[i][2] == '-') {
			assignment(s[i]);
		}
		else if(s[i].substr(0,5) == "print"){
			print(s[i],0,1);
		}
		else{
			for2(s[i]);
		}
	}
	return ans;
}

bool solve(){
	int n,m;
	cin>>n>>m;
	if(n == 0 and m == 0) return false;
	vector<string> s(n), t(m);
	cin>>s>>t;

	// {
	// 	vector<array<ll,2>> vs,vt;
	// 	vs = program(s);
	// 	vt = program(t);
	// 	cerr << "s: ----------------------" << endl;
	// 	for(auto [c,v]:vs)cerr << c << " " << v << endl;
	// 	cerr << "t: ----------------------" << endl;
	// 	for(auto [c,v]:vt)cerr << c << " " << v << endl;
	// 	cerr << "AAAAAAAAAAAAAAAAA" << endl;
	// }

	if(program(s) == program(t)) cout << "Yes\n";
	else cout << "No\n";

	return true;
}


signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(20);

	while(solve());
}
accuracy: 0.05263157894736842
time_ms: 10
memory_kb: 3392
final_rank: 6

code_snippet: #include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}
template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}
int dx[4]={0,1,0,-1}, dy[4]={1,0,-1,0};
long double eps = 1e-9;
long double pi = acos(-1);

template< typename T > istream &operator>>(istream &is, vector< T > &v) {
	for(T &in : v) is >> in;
	return is;
}

template< typename T > ostream &operator<<(ostream &os, const vector< T > &v) {
	for(int i = 0; i < (int) v.size(); i++) {
		os << v[i] << (i + 1 != (int) v.size() ? " " : "");
	}
	return os;
}

template< int mod = 1000000007 >
struct ModInt {
	int x;
	ModInt() : x(0) {}
	ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
	ModInt &operator+=(const ModInt &p) {
		if((x += p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator-=(const ModInt &p) {
		if((x += mod - p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator*=(const ModInt &p) {
		x = (int) (1LL * x * p.x % mod);
		return *this;
	}
	ModInt &operator/=(const ModInt &p) {
		*this *= p.inverse();
		return *this;
	}

	ModInt operator-() const { return ModInt(-x); }
	ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
	ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
	ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
	ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
	bool operator==(const ModInt &p) const { return x == p.x; }
	bool operator!=(const ModInt &p) const { return x != p.x; }

	ModInt inverse() const {
		int a = x, b = mod, u = 1, v = 0, t;
		while(b > 0) {
			t = a / b;
			swap(a -= t * b, b);
			swap(u -= t * v, v);
		}
		return ModInt(u);
	}

	ModInt pow(int64_t n) const {
		ModInt ret(1), mul(x);
		while(n > 0) {
			if(n & 1) ret *= mul;
			mul *= mul;
			n >>= 1;
		}
		return ret;
	}

	friend ostream &operator<<(ostream &os, const ModInt &p) {
		return os << p.x;
	}

	friend istream &operator>>(istream &is, ModInt &a) {
		int64_t t;
		is >> t;
		a = ModInt< mod >(t);
		return (is);
	}

	static int get_mod() { return mod; }
};

// const int mod = 1000000007;
const int mod = 998244353;
using mint = ModInt< mod >;

typedef vector<ll> vec;
typedef vector<vec> mat;

mat mul(mat &a,mat&b){
	mat c(a.size(),vec(b[0].size()));
	for (int i = 0; i < (int)a.size(); i++){
		for (int k = 0; k < (int)b.size(); k++){
			if(a[i][k] != 0)
			for (int j = 0; j < (int)b[0].size(); j++){
				c[i][j] += a[i][k] * b[k][j] % mod;
				if(c[i][j] >= mod) c[i][j] -= mod;
			}
		}
	}
	return c;
}

mat matpow(mat a,ll m){
	mat b(a.size(),vec(a.size()));
	for (int i = 0; i < (int)a.size(); i++){
		b[i][i]=1;
	}
	while(m>0){
		if(m&1) b=mul(b,a);
		a=mul(a,a);
		m>>=1;
	}
	return b;
}

mat A(2,vec(2));

void make_mat(array<ll,2> f){
	auto [a,b] = f;
	A[0][0]=a, A[0][1]=b;
	A[1][0]=0, A[1][1]=1;
}

ll calc(ll m, ll x){
	A = matpow(A,m);
	return (A[0][0] * x % mod + A[0][1] ) % mod;
}


ll value[222];
set<char> st;
void declaration(char &c){
	assert(st.count(c) == false);
	value[c] = 0;
	st.insert(c);
}

ll expression(string &s, int p){
	mint ret = 0;
	mint now = 0;
	bool minus = false;
	for(int i=p;i<(int)s.size();i++){
		assert(s.substr(p, 3) != "for");
		assert(s.substr(p, 2) != "or");
		assert(s[i] != '(');
		if(s[i] == ')') break;
		if(s[i] == '-'){
			if(minus)ret-=now;
			else ret+=now;
			now = 0;
			minus = true;
		}
		else if(s[i] == '+'){
			if(minus)ret-=now;
			else ret+=now;
			now = 0;
			minus = false;
		}
		else if('0' <= s[i] and s[i] <= '9'){
			now *= 10;
			now += s[i] - '0';
		}
		else {
			assert(st.count(s[i]));
			now = value[s[i]];
		}
	}
	if(minus)ret-=now;
	else ret+=now;
	// cerr << "expression: " << s.substr(p) << " ::: " << ret << endl;
	return ret.x;
}

array<ll,2> expression2(string &s, int p, char c){
	// expression: a * value[c] + b -> return (a, b)
	array<ll,2> ret = {0, expression(s, p)};
	ll cnt = 0;
	bool minus = false;
	for(int i=p;i<(int)s.size();i++){
		if(s[i] == c) {
			if(minus) cnt--;
			else cnt++;
			minus = false;
		}
		if(s[i] == '-')minus=true;
		else minus=false;
	}
	ret[0] = cnt;
	ret[1] += mod - (value[c] * cnt) % mod;
	ret[1] %= mod;
	return ret;
}

void assignment(string &s){
	value[s[0]] = expression(s, 3);
}

void assignment(char c, ll x){
	value[c] = x;
}

vector<array<ll,2>> ans;
void print(string &s, int p, ll cnt){
	// print(<expression>)
	if(cnt != 0) ans.push_back({cnt, expression(s, p+6)});
}


void for2(string &s){
	ll cnt = 1;
	for(int i=0;i<(int)s.size();i++){
		if(s.substr(i, 4) == "for("){
			cnt *= expression(s, i+4);
			// assert(cnt <= 1000000000);
			while(s[i] != ')') i++;
		}
		else{
			if(s.substr(i, 6) == "print(") print(s, i, cnt);
			else{
				// <assignment>
				// x<-f(x) or x<-y
				// f(x) = (x+x+x+...+x)+b = ax + b
				array<ll, 2> f = expression2(s, i+3, s[i]);
				make_mat(f);
				ll nx = calc(cnt, value[s[i]]);
				// cerr << "AAAAAAAAAAA " << cnt << " " << nx << endl;
				// cerr << f[0] << " " << f[1] << endl;
				// cerr << A[0][0] << " " << A[0][1] << endl;
				// cerr << A[1][0] << " " << A[1][1] << endl;
				value[s[i]] = nx;
			}
			break;
		}
	}
}


vector<array<ll,2>> program(vector<string> &s){
	ans.clear();
	st.clear();
	for(int i=0;i<(int)s.size();i++){
		if(s[i].size() == 1){
			declaration(s[i][0]);
		}
		else if(s[i][1] == '<' and s[i][2] == '-') {
			assignment(s[i]);
		}
		else if(s[i].substr(0,5) == "print"){
			print(s[i],0,1);
		}
		else{
			for2(s[i]);
		}
	}
	return ans;
}

bool solve(){
	int n,m;
	cin>>n>>m;
	if(n == 0 and m == 0) return false;
	vector<string> s(n), t(m);
	cin>>s>>t;

	// {
	// 	vector<array<ll,2>> vs,vt;
	// 	vs = program(s);
	// 	vt = program(t);
	// 	cerr << "s: ----------------------" << endl;
	// 	for(auto [c,v]:vs)cerr << c << " " << v << endl;
	// 	cerr << "t: ----------------------" << endl;
	// 	for(auto [c,v]:vt)cerr << c << " " << v << endl;
	// 	cerr << "AAAAAAAAAAAAAAAAA" << endl;
	// }

	if(program(s) == program(t)) cout << "Yes\n";
	else cout << "No\n";

	return true;
}


signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(20);

	while(solve());
}
accuracy: 0.07894736842105263
time_ms: 10
memory_kb: 3424
final_rank: 5

code_snippet: #include <bits/stdc++.h>

using namespace std;
using ll = long long;
using mcl = map<char, ll>;
using pll = pair<ll, ll>;
ll mod = 998244353;

template <typename T> constexpr T modpow(T a, ll n) { //(a^n)%MOD
    T ret = 1;
    while (n > 0) {
        if ((n & 1) != 0) { // n%2==1
            ret = ret * a % mod;
        }
        a = a * a % mod;
        n = n / 2;
    }
    return ret;
}

ll dob(ll x, ll a, ll b, ll n) {
    while (n > 0) {
        if (n % 2 == 1) {
            n--;
            x = x * a % mod + b;
            x %= mod;
        } else {
            b = a * b % mod + b;
            b %= mod;
            a = (a * a) % mod;
            n /= 2;
        }
    }
    return x;
}

ll eval(string s, mcl &m) {
    ll ret = 0;
    int i = 0;
    while (i < s.size()) {
        ll d = 1;
        if (s[i] == '-') {
            ++i;
            d = mod - 1;
        }
        if (s[i] == '+') ++i;
        if (isalpha(s[i])) {
            ret += (m[s[i]] * d) % mod;
            if (ret < 0) ret += mod;
            ret %= mod;
            ++i;
        } else {
            ll x = 0;
            while (isdigit(s[i])) {
                x *= 10;
                x += s[i] - '0';
                x %= mod;
                ++i;
            }
            x %= mod;
            ret += (x * d) % mod;
            if (ret < 0) ret += mod;
            ret %= mod;
        }
    }
    return ret;
}

void freval(ll n, string s, mcl &m, vector<pll> &so) {
    if (s[1] == 'o') { // for
        int i = 0;
        while (s[i] != ')')
            ++i;
        ll x = eval(s.substr(4, i - 4), m);
        freval(n * x, s.substr(i + 1, s.size() - i), m, so);
        return;
    }

    if (s[1] == '<') { // 代入
        char c = s[0];
        ll a = 0, b = 0;
        string t = "";
        int i = 3;
        if (s[3] == c) {
            ++a;
            ++i;
        }
        for (; i < s.size(); ++i) {
            if (s[i] == '+' || s[i] == '-') {
                if (s[i + 1] == c) {
                    if (s[i] == '+')
                        ++a;
                    else
                        --a;
                    i += 1;
                    continue;
                }
            }
            if (i < s.size()) t.push_back(s[i]);
        }
        b = eval(t, m);
        if (a < 0) a += mod;
        m[c] = dob(m[c], a, b, n);

        return;
    }
    if (s[1] == 'r') { // print
        if (n != 0) so.emplace_back(n, eval(s.substr(6, s.size() - 7), m));
        return;
    }
}

void parse(string s, mcl &m, vector<pll> &so) {
    if (s.size() == 1) { // 宣言
        m[s[0]] = 0;
        return;
    }

    if (s[1] == '<') { // 代入
        m[s[0]] = eval(s.substr(3, s.size() - 3), m);
        return;
    }

    if (s[1] == 'r') { // print
        // 単体のprint
        so.emplace_back(1, eval(s.substr(6, s.size() - 7), m));
        return;
    }

    if (s[1] == 'o') { // for
        freval(1, s, m, so);
        return;
    }
}

bool solve() {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) return false;
    vector<pll> so(0), to(0);
    mcl var;
    for (int i = 0; i < n; ++i) {
        string s;
        cin >> s;
        parse(s, var, so);
    }
    // for(auto p:so)cerr<<p.first<<" "<<p.second<<endl;
    for (int i = 0; i < m; ++i) {
        string t;
        cin >> t;
        parse(t, var, to);
    }
    int ti = 0;
    for (auto p : so) {
        ll n = p.first, v = p.second;
        while (n > 0) {
            while (ti < to.size() && to[ti].first == 0)
                ++ti;
            if (ti >= to.size()) {
                cout << "No" << endl;
                return true;
            }
            if (to[ti].second % mod != v % mod) {
                cout << "No" << endl;
                return true;
            }

            if (n < to[ti].first) {
                to[ti].first -= n;
                n = 0;
            } else {
                n -= to[ti].first;
                ++ti;
            }
        }
    }
    if (ti == to.size()) {
        cout << "Yes" << endl;
    } else
        cout << "No" << endl;
    return true;
}

int main() {
    while (solve())
        ;

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3576
final_rank: 2

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <queue>
#include <ciso646>
#include <random>
#include <map>
#include <set>
#include <bitset>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <cassert>
#include <complex>
#include <numeric>
#include <array>
#include <chrono>
using namespace std;

#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
// constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<long long, long long> P;

#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define all(v) (v).begin(), (v).end()
typedef pair<ll, ll> LP;

template <typename T>
void chmin(T &a, T b)
{
  a = min(a, b);
}
template <typename T>
void chmax(T &a, T b)
{
  a = max(a, b);
}
template <typename T>
void cinarray(vector<T> &v)
{
  rep(i, v.size()) cin >> v[i];
}
template <typename T>
void coutarray(vector<T> &v)
{
  rep(i, v.size())
  {
    if (i > 0)
      cout << " ";
    cout << v[i];
  }
  cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod)
{
  if (n < 0)
  {
    ll res = mod_pow(x, -n, m);
    return mod_pow(res, m - 2, m);
  }
  if (abs(x) >= m)
    x %= m;
  if (x < 0)
    x += m;
  // if (x == 0)return 0;
  ll res = 1;
  while (n)
  {
    if (n & 1)
      res = res * x % m;
    x = x * x % m;
    n >>= 1;
  }
  return res;
}
struct modint
{
  int n;
  modint() : n(0) { ; }
  modint(ll m)
  {
    if (m < 0 || mod <= m)
    {
      m %= mod;
      if (m < 0)
        m += mod;
    }
    n = m;
  }
  operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint &a, modint b)
{
  a.n += b.n;
  if (a.n >= mod)
    a.n -= mod;
  return a;
}
modint operator-=(modint &a, modint b)
{
  a.n -= b.n;
  if (a.n < 0)
    a.n += mod;
  return a;
}
modint operator*=(modint &a, modint b)
{
  a.n = ((ll)a.n * b.n) % mod;
  return a;
}
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n)
{
  if (n == 0)
    return modint(1);
  modint res = (a * a) ^ (n / 2);
  if (n % 2)
    res = res * a;
  return res;
}

ll inv(ll a, ll p)
{
  return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint &a, modint b)
{
  a = a / b;
  return a;
}
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f()
{
  fact[0] = modint(1);
  for (int i = 0; i < max_n - 1; i++)
  {
    fact[i + 1] = fact[i] * modint(i + 1);
  }
  factinv[max_n - 1] = modint(1) / fact[max_n - 1];
  for (int i = max_n - 2; i >= 0; i--)
  {
    factinv[i] = factinv[i + 1] * modint(i + 1);
  }
}
modint comb(int a, int b)
{
  if (a < 0 || b < 0 || a < b)
    return 0;
  return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b)
{
  if (a < 0 || b < 0 || a < b)
    return 0;
  return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b)
{
  a = abs(a);
  b = abs(b);
  if (a < b)
    swap(a, b);
  while (b)
  {
    ll r = a % b;
    a = b;
    b = r;
  }
  return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template <typename T>
void addv(vector<T> &v, int loc, T val)
{
  if (loc >= v.size())
    v.resize(loc + 1, 0);
  v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
  fill(isp + 2, isp + mn, true);
  for (int i = 2; i < mn; i++) {
    if (!isp[i])continue;
    ps.push_back(i);
    for (int j = 2 * i; j < mn; j += i) {
      isp[j] = false;
    }
  }
}*/

//[,val)
template <typename T>
auto prev_itr(set<T> &st, T val)
{
  auto res = st.lower_bound(val);
  if (res == st.begin())
    return st.end();
  res--;
  return res;
}

//[val,)
template <typename T>
auto next_itr(set<T> &st, T val)
{
  auto res = st.lower_bound(val);
  return res;
}
using mP = pair<modint, modint>;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
//-----------------------------------------

typedef vector<vector<modint>> mat;
typedef vector<modint> vec;
mat mtmul(mat &A, mat &B)
{
  mat C(A.size(), vec(B[0].size()));
  rep(i, (int)A.size())
  {
    rep(k, (int)B.size())
    {
      rep(j, (int)B[0].size())
      {
        C[i][j] += A[i][k] * B[k][j];
      }
    }
  }
  return C;
}
mat mtpow(mat A, ll n)
{
  mat B(A.size(), vec(A.size()));
  rep(i, (int)A.size())
  {
    B[i][i] = 1;
  }
  while (n > 0)
  {
    if (n & (ll)1)
      B = mtmul(B, A);
    A = mtmul(A, A);
    n >>= 1;
  }
  return B;
}

int n, m;

modint val[27];

using ar = array<modint, 27>;

ar term(string &s, int &i)
{
  ar res;
  rep(j, 27) res[j] = 0;
  int coef = 1;
  while (s[i] == '-')
  {
    coef *= -1;
    i++;
  }
  if ('0' <= s[i] && s[i] <= '9')
  {
    modint cur = 0;
    while (i < s.size() && isdigit(s[i]))
    {
      cur = (modint)10 * cur + (modint)(s[i] - '0');
      i++;
    }
    res[26] = cur;
  }
  else
  {
    res[s[i] - 'a'] = 1;
    i++;
  }
  rep(j, 27) res[j] *= coef;
  return res;
}
ar expr(string &s, int &i)
{
  ar res = term(s, i);
  while (i < s.size() && (s[i] == '+' || s[i] == '-'))
  {
    int coef = 1;
    if (s[i] == '-')
    {
      coef = -1;
    }
    i++;
    ar val = term(s, i);
    rep(j, 27) res[j] += val[j] * (modint)coef;
  }
  return res;
}
modint trans(ar a)
{
  modint res = a[26];
  rep(i, 26) res += a[i] * (modint)val[i];
  return res;
}

vector<P> calc(vector<string> vs)
{
  vector<P> res;
  rep(i, 26) val[i] = 0;
  for (auto s : vs)
  {
    if (s.size() == 1)
    {
      // declare
      val[s[0] - 'a'] = 0;
      continue;
    }

    // for
    int pro = 1;
    int loc = 0;
    while (true)
    {
      if (loc + 1 < s.size() && s[loc] == 'f' && s[loc + 1] == 'o')
      {
        loc += 4;

        int ri = loc;
        while (s[ri] != ')')
          ri++;
        string u = s.substr(loc, ri - loc);
        int i = 0;
        pro *= trans(expr(u, i));

        loc = ri;
        loc++;
      }
      else
        break;
    }

    if (pro == 0)
      continue;

    if (s[loc] == 'p' && s[loc + 1] == 'r')
    {
      // print
      loc += 6;
      string u = s.substr(loc, s.size() - 1 - loc);
      int i = 0;
      int v = trans(expr(u, i));
      res.push_back({v, pro});
    }
    else
    {
      // assign
      int id = s[loc] - 'a';
      loc += 3;
      
      string u = s.substr(loc, s.size() - loc);
      int i = 0;
      auto a = expr(u, i);

      modint sum = 0;
      rep(j, 27) if (j != id) sum += a[j] * val[j];
      {
        mat A = {{a[id], sum}, {0, 1}};
        A = mtpow(A, pro);
        val[id] = A[0][0] * (modint)val[id] + A[0][1];
      }
    }
  }
  return res;
}
vector<LP> compress(vector<LP> v)
{
  vector<LP> res;
  rep(i, v.size())
  {
    if (res.empty() or res.back().first != v[i].first)
    {
      res.push_back({v[i].first, 0});
    }
    res.back().second += v[i].second;
  }
  return res;
}

void solve()
{
  val[26] = 1;
  vector<string> vs(n), vt(m);
  rep(i, n)
  {
    cin >> vs[i];
  }
  rep(i, m)
  {
    cin >> vt[i];
  }
  vector<LP> cs = calc(vs), ct = calc(vt);
  if (compress(cs) == compress(ct))
  {
    cout << "Yes\n";
  }
  else
  {
    cout << "No\n";
  }
}

signed main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  while (cin >> n >> m, n)
    solve();
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 19928
final_rank: 4

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
//constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

template<typename T>
void chmin(T& a, T b) {
	a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
	a = max(a, b);
}
template<typename T>
void cinarray(vector<T>& v) {
	rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
	rep(i, v.size()) {
		if (i > 0)cout << " "; cout << v[i];
	}
	cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	//if (x == 0)return 0;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	int n;
	modint() :n(0) { ; }
	modint(ll m) {
		if (m < 0 || mod <= m) {
			m %= mod; if (m < 0)m += mod;
		}
		n = m;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template<typename T>
void addv(vector<T>& v, int loc, T val) {
	if (loc >= v.size())v.resize(loc + 1, 0);
	v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
	fill(isp + 2, isp + mn, true);
	for (int i = 2; i < mn; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j < mn; j += i) {
			isp[j] = false;
		}
	}
}*/

//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	if (res == st.begin())return st.end();
	res--; return res;
}

//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	return res;
}
using mP = pair<modint, modint>;


int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
//-----------------------------------------

typedef vector<vector<modint>> mat;
typedef vector<modint> vec;
mat mtmul(mat& A, mat& B) {
	mat C(A.size(), vec(B[0].size()));
	rep(i, (int)A.size()) {
		rep(k, (int)B.size()) {
			rep(j, (int)B[0].size()) {
				C[i][j] += A[i][k] * B[k][j];
			}
		}
	}
	return C;
}
mat mtpow(mat A, ll n) {
	mat B(A.size(), vec(A.size()));
	rep(i, (int)A.size()) {
		B[i][i] = 1;
	}
	while (n > 0) {
		if (n & (ll)1)B = mtmul(B, A);
		A = mtmul(A, A);
		n >>= 1;
	}
	return B;
}

int n, m;


using ar = array<modint, 27>;

ar term(string& s, int& i) {
	ar res;
	rep(j, 27)res[j] = 0;
	int coef = 1;
	if (i > 0)assert(s[i] != '-');
	if(s[i] == '-') {
		coef *= -1;
		i++;
	}
	if ('0' <= s[i] && s[i] <= '9') {
		modint cur = 0;
		while (i < s.size() && '0' <= s[i] && s[i] <= '9') {
			cur = (modint)10 * cur + (modint)(s[i] - '0');
			i++;
		}
		res[26] = cur;
	}
	else {
		assert('a' <= s[i] && s[i] <= 'z');
		res[s[i] - 'a'] = 1;
		i++;
	}
	rep(j, 27)res[j] *= coef;
	return res;
}

ar expr(string& s, int& i) {
	//ar res;
	ar res = term(s, i);
	while (i < s.size() && (s[i] == '+' || s[i] == '-')) {
		int coef = 1;
		if (s[i] == '-') {
			coef = -1;
		}
		i++;
		ar a = term(s, i);
		rep(j, 27)res[j] += a[j] * (modint)coef;
	}
	assert(i == s.size());
	return res;
}



modint val[26];
modint trans(ar a) {
	modint res = a[26];
	rep(i, 26)res += a[i] * (modint)val[i];
	return res;
}

vector<P> calc(vector<string> vs) {
	vector<P> res;
	rep(i, 26)val[i] = 0;
	for (auto s : vs) {
		if (s.size() == 1) {
			//declear
			int id = s[0] - 'a';
			assert(0 <= id && id < 26);
			val[id] = 0;
		}
		else if (s.size() >= 2 && s[0] == 'p' && s[1] == 'r') {
			assert(s.substr(0, 5) == "print");
			//print
			string u = s.substr(6, s.size() - 6 - 1);
			int i = 0;
			int v = trans(expr(u, i));
			res.push_back({ v,1 });
			assert(i == u.size());
		}
		else if (s.size() >= 2 && s[0] == 'f' && s[1] == 'o') {
			assert(s.substr(0, 3) == "for");
			//for
			int pro = 1;
			int loc = 0;
			while (true) {
				if (loc + 1 < s.size() && s[loc] == 'f' && s[loc + 1] == 'o') {
					assert(s.substr(loc, 3) == "for");
					//for(
					loc += 4;

					int ri = loc;
					while (s[ri] != ')')ri++;
					assert(s[ri] == ')');
					string u = s.substr(loc, ri - loc);
					int i = 0;
					pro *= trans(expr(u, i));
					assert(i == u.size());

					loc = ri;
					//)
					loc++;
				}
				else break;
			}
			assert(0 <= pro && pro <= 1000000000);
			if (pro > 0) {
				if (s[loc] == 'p' && s[loc + 1] == 'r') {
					assert(s.substr(loc, 5) == "print");
					//print
					loc += 6;
					string u = s.substr(loc, s.size() - 1 - loc);
					int i = 0;
					int v = trans(expr(u, i));
					res.push_back({ v,pro });
					assert(i == u.size());
				}
				else {
					//assign
					int id = s[loc] - 'a';
					assert(0 <= id && id < 26);
					assert(s[loc + 1] == '<' && s[loc + 2] == '-');
					loc += 3;
					string u = s.substr(loc, s.size() - loc);
					int i = 0;
					auto a = expr(u, i);
					assert(i == u.size());
					modint sum = 0;
					rep(j, 27)if (j != id) {
						if (j < 26)sum += val[j] * a[j];
						else sum += a[j];
					}
					//cout << "e? " << a[0] << "\n";
					//cout << "?? " << u << "\n";

					{
						mat A = { {a[id],sum},{0,1} };
						A = mtpow(A, pro);
						val[id] = A[0][0] * (modint)val[id] + A[0][1];
					}
					/*{
						if (a[id] == (modint)1) {
							val[id] += (modint)pro * sum;
						}
						else {
							modint p = a[id];
							modint q = sum;
							modint x = mod_pow(p, pro) - 1;
							x /= (p - (modint)1);
							x *= q;
							x += val[id] * (modint)mod_pow(p, pro);
							val[id] = x;
						}
					}*/
				}
			}
		}
		else {
			//assign
			int id = s[0] - 'a';
			assert(0 <= id && id < 26);
			assert(s[1] == '<');
			assert(s[2] == '-');
			string u = s.substr(3, s.size() - 3);
			int i = 0;
			int x = trans(expr(u, i));
			val[id] = x;
			assert(i == u.size());
		}
	}
	return res;
}
vector<LP> ttt(vector<P> v) {
	vector<LP> res;
	rep(i, v.size()) {
		ll sum = v[i].second;
		while (i + 1 < v.size() && v[i].first == v[i + 1].first) {
			i++; sum += v[i].second;
		}
		res.push_back({ v[i].first,sum });
	}
	return res;
}
void solve() {
	vector<string> vs(n), vt(m);
	rep(i, n)cin >> vs[i];
	rep(i, m)cin >> vt[i];
	vector<P> cs = calc(vs), ct = calc(vt);
	auto s = ttt(cs), t = ttt(ct);
	//cout << "hello s\n";
	//rep(i, s.size())cout << s[i].first << " " << s[i].second << "\n";
	//cout << "hello t\n";
	//rep(i, t.size())cout << t[i].first << " " << t[i].second << "\n";
	if (s == t) {
		cout << "Yes\n";
	}
	else {
		cout << "No\n";
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//init_f();
	//init();
	//while(true)
	//expr();
	//int t; cin >> t; rep(i, t)
	while (cin >> n >> m, n)
		solve();
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 11740
final_rank: 3

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <queue>
#include <ciso646>
#include <random>
#include <map>
#include <set>
#include <bitset>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <cassert>
#include <complex>
#include <numeric>
#include <array>
#include <chrono>
using namespace std;

#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
// constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<long long, long long> P;

#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define all(v) (v).begin(), (v).end()
typedef pair<ll, ll> LP;

template <typename T>
void chmin(T &a, T b)
{
  a = min(a, b);
}
template <typename T>
void chmax(T &a, T b)
{
  a = max(a, b);
}
template <typename T>
void cinarray(vector<T> &v)
{
  rep(i, v.size()) cin >> v[i];
}
template <typename T>
void coutarray(vector<T> &v)
{
  rep(i, v.size())
  {
    if (i > 0)
      cout << " ";
    cout << v[i];
  }
  cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod)
{
  if (n < 0)
  {
    ll res = mod_pow(x, -n, m);
    return mod_pow(res, m - 2, m);
  }
  if (abs(x) >= m)
    x %= m;
  if (x < 0)
    x += m;
  // if (x == 0)return 0;
  ll res = 1;
  while (n)
  {
    if (n & 1)
      res = res * x % m;
    x = x * x % m;
    n >>= 1;
  }
  return res;
}
struct modint
{
  int n;
  modint() : n(0) { ; }
  modint(ll m)
  {
    if (m < 0 || mod <= m)
    {
      m %= mod;
      if (m < 0)
        m += mod;
    }
    n = m;
  }
  operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint &a, modint b)
{
  a.n += b.n;
  if (a.n >= mod)
    a.n -= mod;
  return a;
}
modint operator-=(modint &a, modint b)
{
  a.n -= b.n;
  if (a.n < 0)
    a.n += mod;
  return a;
}
modint operator*=(modint &a, modint b)
{
  a.n = ((ll)a.n * b.n) % mod;
  return a;
}
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n)
{
  if (n == 0)
    return modint(1);
  modint res = (a * a) ^ (n / 2);
  if (n % 2)
    res = res * a;
  return res;
}

ll inv(ll a, ll p)
{
  return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint &a, modint b)
{
  a = a / b;
  return a;
}
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f()
{
  fact[0] = modint(1);
  for (int i = 0; i < max_n - 1; i++)
  {
    fact[i + 1] = fact[i] * modint(i + 1);
  }
  factinv[max_n - 1] = modint(1) / fact[max_n - 1];
  for (int i = max_n - 2; i >= 0; i--)
  {
    factinv[i] = factinv[i + 1] * modint(i + 1);
  }
}
modint comb(int a, int b)
{
  if (a < 0 || b < 0 || a < b)
    return 0;
  return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b)
{
  if (a < 0 || b < 0 || a < b)
    return 0;
  return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b)
{
  a = abs(a);
  b = abs(b);
  if (a < b)
    swap(a, b);
  while (b)
  {
    ll r = a % b;
    a = b;
    b = r;
  }
  return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template <typename T>
void addv(vector<T> &v, int loc, T val)
{
  if (loc >= v.size())
    v.resize(loc + 1, 0);
  v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
  fill(isp + 2, isp + mn, true);
  for (int i = 2; i < mn; i++) {
    if (!isp[i])continue;
    ps.push_back(i);
    for (int j = 2 * i; j < mn; j += i) {
      isp[j] = false;
    }
  }
}*/

//[,val)
template <typename T>
auto prev_itr(set<T> &st, T val)
{
  auto res = st.lower_bound(val);
  if (res == st.begin())
    return st.end();
  res--;
  return res;
}

//[val,)
template <typename T>
auto next_itr(set<T> &st, T val)
{
  auto res = st.lower_bound(val);
  return res;
}
using mP = pair<modint, modint>;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
//-----------------------------------------

typedef vector<vector<modint>> mat;
typedef vector<modint> vec;
mat mtmul(mat &A, mat &B)
{
  mat C(A.size(), vec(B[0].size()));
  rep(i, (int)A.size())
  {
    rep(k, (int)B.size())
    {
      rep(j, (int)B[0].size())
      {
        C[i][j] += A[i][k] * B[k][j];
      }
    }
  }
  return C;
}
mat mtpow(mat A, ll n)
{
  mat B(A.size(), vec(A.size()));
  rep(i, (int)A.size())
  {
    B[i][i] = 1;
  }
  while (n > 0)
  {
    if (n & (ll)1)
      B = mtmul(B, A);
    A = mtmul(A, A);
    n >>= 1;
  }
  return B;
}

int n, m;

modint val[26];

using ar = array<modint, 27>;

ar term(string &s, int &i)
{
  ar res;
  rep(j, 27) res[j] = 0;
  int coef = 1;
  while (s[i] == '-')
  {
    coef *= -1;
    i++;
  }
  if ('0' <= s[i] && s[i] <= '9')
  {
    modint cur = 0;
    while (i < s.size() && '0' <= s[i] && s[i] <= '9')
    {
      cur = (modint)10 * cur + (modint)(s[i] - '0');
      i++;
    }
    res[26] = cur;
  }
  else
  {
    res[s[i] - 'a'] = 1;
    i++;
  }
  rep(j, 27) res[j] *= coef;
  return res;
}
ar expr(string &s, int &i)
{
  ar res = term(s, i);
  while (i < s.size() && (s[i] == '+' || s[i] == '-'))
  {
    int coef = 1;
    if (s[i] == '-')
    // cout << "fin " << s << "\n";
    {
      coef = -1;
    }
    i++;
    ar val = term(s, i);
    rep(j, 27) res[j] += val[j] * (modint)coef;
  }
  return res;
}
modint trans(ar a)
{
  modint res = a[26];
  rep(i, 26) res += a[i] * (modint)val[i];
  return res;
}

vector<P> calc(vector<string> vs)
{
  vector<P> res;
  rep(i, 26) val[i] = 0;
  for (auto s : vs)
  {
    if (s.size() == 1)
    {
      // declare
      val[s[0] - 'a'] = 0;
      continue;
    }

    // for
    int pro = 1;
    int loc = 0;
    while (true)
    {
      if (loc + 1 < s.size() && s[loc] == 'f' && s[loc + 1] == 'o')
      {
        loc += 4;

        int ri = loc;
        while (s[ri] != ')')
          ri++;
        string u = s.substr(loc, ri - loc);
        int i = 0;
        pro *= trans(expr(u, i));

        loc = ri;
        loc++;
      }
      else
        break;
    }
    if (pro == 0)
      continue;

    if (s[loc] == 'p' && s[loc + 1] == 'r')
    {
      // print
      loc += 6;
      string u = s.substr(loc, s.size() - 1 - loc);
      int i = 0;
      int v = trans(expr(u, i));
      res.push_back({v, pro});
      assert(i == u.size());
    }
    else
    {
      // assign
      int id = s[loc] - 'a';
      assert(0 <= id && id < 26);
      assert(s[loc + 1] == '<' && s[loc + 2] == '-');
      loc += 3;
      string u = s.substr(loc, s.size() - loc);
      int i = 0;
      auto a = expr(u, i);
      assert(i == u.size());
      modint sum = 0;
      rep(j, 27) if (j != id) sum += a[j] * val[j];
      {
        mat A = {{a[id], sum}, {0, 1}};
        A = mtpow(A, pro);
        val[id] = A[0][0] * (modint)val[id] + A[0][1];
      }
    }
  }
  return res;
}
vector<LP> compress(vector<P> v)
{
  vector<LP> res;
  rep(i, v.size())
  {
    if (res.empty() or res.back().first != v[i].first)
    {
      res.push_back({v[i].first, 0});
    }
    res.back().second += v[i].second;
  }
  return res;
}
void solve()
{
  vector<string> vs(n), vt(m);
  rep(i, n)
  {
    cin >> vs[i];
  }
  rep(i, m)
  {
    cin >> vt[i];
  }
  vector<P> cs = calc(vs), ct = calc(vt);
  if (compress(cs) == compress(ct))
  {
    cout << "Yes\n";
  }
  else
  {
    cout << "No\n";
  }
}

signed main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  while (cin >> n >> m, n)
    solve();
  return 0;
}
accuracy: 0.02631578947368421
time_ms: 10
memory_kb: 19920
final_rank: 19

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <queue>
#include <ciso646>
#include <random>
#include <map>
#include <set>
#include <bitset>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <cassert>
#include <complex>
#include <numeric>
#include <array>
#include <chrono>
using namespace std;

#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
// constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<long long, long long> P;

#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define all(v) (v).begin(), (v).end()
typedef pair<ll, ll> LP;

template <typename T>
void chmin(T &a, T b)
{
  a = min(a, b);
}
template <typename T>
void chmax(T &a, T b)
{
  a = max(a, b);
}
template <typename T>
void cinarray(vector<T> &v)
{
  rep(i, v.size()) cin >> v[i];
}
template <typename T>
void coutarray(vector<T> &v)
{
  rep(i, v.size())
  {
    if (i > 0)
      cout << " ";
    cout << v[i];
  }
  cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod)
{
  if (n < 0)
  {
    ll res = mod_pow(x, -n, m);
    return mod_pow(res, m - 2, m);
  }
  if (abs(x) >= m)
    x %= m;
  if (x < 0)
    x += m;
  // if (x == 0)return 0;
  ll res = 1;
  while (n)
  {
    if (n & 1)
      res = res * x % m;
    x = x * x % m;
    n >>= 1;
  }
  return res;
}
struct modint
{
  int n;
  modint() : n(0) { ; }
  modint(ll m)
  {
    if (m < 0 || mod <= m)
    {
      m %= mod;
      if (m < 0)
        m += mod;
    }
    n = m;
  }
  operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint &a, modint b)
{
  a.n += b.n;
  if (a.n >= mod)
    a.n -= mod;
  return a;
}
modint operator-=(modint &a, modint b)
{
  a.n -= b.n;
  if (a.n < 0)
    a.n += mod;
  return a;
}
modint operator*=(modint &a, modint b)
{
  a.n = ((ll)a.n * b.n) % mod;
  return a;
}
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n)
{
  if (n == 0)
    return modint(1);
  modint res = (a * a) ^ (n / 2);
  if (n % 2)
    res = res * a;
  return res;
}

ll inv(ll a, ll p)
{
  return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint &a, modint b)
{
  a = a / b;
  return a;
}
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f()
{
  fact[0] = modint(1);
  for (int i = 0; i < max_n - 1; i++)
  {
    fact[i + 1] = fact[i] * modint(i + 1);
  }
  factinv[max_n - 1] = modint(1) / fact[max_n - 1];
  for (int i = max_n - 2; i >= 0; i--)
  {
    factinv[i] = factinv[i + 1] * modint(i + 1);
  }
}
modint comb(int a, int b)
{
  if (a < 0 || b < 0 || a < b)
    return 0;
  return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b)
{
  if (a < 0 || b < 0 || a < b)
    return 0;
  return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b)
{
  a = abs(a);
  b = abs(b);
  if (a < b)
    swap(a, b);
  while (b)
  {
    ll r = a % b;
    a = b;
    b = r;
  }
  return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template <typename T>
void addv(vector<T> &v, int loc, T val)
{
  if (loc >= v.size())
    v.resize(loc + 1, 0);
  v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
  fill(isp + 2, isp + mn, true);
  for (int i = 2; i < mn; i++) {
    if (!isp[i])continue;
    ps.push_back(i);
    for (int j = 2 * i; j < mn; j += i) {
      isp[j] = false;
    }
  }
}*/

//[,val)
template <typename T>
auto prev_itr(set<T> &st, T val)
{
  auto res = st.lower_bound(val);
  if (res == st.begin())
    return st.end();
  res--;
  return res;
}

//[val,)
template <typename T>
auto next_itr(set<T> &st, T val)
{
  auto res = st.lower_bound(val);
  return res;
}
using mP = pair<modint, modint>;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
//-----------------------------------------

typedef vector<vector<modint>> mat;
typedef vector<modint> vec;
mat mtmul(mat &A, mat &B)
{
  mat C(A.size(), vec(B[0].size()));
  rep(i, (int)A.size())
  {
    rep(k, (int)B.size())
    {
      rep(j, (int)B[0].size())
      {
        C[i][j] += A[i][k] * B[k][j];
      }
    }
  }
  return C;
}
mat mtpow(mat A, ll n)
{
  mat B(A.size(), vec(A.size()));
  rep(i, (int)A.size())
  {
    B[i][i] = 1;
  }
  while (n > 0)
  {
    if (n & (ll)1)
      B = mtmul(B, A);
    A = mtmul(A, A);
    n >>= 1;
  }
  return B;
}

int n, m;

modint val[26];

using ar = array<modint, 27>;

ar term(string &s, int &i)
{
  ar res;
  rep(j, 27) res[j] = 0;
  int coef = 1;
  while (s[i] == '-')
  {
    coef *= -1;
    i++;
  }
  if ('0' <= s[i] && s[i] <= '9')
  {
    modint cur = 0;
    while (i < s.size() && '0' <= s[i] && s[i] <= '9')
    {
      cur = (modint)10 * cur + (modint)(s[i] - '0');
      i++;
    }
    res[26] = cur;
  }
  else
  {
    res[s[i] - 'a'] = 1;
    i++;
  }
  rep(j, 27) res[j] *= coef;
  return res;
}
ar expr(string &s, int &i)
{
  ar res = term(s, i);
  while (i < s.size() && (s[i] == '+' || s[i] == '-'))
  {
    int coef = 1;
    if (s[i] == '-')
    // cout << "fin " << s << "\n";
    {
      coef = -1;
    }
    i++;
    ar val = term(s, i);
    rep(j, 27) res[j] += val[j] * (modint)coef;
  }
  return res;
}
modint trans(ar a)
{
  modint res = a[26];
  rep(i, 26) res += a[i] * (modint)val[i];
  return res;
}

vector<P> calc(vector<string> vs)
{
  vector<P> res;
  rep(i, 26) val[i] = 0;
  for (auto s : vs)
  {
    if (s.size() == 1)
    {
      // declare
      val[s[0] - 'a'] = 0;
      continue;
    }

    // for
    int pro = 1;
    int loc = 0;
    while (true)
    {
      if (loc + 1 < s.size() && s[loc] == 'f' && s[loc + 1] == 'o')
      {
        loc += 4;

        int ri = loc;
        while (s[ri] != ')')
          ri++;
        string u = s.substr(loc, ri - loc);
        int i = 0;
        pro *= trans(expr(u, i));

        loc = ri;
        loc++;
      }
      else
        break;
    }
    if (pro > 0)
    {
      if (s[loc] == 'p' && s[loc + 1] == 'r')
      {
        // print
        loc += 6;
        string u = s.substr(loc, s.size() - 1 - loc);
        int i = 0;
        int v = trans(expr(u, i));
        res.push_back({v, pro});
        assert(i == u.size());
      }
      else
      {
        // assign
        int id = s[loc] - 'a';
        assert(0 <= id && id < 26);
        assert(s[loc + 1] == '<' && s[loc + 2] == '-');
        loc += 3;
        string u = s.substr(loc, s.size() - loc);
        int i = 0;
        auto a = expr(u, i);
        assert(i == u.size());
        modint sum = 0;
        rep(j, 27) if (j != id) sum += a[j] * val[j];
        {
          mat A = {{a[id], sum}, {0, 1}};
          A = mtpow(A, pro);
          val[id] = A[0][0] * (modint)val[id] + A[0][1];
        }
      }
    }
  }
  return res;
}
vector<LP> ttt(vector<P> v)
{
  vector<LP> res;
  rep(i, v.size())
  {
    ll sum = v[i].second;
    while (i + 1 < v.size() && v[i].first == v[i + 1].first)
    {
      i++;
      sum += v[i].second;
    }
    res.push_back({v[i].first, sum});
  }
  return res;
}
void solve()
{
  vector<string> vs(n), vt(m);
  rep(i, n)
  {
    cin >> vs[i];
  }
  rep(i, m)
  {
    cin >> vt[i];
  }
  vector<P> cs = calc(vs), ct = calc(vt);
  auto s = ttt(cs), t = ttt(ct);
  // cout << "hello s\n";
  // rep(i, s.size())cout << s[i].first << " " << s[i].second << "\n";
  // cout << "hello t\n";
  // rep(i, t.size())cout << t[i].first << " " << t[i].second << "\n";
  if (s == t)
  {
    cout << "Yes\n";
  }
  else
  {
    cout << "No\n";
  }
}

signed main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout << fixed << setprecision(10);
  // init_f();
  // init();
  // while(true)
  // expr();
  // int t; cin >> t; rep(i, t)
  while (cin >> n >> m, n)
    solve();
  return 0;
}
accuracy: 0.02631578947368421
time_ms: 10
memory_kb: 19924
final_rank: 20

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <queue>
#include <ciso646>
#include <random>
#include <map>
#include <set>
#include <bitset>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <cassert>
#include <complex>
#include <numeric>
#include <array>
#include <chrono>
using namespace std;

#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
// constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<long long, long long> P;

#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define all(v) (v).begin(), (v).end()
typedef pair<ll, ll> LP;

template <typename T>
void chmin(T &a, T b)
{
  a = min(a, b);
}
template <typename T>
void chmax(T &a, T b)
{
  a = max(a, b);
}
template <typename T>
void cinarray(vector<T> &v)
{
  rep(i, v.size()) cin >> v[i];
}
template <typename T>
void coutarray(vector<T> &v)
{
  rep(i, v.size())
  {
    if (i > 0)
      cout << " ";
    cout << v[i];
  }
  cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod)
{
  if (n < 0)
  {
    ll res = mod_pow(x, -n, m);
    return mod_pow(res, m - 2, m);
  }
  if (abs(x) >= m)
    x %= m;
  if (x < 0)
    x += m;
  // if (x == 0)return 0;
  ll res = 1;
  while (n)
  {
    if (n & 1)
      res = res * x % m;
    x = x * x % m;
    n >>= 1;
  }
  return res;
}
struct modint
{
  int n;
  modint() : n(0) { ; }
  modint(ll m)
  {
    if (m < 0 || mod <= m)
    {
      m %= mod;
      if (m < 0)
        m += mod;
    }
    n = m;
  }
  operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint &a, modint b)
{
  a.n += b.n;
  if (a.n >= mod)
    a.n -= mod;
  return a;
}
modint operator-=(modint &a, modint b)
{
  a.n -= b.n;
  if (a.n < 0)
    a.n += mod;
  return a;
}
modint operator*=(modint &a, modint b)
{
  a.n = ((ll)a.n * b.n) % mod;
  return a;
}
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n)
{
  if (n == 0)
    return modint(1);
  modint res = (a * a) ^ (n / 2);
  if (n % 2)
    res = res * a;
  return res;
}

ll inv(ll a, ll p)
{
  return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint &a, modint b)
{
  a = a / b;
  return a;
}
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f()
{
  fact[0] = modint(1);
  for (int i = 0; i < max_n - 1; i++)
  {
    fact[i + 1] = fact[i] * modint(i + 1);
  }
  factinv[max_n - 1] = modint(1) / fact[max_n - 1];
  for (int i = max_n - 2; i >= 0; i--)
  {
    factinv[i] = factinv[i + 1] * modint(i + 1);
  }
}
modint comb(int a, int b)
{
  if (a < 0 || b < 0 || a < b)
    return 0;
  return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b)
{
  if (a < 0 || b < 0 || a < b)
    return 0;
  return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b)
{
  a = abs(a);
  b = abs(b);
  if (a < b)
    swap(a, b);
  while (b)
  {
    ll r = a % b;
    a = b;
    b = r;
  }
  return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template <typename T>
void addv(vector<T> &v, int loc, T val)
{
  if (loc >= v.size())
    v.resize(loc + 1, 0);
  v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
  fill(isp + 2, isp + mn, true);
  for (int i = 2; i < mn; i++) {
    if (!isp[i])continue;
    ps.push_back(i);
    for (int j = 2 * i; j < mn; j += i) {
      isp[j] = false;
    }
  }
}*/

//[,val)
template <typename T>
auto prev_itr(set<T> &st, T val)
{
  auto res = st.lower_bound(val);
  if (res == st.begin())
    return st.end();
  res--;
  return res;
}

//[val,)
template <typename T>
auto next_itr(set<T> &st, T val)
{
  auto res = st.lower_bound(val);
  return res;
}
using mP = pair<modint, modint>;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
//-----------------------------------------

typedef vector<vector<modint>> mat;
typedef vector<modint> vec;
mat mtmul(mat &A, mat &B)
{
  mat C(A.size(), vec(B[0].size()));
  rep(i, (int)A.size())
  {
    rep(k, (int)B.size())
    {
      rep(j, (int)B[0].size())
      {
        C[i][j] += A[i][k] * B[k][j];
      }
    }
  }
  return C;
}
mat mtpow(mat A, ll n)
{
  mat B(A.size(), vec(A.size()));
  rep(i, (int)A.size())
  {
    B[i][i] = 1;
  }
  while (n > 0)
  {
    if (n & (ll)1)
      B = mtmul(B, A);
    A = mtmul(A, A);
    n >>= 1;
  }
  return B;
}

int n, m;

modint val[26];

using ar = array<modint, 27>;

ar term(string &s, int &i)
{
  ar res;
  rep(j, 27) res[j] = 0;
  int coef = 1;
  while (s[i] == '-')
  {
    coef *= -1;
    i++;
  }
  if ('0' <= s[i] && s[i] <= '9')
  {
    modint cur = 0;
    while (i < s.size() && '0' <= s[i] && s[i] <= '9')
    {
      cur = (modint)10 * cur + (modint)(s[i] - '0');
      i++;
    }
    res[26] = cur;
  }
  else
  {
    assert('a' <= s[i] && s[i] <= 'z');
    res[s[i] - 'a'] = 1;
    i++;
  }
  rep(j, 27) res[j] *= coef;
  return res;
}
ar expr(string &s, int &i)
{
  ar res = term(s, i);
  while (i < s.size() && (s[i] == '+' || s[i] == '-'))
  {
    int coef = 1;
    if (s[i] == '-')
    // cout << "fin " << s << "\n";
    {
      coef = -1;
    }
    i++;
    ar val = term(s, i);
    rep(j, 27) res[j] += val[j] * (modint)coef;
  }
  return res;
}
modint trans(ar a)
{
  modint res = a[26];
  rep(i, 26) res += a[i] * (modint)val[i];
  return res;
}

vector<P> calc(vector<string> vs)
{
  vector<P> res;
  rep(i, 26) val[i] = 0;
  for (auto s : vs)
  {
    if (s.size() == 1)
    {
      // declare
      val[s[0] - 'a'] = 0;
      continue;
    }
    
    // for
    int pro = 1;
    int loc = 0;
    while (true)
    {
      if (loc + 1 < s.size() && s[loc] == 'f' && s[loc + 1] == 'o')
      {
        loc += 4;

        int ri = loc;
        while (s[ri] != ')')
          ri++;
        string u = s.substr(loc, ri - loc);
        int i = 0;
        pro *= trans(expr(u, i));
        assert(i == u.size());

        loc = ri;
        loc++;
      }
      else
        break;
    }
    if (pro > 0)
    {
      if (s[loc] == 'p' && s[loc + 1] == 'r')
      {
        // print
        loc += 6;
        string u = s.substr(loc, s.size() - 1 - loc);
        int i = 0;
        int v = trans(expr(u, i));
        res.push_back({v, pro});
        assert(i == u.size());
      }
      else
      {
        // assign
        int id = s[loc] - 'a';
        assert(0 <= id && id < 26);
        assert(s[loc + 1] == '<' && s[loc + 2] == '-');
        loc += 3;
        string u = s.substr(loc, s.size() - loc);
        int i = 0;
        auto a = expr(u, i);
        assert(i == u.size());
        modint sum = 0;
        rep(j, 27) if (j != id) sum += a[j] * val[j];
        {
          mat A = {{a[id], sum}, {0, 1}};
          A = mtpow(A, pro);
          val[id] = A[0][0] * (modint)val[id] + A[0][1];
        }
      }
    }
  }
  return res;
}
vector<LP> ttt(vector<P> v)
{
  vector<LP> res;
  rep(i, v.size())
  {
    ll sum = v[i].second;
    while (i + 1 < v.size() && v[i].first == v[i + 1].first)
    {
      i++;
      sum += v[i].second;
    }
    res.push_back({v[i].first, sum});
  }
  return res;
}
void solve()
{
  vector<string> vs(n), vt(m);
  rep(i, n)
  {
    cin >> vs[i];
  }
  rep(i, m)
  {
    cin >> vt[i];
  }
  vector<P> cs = calc(vs), ct = calc(vt);
  auto s = ttt(cs), t = ttt(ct);
  // cout << "hello s\n";
  // rep(i, s.size())cout << s[i].first << " " << s[i].second << "\n";
  // cout << "hello t\n";
  // rep(i, t.size())cout << t[i].first << " " << t[i].second << "\n";
  if (s == t)
  {
    cout << "Yes\n";
  }
  else
  {
    cout << "No\n";
  }
}

signed main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout << fixed << setprecision(10);
  // init_f();
  // init();
  // while(true)
  // expr();
  // int t; cin >> t; rep(i, t)
  while (cin >> n >> m, n)
    solve();
  return 0;
}
accuracy: 0.02631578947368421
time_ms: 10
memory_kb: 19852
final_rank: 17

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
//constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

template<typename T>
void chmin(T& a, T b) {
	a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
	a = max(a, b);
}
template<typename T>
void cinarray(vector<T>& v) {
	rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
	rep(i, v.size()) {
		if (i > 0)cout << " "; cout << v[i];
	}
	cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	//if (x == 0)return 0;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	int n;
	modint() :n(0) { ; }
	modint(ll m) {
		if (m < 0 || mod <= m) {
			m %= mod; if (m < 0)m += mod;
		}
		n = m;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template<typename T>
void addv(vector<T>& v, int loc, T val) {
	if (loc >= v.size())v.resize(loc + 1, 0);
	v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
	fill(isp + 2, isp + mn, true);
	for (int i = 2; i < mn; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j < mn; j += i) {
			isp[j] = false;
		}
	}
}*/

//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	if (res == st.begin())return st.end();
	res--; return res;
}

//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	return res;
}
using mP = pair<modint, modint>;


int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
//-----------------------------------------

typedef vector<vector<modint>> mat;
typedef vector<modint> vec;
mat mtmul(mat& A, mat& B) {
	mat C(A.size(), vec(B[0].size()));
	rep(i, (int)A.size()) {
		rep(k, (int)B.size()) {
			rep(j, (int)B[0].size()) {
				C[i][j] += A[i][k] * B[k][j];
			}
		}
	}
	return C;
}
mat mtpow(mat A, ll n) {
	mat B(A.size(), vec(A.size()));
	rep(i, (int)A.size()) {
		B[i][i] = 1;
	}
	while (n > 0) {
		if (n & (ll)1)B = mtmul(B, A);
		A = mtmul(A, A);
		n >>= 1;
	}
	return B;
}

int n, m;


using ar = array<modint, 27>;

ar term(string& s, int& i) {
	ar res;
	rep(j, 27)res[j] = 0;
	int coef = 1;
	if (i > 0)assert(s[i] != '-');
	if(s[i] == '-') {
		coef *= -1;
		i++;
	}
	if ('0' <= s[i] && s[i] <= '9') {
		modint cur = 0;
		while (i < s.size() && '0' <= s[i] && s[i] <= '9') {
			cur = (modint)10 * cur + (modint)(s[i] - '0');
			i++;
		}
		res[26] = cur;
	}
	else {
		assert('a' <= s[i] && s[i] <= 'z');
		res[s[i] - 'a'] = 1;
		i++;
	}
	rep(j, 27)res[j] *= coef;
	return res;
}
ar expr(string& s, int& i) {
	//ar res;
	ar res = term(s, i);
	while (i < s.size() && (s[i] == '+' || s[i] == '-')) {
		int coef = 1;
		if (s[i] == '-') {
			coef = -1;
		}
		i++;
		ar a = term(s, i);
		rep(j, 27)res[j] += a[j] * (modint)coef;
	}
	assert(i == s.size());
	return res;
}



modint val[26];
modint trans(ar a) {
	modint res = a[26];
	rep(i, 26)res += a[i] * (modint)val[i];
	return res;
}

vector<P> calc(vector<string> vs) {
	vector<P> res;
	rep(i, 26)val[i] = 0;
	for (auto s : vs) {
		if (s.size() == 1) {
			//declear
			int id = s[0] - 'a';
			assert(0 <= id && id < 26);
			val[id] = 0;
		}
		else if (s.size() >= 2 && s[0] == 'p' && s[1] == 'r') {
			assert(s.substr(0, 5) == "print");
			//print
			string u = s.substr(6, s.size() - 6 - 1);
			int i = 0;
			int v = trans(expr(u, i));
			res.push_back({ v,1 });
			assert(i == u.size());
		}
		else if (s.size() >= 2 && s[0] == 'f' && s[1] == 'o') {
			assert(s.substr(0, 3) == "for");
			//for
			int pro = 1;
			int loc = 0;
			while (true) {
				if (loc + 1 < s.size() && s[loc] == 'f' && s[loc + 1] == 'o') {
					assert(s.substr(loc, 3) == "for");
					//for(
					loc += 4;

					int ri = loc;
					while (s[ri] != ')')ri++;
					assert(s[ri] == ')');
					string u = s.substr(loc, ri - loc);
					int i = 0;
					pro *= trans(expr(u, i));
					assert(i == u.size());

					loc = ri;
					//)
					loc++;
				}
				else break;
			}
			if (pro > 0) {
				if (s[loc] == 'p' && s[loc + 1] == 'r') {
					assert(s.substr(loc, 5) == "print");
					//print
					loc += 6;
					string u = s.substr(loc, s.size() - 1 - loc);
					int i = 0;
					int v = trans(expr(u, i));
					res.push_back({ v,pro });
					assert(i == u.size());
				}
				else {
					//assign
					int id = s[loc] - 'a';
					assert(0 <= id && id < 26);
					assert(s[loc + 1] == '<' && s[loc + 2] == '-');
					loc += 3;
					string u = s.substr(loc, s.size() - loc);
					int i = 0;
					auto a = expr(u, i);
					assert(i == u.size());
					modint sum = 0;
					rep(j, 27)if (j != id)sum += a[j];
					//cout << "e? " << a[0] << "\n";
					//cout << "?? " << u << "\n";

					{
						mat A = { {a[id],sum},{0,1} };
						A = mtpow(A, pro);
						val[id] = A[0][0] * (modint)val[id] + A[0][1];
					}
					/*{
						if (a[id] == (modint)1) {
							val[id] += (modint)pro * sum;
						}
						else {
							modint p = a[id];
							modint q = sum;
							modint x = mod_pow(p, pro) - 1;
							x /= (p - (modint)1);
							x *= q;
							x += val[id] * (modint)mod_pow(p, pro);
							val[id] = x;
						}
					}*/
				}
			}
		}
		else {
			//assign
			int id = s[0] - 'a';
			assert(0 <= id && id < 26);
			assert(s[1] == '<');
			assert(s[2] == '-');
			string u = s.substr(3, s.size() - 3);
			int i = 0;
			int x = trans(expr(u, i));
			val[id] = x;
			assert(i == u.size());
		}
		//cout << "fin " << s << "\n";
	}
	return res;
}
vector<LP> ttt(vector<P> v) {
	vector<LP> res;
	rep(i, v.size()) {
		ll sum = v[i].second;
		while (i + 1 < v.size() && v[i].first == v[i + 1].first) {
			i++; sum += v[i].second;
		}
		res.push_back({ v[i].first,sum });
	}
	return res;
}
void solve() {
	vector<string> vs(n), vt(m);
	rep(i, n)cin >> vs[i];
	rep(i, m)cin >> vt[i];
	vector<P> cs = calc(vs), ct = calc(vt);
	auto s = ttt(cs), t = ttt(ct);
	//cout << "hello s\n";
	//rep(i, s.size())cout << s[i].first << " " << s[i].second << "\n";
	//cout << "hello t\n";
	//rep(i, t.size())cout << t[i].first << " " << t[i].second << "\n";
	if (s == t) {
		cout << "Yes\n";
	}
	else {
		cout << "No\n";
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//init_f();
	//init();
	//while(true)
	//expr();
	//int t; cin >> t; rep(i, t)
	while (cin >> n >> m, n)
		solve();
	return 0;
}
accuracy: 0.02631578947368421
time_ms: 10
memory_kb: 11724
final_rank: 14

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
//constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

template<typename T>
void chmin(T& a, T b) {
	a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
	a = max(a, b);
}
template<typename T>
void cinarray(vector<T>& v) {
	rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
	rep(i, v.size()) {
		if (i > 0)cout << " "; cout << v[i];
	}
	cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	//if (x == 0)return 0;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	int n;
	modint() :n(0) { ; }
	modint(ll m) {
		if (m < 0 || mod <= m) {
			m %= mod; if (m < 0)m += mod;
		}
		n = m;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template<typename T>
void addv(vector<T>& v, int loc, T val) {
	if (loc >= v.size())v.resize(loc + 1, 0);
	v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
	fill(isp + 2, isp + mn, true);
	for (int i = 2; i < mn; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j < mn; j += i) {
			isp[j] = false;
		}
	}
}*/

//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	if (res == st.begin())return st.end();
	res--; return res;
}

//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	return res;
}
using mP = pair<modint, modint>;


int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
//-----------------------------------------

typedef vector<vector<modint>> mat;
typedef vector<modint> vec;
mat mtmul(mat& A, mat& B) {
	mat C(A.size(), vec(B[0].size()));
	rep(i, (int)A.size()) {
		rep(k, (int)B.size()) {
			rep(j, (int)B[0].size()) {
				C[i][j] += A[i][k] * B[k][j];
			}
		}
	}
	return C;
}
mat mtpow(mat A, ll n) {
	mat B(A.size(), vec(A.size()));
	rep(i, (int)A.size()) {
		B[i][i] = 1;
	}
	while (n > 0) {
		if (n & (ll)1)B = mtmul(B, A);
		A = mtmul(A, A);
		n >>= 1;
	}
	return B;
}

int n, m;


using ar = array<modint, 27>;

ar term(string& s, int& i) {
	ar res;
	rep(j, 27)res[j] = 0;
	int coef = 1;
	while(s[i] == '-') {
		coef *= -1;
		i++;
	}
	while (s[i] == '+')i++;
	if ('0' <= s[i] && s[i] <= '9') {
		modint cur = 0;
		while (i < s.size() && '0' <= s[i] && s[i] <= '9') {
			cur = (modint)10 * cur + (modint)(s[i] - '0');
			i++;
		}
		res[26] = cur;
	}
	else {
		assert('a' <= s[i] && s[i] <= 'z');
		res[s[i] - 'a'] = 1;
		i++;
	}
	rep(j, 27)res[j] *= coef;
	return res;
}
ar expr(string& s, int& i) {
	//ar res;
	ar res = term(s, i);
	while (i < s.size() && (s[i] == '+' || s[i] == '-')) {
		int coef = 1;
		if (s[i] == '-') {
			coef = -1;
		}
		i++;
		ar val = term(s, i);
		rep(j, 27)res[j] += val[j] * (modint)coef;
	}
	assert(i == s.size());
	return res;
}



modint val[26];
modint trans(ar a) {
	modint res = a[26];
	rep(i, 26)res += a[i] * (modint)val[i];
	return res;
}

vector<P> calc(vector<string> vs) {
	vector<P> res;
	rep(i, 26)val[i] = 0;
	for (auto s : vs) {
		if (s.size() == 1) {
			//declear
			int id = s[0] - 'a';
			assert(0 <= id && id < 26);
			val[id] = 0;
		}
		else if (s.size() >= 2 && s[0] == 'p' && s[1] == 'r') {
			assert(s.substr(0, 5) == "print");
			//print
			string u = s.substr(6, s.size() - 6 - 1);
			int i = 0;
			int v = trans(expr(u, i));
			res.push_back({ v,1 });
			assert(i == u.size());
		}
		else if (s.size() >= 2 && s[0] == 'f' && s[1] == 'o') {
			assert(s.substr(0, 3) == "for");
			//for
			int pro = 1;
			int loc = 0;
			while (true) {
				if (loc + 1 < s.size() && s[loc] == 'f' && s[loc + 1] == 'o') {
					assert(s.substr(loc, 3) == "for");
					//for(
					loc += 4;

					int ri = loc;
					while (s[ri] != ')')ri++;
					assert(s[ri] == ')');
					string u = s.substr(loc, ri - loc);
					int i = 0;
					pro *= trans(expr(u, i));
					assert(i == u.size());

					loc = ri;
					//)
					loc++;
				}
				else break;
			}
			if (pro > 0) {
				if (s[loc] == 'p' && s[loc + 1] == 'r') {
					assert(s.substr(loc, 5) == "print");
					//print
					loc += 6;
					string u = s.substr(loc, s.size() - 1 - loc);
					int i = 0;
					int v = trans(expr(u, i));
					res.push_back({ v,pro });
					assert(i == u.size());
				}
				else {
					//assign
					int id = s[loc] - 'a';
					assert(0 <= id && id < 26);
					assert(s[loc + 1] == '<' && s[loc + 2] == '-');
					loc += 3;
					string u = s.substr(loc, s.size() - loc);
					int i = 0;
					auto a = expr(u, i);
					assert(i == u.size());
					modint sum = 0;
					rep(j, 27)if (j != id)sum += a[j];
					//cout << "e? " << a[0] << "\n";
					//cout << "?? " << u << "\n";

					{
						mat A = { {a[id],sum},{0,1} };
						A = mtpow(A, pro);
						val[id] = A[0][0] * (modint)val[id] + A[0][1];
					}
					/*{
						if (a[id] == (modint)1) {
							val[id] += (modint)pro * sum;
						}
						else {
							modint p = a[id];
							modint q = sum;
							modint x = mod_pow(p, pro) - 1;
							x /= (p - (modint)1);
							x *= q;
							x += val[id] * (modint)mod_pow(p, pro);
							val[id] = x;
						}
					}*/
				}
			}
		}
		else {
			//assign
			int id = s[0] - 'a';
			assert(0 <= id && id < 26);
			assert(s[1] == '<');
			assert(s[2] == '-');
			string u = s.substr(3, s.size() - 3);
			int i = 0;
			int x = trans(expr(u, i));
			val[id] = x;
			assert(i == u.size());
		}
		//cout << "fin " << s << "\n";
	}
	return res;
}
vector<LP> ttt(vector<P> v) {
	vector<LP> res;
	rep(i, v.size()) {
		ll sum = v[i].second;
		while (i + 1 < v.size() && v[i].first == v[i + 1].first) {
			i++; sum += v[i].second;
		}
		res.push_back({ v[i].first,sum });
	}
	return res;
}
void solve() {
	vector<string> vs(n), vt(m);
	rep(i, n)cin >> vs[i];
	rep(i, m)cin >> vt[i];
	vector<P> cs = calc(vs), ct = calc(vt);
	auto s = ttt(cs), t = ttt(ct);
	//cout << "hello s\n";
	//rep(i, s.size())cout << s[i].first << " " << s[i].second << "\n";
	//cout << "hello t\n";
	//rep(i, t.size())cout << t[i].first << " " << t[i].second << "\n";
	if (s == t) {
		cout << "Yes\n";
	}
	else {
		cout << "No\n";
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//init_f();
	//init();
	//while(true)
	//expr();
	//int t; cin >> t; rep(i, t)
	while (cin >> n >> m, n)
		solve();
	return 0;
}
accuracy: 0.02631578947368421
time_ms: 10
memory_kb: 11676
final_rank: 12

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <queue>
#include <ciso646>
#include <random>
#include <map>
#include <set>
#include <bitset>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <cassert>
#include <complex>
#include <numeric>
#include <array>
#include <chrono>
using namespace std;

#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
// constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<long long, long long> P;

#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define all(v) (v).begin(), (v).end()
typedef pair<ll, ll> LP;

template <typename T>
void chmin(T &a, T b)
{
  a = min(a, b);
}
template <typename T>
void chmax(T &a, T b)
{
  a = max(a, b);
}
template <typename T>
void cinarray(vector<T> &v)
{
  rep(i, v.size()) cin >> v[i];
}
template <typename T>
void coutarray(vector<T> &v)
{
  rep(i, v.size())
  {
    if (i > 0)
      cout << " ";
    cout << v[i];
  }
  cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod)
{
  if (n < 0)
  {
    ll res = mod_pow(x, -n, m);
    return mod_pow(res, m - 2, m);
  }
  if (abs(x) >= m)
    x %= m;
  if (x < 0)
    x += m;
  // if (x == 0)return 0;
  ll res = 1;
  while (n)
  {
    if (n & 1)
      res = res * x % m;
    x = x * x % m;
    n >>= 1;
  }
  return res;
}
struct modint
{
  int n;
  modint() : n(0) { ; }
  modint(ll m)
  {
    if (m < 0 || mod <= m)
    {
      m %= mod;
      if (m < 0)
        m += mod;
    }
    n = m;
  }
  operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint &a, modint b)
{
  a.n += b.n;
  if (a.n >= mod)
    a.n -= mod;
  return a;
}
modint operator-=(modint &a, modint b)
{
  a.n -= b.n;
  if (a.n < 0)
    a.n += mod;
  return a;
}
modint operator*=(modint &a, modint b)
{
  a.n = ((ll)a.n * b.n) % mod;
  return a;
}
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n)
{
  if (n == 0)
    return modint(1);
  modint res = (a * a) ^ (n / 2);
  if (n % 2)
    res = res * a;
  return res;
}

ll inv(ll a, ll p)
{
  return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint &a, modint b)
{
  a = a / b;
  return a;
}
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f()
{
  fact[0] = modint(1);
  for (int i = 0; i < max_n - 1; i++)
  {
    fact[i + 1] = fact[i] * modint(i + 1);
  }
  factinv[max_n - 1] = modint(1) / fact[max_n - 1];
  for (int i = max_n - 2; i >= 0; i--)
  {
    factinv[i] = factinv[i + 1] * modint(i + 1);
  }
}
modint comb(int a, int b)
{
  if (a < 0 || b < 0 || a < b)
    return 0;
  return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b)
{
  if (a < 0 || b < 0 || a < b)
    return 0;
  return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b)
{
  a = abs(a);
  b = abs(b);
  if (a < b)
    swap(a, b);
  while (b)
  {
    ll r = a % b;
    a = b;
    b = r;
  }
  return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template <typename T>
void addv(vector<T> &v, int loc, T val)
{
  if (loc >= v.size())
    v.resize(loc + 1, 0);
  v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
  fill(isp + 2, isp + mn, true);
  for (int i = 2; i < mn; i++) {
    if (!isp[i])continue;
    ps.push_back(i);
    for (int j = 2 * i; j < mn; j += i) {
      isp[j] = false;
    }
  }
}*/

//[,val)
template <typename T>
auto prev_itr(set<T> &st, T val)
{
  auto res = st.lower_bound(val);
  if (res == st.begin())
    return st.end();
  res--;
  return res;
}

//[val,)
template <typename T>
auto next_itr(set<T> &st, T val)
{
  auto res = st.lower_bound(val);
  return res;
}
using mP = pair<modint, modint>;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
//-----------------------------------------

typedef vector<vector<modint>> mat;
typedef vector<modint> vec;
mat mtmul(mat &A, mat &B)
{
  mat C(A.size(), vec(B[0].size()));
  rep(i, (int)A.size())
  {
    rep(k, (int)B.size())
    {
      rep(j, (int)B[0].size())
      {
        C[i][j] += A[i][k] * B[k][j];
      }
    }
  }
  return C;
}
mat mtpow(mat A, ll n)
{
  mat B(A.size(), vec(A.size()));
  rep(i, (int)A.size())
  {
    B[i][i] = 1;
  }
  while (n > 0)
  {
    if (n & (ll)1)
      B = mtmul(B, A);
    A = mtmul(A, A);
    n >>= 1;
  }
  return B;
}

int n, m;

modint val[26];

using ar = array<modint, 27>;

ar term(string &s, int &i)
{
  ar res;
  rep(j, 27) res[j] = 0;
  int coef = 1;
  while (s[i] == '-')
  {
    coef *= -1;
    i++;
  }
  if ('0' <= s[i] && s[i] <= '9')
  {
    modint cur = 0;
    while (i < s.size() && '0' <= s[i] && s[i] <= '9')
    {
      cur = (modint)10 * cur + (modint)(s[i] - '0');
      i++;
    }
    res[26] = cur;
  }
  else
  {
    assert('a' <= s[i] && s[i] <= 'z');
    res[s[i] - 'a'] = 1;
    i++;
  }
  rep(j, 27) res[j] *= coef;
  return res;
}
ar expr(string &s, int &i)
{
  ar res = term(s, i);
  while (i < s.size() && (s[i] == '+' || s[i] == '-'))
  {
    int coef = 1;
    if (s[i] == '-')
    // cout << "fin " << s << "\n";
    {
      coef = -1;
    }
    i++;
    ar val = term(s, i);
    rep(j, 27) res[j] += val[j] * (modint)coef;
  }
  return res;
}
modint trans(ar a)
{
  modint res = a[26];
  rep(i, 26) res += a[i] * (modint)val[i];
  return res;
}

vector<P> calc(vector<string> vs)
{
  vector<P> res;
  rep(i, 26) val[i] = 0;
  for (auto s : vs)
  {
    if (s.size() == 1 && 'a' <= s[0] && s[0] <= 'z')
    {
      // declear
      val[s[0] - 'a'] = 0;
    }
    else if (s.size() >= 2 && s[0] == 'p' && s[1] == 'r')
    {
      // print
      string u = s.substr(6, s.size() - 6 - 1);
      int i = 0;
      int v = trans(expr(u, i));
      res.push_back({v, 1});
      assert(i == u.size());
    }
    else if (s.size() >= 2 && s[0] == 'f' && s[1] == 'o')
    {
      // for
      int pro = 1;
      int loc = 0;
      while (true)
      {
        if (loc + 1 < s.size() && s[loc] == 'f' && s[loc + 1] == 'o')
        {
          loc += 4;

          int ri = loc;
          while (s[ri] != ')')
            ri++;
          string u = s.substr(loc, ri - loc);
          int i = 0;
          pro *= trans(expr(u, i));
          assert(i == u.size());

          loc = ri;
          loc++;
        }
        else
          break;
      }
      if (pro > 0)
      {
        if (s[loc] == 'p' && s[loc + 1] == 'r')
        {
          // print
          loc += 6;
          string u = s.substr(loc, s.size() - 1 - loc);
          int i = 0;
          int v = trans(expr(u, i));
          if (pro > 0)
            res.push_back({v, pro});
          assert(i == u.size());
        }
        else
        {
          // assign
          int id = s[loc] - 'a';
          assert(0 <= id && id < 26);
          assert(s[loc + 1] == '<' && s[loc + 2] == '-');
          loc += 3;
          string u = s.substr(loc, s.size() - loc);
          int i = 0;
          auto a = expr(u, i);
          assert(i == u.size());
          modint sum = 0;
          rep(j, 27) if (j != id) sum += a[j] * val[j];
          {
            mat A = {{a[id], sum}, {0, 1}};
            A = mtpow(A, pro);
            val[id] = A[0][0] * (modint)val[id] + A[0][1];
          }
        }
      }
    }
    else
    {
      // assign
      int id = s[0] - 'a';
      string u = s.substr(3, s.size() - 3);
      int i = 0;
      int x = trans(expr(u, i));
      val[id] = x;
      assert(i == u.size());
    }
  }
  return res;
}
vector<LP> ttt(vector<P> v)
{
  vector<LP> res;
  rep(i, v.size())
  {
    ll sum = v[i].second;
    while (i + 1 < v.size() && v[i].first == v[i + 1].first)
    {
      i++;
      sum += v[i].second;
    }
    res.push_back({v[i].first, sum});
  }
  return res;
}
void solve()
{
  vector<string> vs(n), vt(m);
  rep(i, n)
  {
    cin >> vs[i];
  }
  rep(i, m)
  {
    cin >> vt[i];
  }
  vector<P> cs = calc(vs), ct = calc(vt);
  auto s = ttt(cs), t = ttt(ct);
  // cout << "hello s\n";
  // rep(i, s.size())cout << s[i].first << " " << s[i].second << "\n";
  // cout << "hello t\n";
  // rep(i, t.size())cout << t[i].first << " " << t[i].second << "\n";
  if (s == t)
  {
    cout << "Yes\n";
  }
  else
  {
    cout << "No\n";
  }
}

signed main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout << fixed << setprecision(10);
  // init_f();
  // init();
  // while(true)
  // expr();
  // int t; cin >> t; rep(i, t)
  while (cin >> n >> m, n)
    solve();
  return 0;
}
accuracy: 0.02631578947368421
time_ms: 10
memory_kb: 19836
final_rank: 15

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
//constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

template<typename T>
void chmin(T& a, T b) {
	a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
	a = max(a, b);
}
template<typename T>
void cinarray(vector<T>& v) {
	rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
	rep(i, v.size()) {
		if (i > 0)cout << " "; cout << v[i];
	}
	cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	//if (x == 0)return 0;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	int n;
	modint() :n(0) { ; }
	modint(ll m) {
		if (m < 0 || mod <= m) {
			m %= mod; if (m < 0)m += mod;
		}
		n = m;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template<typename T>
void addv(vector<T>& v, int loc, T val) {
	if (loc >= v.size())v.resize(loc + 1, 0);
	v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
	fill(isp + 2, isp + mn, true);
	for (int i = 2; i < mn; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j < mn; j += i) {
			isp[j] = false;
		}
	}
}*/

//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	if (res == st.begin())return st.end();
	res--; return res;
}

//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	return res;
}
using mP = pair<modint, modint>;


int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
//-----------------------------------------

typedef vector<vector<modint>> mat;
typedef vector<modint> vec;
mat mtmul(mat& A, mat& B) {
	mat C(A.size(), vec(B[0].size()));
	rep(i, (int)A.size()) {
		rep(k, (int)B.size()) {
			rep(j, (int)B[0].size()) {
				C[i][j] += A[i][k] * B[k][j];
			}
		}
	}
	return C;
}
mat mtpow(mat A, ll n) {
	mat B(A.size(), vec(A.size()));
	rep(i, (int)A.size()) {
		B[i][i] = 1;
	}
	while (n > 0) {
		if (n & (ll)1)B = mtmul(B, A);
		A = mtmul(A, A);
		n >>= 1;
	}
	return B;
}

int n, m;

modint val[26];

using ar = array<modint, 27>;

ar term(string& s, int& i) {
	ar res;
	rep(j, 27)res[j] = 0;
	int coef = 1;
	while(s[i] == '-') {
		coef *= -1;
		i++;
	}
	while (s[i] == '+')i++;
	if ('0' <= s[i] && s[i] <= '9') {
		modint cur = 0;
		while (i < s.size() && '0' <= s[i] && s[i] <= '9') {
			cur = (modint)10 * cur + (modint)(s[i] - '0');
			i++;
		}
		res[26] = cur;
	}
	else {
		assert('a' <= s[i] && s[i] <= 'z');
		res[s[i] - 'a'] = 1;
		i++;
	}
	rep(j, 27)res[j] *= coef;
	return res;
}
ar expr(string& s, int& i) {
	ar res = term(s, i);
	while (i < s.size() && (s[i] == '+' || s[i] == '-')) {
		int coef = 1;
		if (s[i] == '-') {
			coef = -1;
		}
		i++;
		ar val = term(s, i);
		rep(j, 27)res[j] += val[j] * (modint)coef;
	}
	return res;
}
modint trans(ar a) {
	modint res = a[26];
	rep(i, 26)res += a[i] * (modint)val[i];
	return res;
}

vector<P> calc(vector<string> vs) {
	vector<P> res;
	rep(i, 26)val[i] = 0;
	for (auto s : vs) {
		if (s.size() == 1) {
			//declear
			int id = s[0] - 'a';
			assert(0 <= id && id < 26);
			val[id] = 0;
		}
		else if (s.size() >= 2 && s[0] == 'p' && s[1] == 'r') {
			assert(s.substr(0, 5) == "print");
			//print
			string u = s.substr(6, s.size() - 6 - 1);
			int i = 0;
			int v = trans(expr(u, i));
			res.push_back({ v,1 });
			assert(i == u.size());
		}
		else if (s.size() >= 2 && s[0] == 'f' && s[1] == 'o') {
			assert(s.substr(0, 3) == "for");
			//for
			int pro = 1;
			int loc = 0;
			while (true) {
				if (loc + 1 < s.size() && s[loc] == 'f' && s[loc + 1] == 'o') {
					assert(s.substr(loc, 3) == "for");
					//for(
					loc += 4;

					int ri = loc;
					while (s[ri] != ')')ri++;
					assert(s[ri] == ')');
					string u = s.substr(loc, ri - loc);
					int i = 0;
					pro *= trans(expr(u, i));
					assert(i == u.size());

					loc = ri;
					//)
					loc++;
				}
				else break;
			}
			if (pro > 0) {
				if (s[loc] == 'p' && s[loc + 1] == 'r') {
					assert(s.substr(loc, 5) == "print");
					//print
					loc += 6;
					string u = s.substr(loc, s.size() - 1 - loc);
					int i = 0;
					int v = trans(expr(u, i));
					res.push_back({ v,pro });
					assert(i == u.size());
				}
				else {
					//assign
					int id = s[loc] - 'a';
					assert(0 <= id && id < 26);
					assert(s[loc + 1] == '<' && s[loc + 2] == '-');
					loc += 3;
					string u = s.substr(loc, s.size() - loc);
					int i = 0;
					auto a = expr(u, i);
					assert(i == u.size());
					modint sum = 0;
					rep(j, 27)if (j != id)sum += a[j];
					//cout << "e? " << a[0] << "\n";
					//cout << "?? " << u << "\n";

					{
						mat A = { {a[id],sum},{0,1} };
						A = mtpow(A, pro);
						val[id] = A[0][0] * (modint)val[id] + A[0][1];
					}
					/*{
						if (a[id] == (modint)1) {
							val[id] += (modint)pro * sum;
						}
						else {
							modint p = a[id];
							modint q = sum;
							modint x = mod_pow(p, pro) - 1;
							x /= (p - (modint)1);
							x *= q;
							x += val[id] * (modint)mod_pow(p, pro);
							val[id] = x;
						}
					}*/
				}
			}
		}
		else {
			//assign
			int id = s[0] - 'a';
			assert(0 <= id && id < 26);
			assert(s[1] == '<');
			assert(s[2] == '-');
			string u = s.substr(3, s.size() - 3);
			int i = 0;
			int x = trans(expr(u, i));
			val[id] = x;
			assert(i == u.size());
		}
		//cout << "fin " << s << "\n";
	}
	return res;
}
vector<LP> ttt(vector<P> v) {
	vector<LP> res;
	rep(i, v.size()) {
		ll sum = v[i].second;
		while (i + 1 < v.size() && v[i].first == v[i + 1].first) {
			i++; sum += v[i].second;
		}
		res.push_back({ v[i].first,sum });
	}
	return res;
}
void solve() {
	vector<string> vs(n), vt(m);
	rep(i, n)cin >> vs[i];
	rep(i, m)cin >> vt[i];
	vector<P> cs = calc(vs), ct = calc(vt);
	auto s = ttt(cs), t = ttt(ct);
	//cout << "hello s\n";
	//rep(i, s.size())cout << s[i].first << " " << s[i].second << "\n";
	//cout << "hello t\n";
	//rep(i, t.size())cout << t[i].first << " " << t[i].second << "\n";
	if (s == t) {
		cout << "Yes\n";
	}
	else {
		cout << "No\n";
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//init_f();
	//init();
	//while(true)
	//expr();
	//int t; cin >> t; rep(i, t)
	while (cin >> n >> m, n)
		solve();
	return 0;
}
accuracy: 0.02631578947368421
time_ms: 10
memory_kb: 11656
final_rank: 11

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <queue>
#include <ciso646>
#include <random>
#include <map>
#include <set>
#include <bitset>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <cassert>
#include <complex>
#include <numeric>
#include <array>
#include <chrono>
using namespace std;

#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
// constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<long long, long long> P;

#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define all(v) (v).begin(), (v).end()
typedef pair<ll, ll> LP;

template <typename T>
void chmin(T &a, T b)
{
  a = min(a, b);
}
template <typename T>
void chmax(T &a, T b)
{
  a = max(a, b);
}
template <typename T>
void cinarray(vector<T> &v)
{
  rep(i, v.size()) cin >> v[i];
}
template <typename T>
void coutarray(vector<T> &v)
{
  rep(i, v.size())
  {
    if (i > 0)
      cout << " ";
    cout << v[i];
  }
  cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod)
{
  if (n < 0)
  {
    ll res = mod_pow(x, -n, m);
    return mod_pow(res, m - 2, m);
  }
  if (abs(x) >= m)
    x %= m;
  if (x < 0)
    x += m;
  // if (x == 0)return 0;
  ll res = 1;
  while (n)
  {
    if (n & 1)
      res = res * x % m;
    x = x * x % m;
    n >>= 1;
  }
  return res;
}
struct modint
{
  int n;
  modint() : n(0) { ; }
  modint(ll m)
  {
    if (m < 0 || mod <= m)
    {
      m %= mod;
      if (m < 0)
        m += mod;
    }
    n = m;
  }
  operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint &a, modint b)
{
  a.n += b.n;
  if (a.n >= mod)
    a.n -= mod;
  return a;
}
modint operator-=(modint &a, modint b)
{
  a.n -= b.n;
  if (a.n < 0)
    a.n += mod;
  return a;
}
modint operator*=(modint &a, modint b)
{
  a.n = ((ll)a.n * b.n) % mod;
  return a;
}
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n)
{
  if (n == 0)
    return modint(1);
  modint res = (a * a) ^ (n / 2);
  if (n % 2)
    res = res * a;
  return res;
}

ll inv(ll a, ll p)
{
  return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint &a, modint b)
{
  a = a / b;
  return a;
}
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f()
{
  fact[0] = modint(1);
  for (int i = 0; i < max_n - 1; i++)
  {
    fact[i + 1] = fact[i] * modint(i + 1);
  }
  factinv[max_n - 1] = modint(1) / fact[max_n - 1];
  for (int i = max_n - 2; i >= 0; i--)
  {
    factinv[i] = factinv[i + 1] * modint(i + 1);
  }
}
modint comb(int a, int b)
{
  if (a < 0 || b < 0 || a < b)
    return 0;
  return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b)
{
  if (a < 0 || b < 0 || a < b)
    return 0;
  return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b)
{
  a = abs(a);
  b = abs(b);
  if (a < b)
    swap(a, b);
  while (b)
  {
    ll r = a % b;
    a = b;
    b = r;
  }
  return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template <typename T>
void addv(vector<T> &v, int loc, T val)
{
  if (loc >= v.size())
    v.resize(loc + 1, 0);
  v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
  fill(isp + 2, isp + mn, true);
  for (int i = 2; i < mn; i++) {
    if (!isp[i])continue;
    ps.push_back(i);
    for (int j = 2 * i; j < mn; j += i) {
      isp[j] = false;
    }
  }
}*/

//[,val)
template <typename T>
auto prev_itr(set<T> &st, T val)
{
  auto res = st.lower_bound(val);
  if (res == st.begin())
    return st.end();
  res--;
  return res;
}

//[val,)
template <typename T>
auto next_itr(set<T> &st, T val)
{
  auto res = st.lower_bound(val);
  return res;
}
using mP = pair<modint, modint>;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
//-----------------------------------------

typedef vector<vector<modint>> mat;
typedef vector<modint> vec;
mat mtmul(mat &A, mat &B)
{
  mat C(A.size(), vec(B[0].size()));
  rep(i, (int)A.size())
  {
    rep(k, (int)B.size())
    {
      rep(j, (int)B[0].size())
      {
        C[i][j] += A[i][k] * B[k][j];
      }
    }
  }
  return C;
}
mat mtpow(mat A, ll n)
{
  mat B(A.size(), vec(A.size()));
  rep(i, (int)A.size())
  {
    B[i][i] = 1;
  }
  while (n > 0)
  {
    if (n & (ll)1)
      B = mtmul(B, A);
    A = mtmul(A, A);
    n >>= 1;
  }
  return B;
}

int n, m;

modint val[26];

using ar = array<modint, 27>;

ar term(string &s, int &i)
{
  ar res;
  rep(j, 27) res[j] = 0;
  int coef = 1;
  while (s[i] == '-')
  {
    coef *= -1;
    i++;
  }
  if ('0' <= s[i] && s[i] <= '9')
  {
    modint cur = 0;
    while (i < s.size() && '0' <= s[i] && s[i] <= '9')
    {
      cur = (modint)10 * cur + (modint)(s[i] - '0');
      i++;
    }
    res[26] = cur;
  }
  else
  {
    assert('a' <= s[i] && s[i] <= 'z');
    res[s[i] - 'a'] = 1;
    i++;
  }
  rep(j, 27) res[j] *= coef;
  return res;
}
ar expr(string &s, int &i)
{
  ar res = term(s, i);
  while (i < s.size() && (s[i] == '+' || s[i] == '-'))
  {
    int coef = 1;
    if (s[i] == '-')
    // cout << "fin " << s << "\n";
    {
      coef = -1;
    }
    i++;
    ar val = term(s, i);
    rep(j, 27) res[j] += val[j] * (modint)coef;
  }
  return res;
}
modint trans(ar a)
{
  modint res = a[26];
  rep(i, 26) res += a[i] * (modint)val[i];
  return res;
}

vector<P> calc(vector<string> vs)
{
  vector<P> res;
  rep(i, 26) val[i] = 0;
  for (auto s : vs)
  {
    if (s.size() == 1 && 'a' <= s[0] && s[0] <= 'z')
    {
      // declear
      val[s[0] - 'a'] = 0;
    }
    else if (s.size() >= 2 && s[0] == 'p' && s[1] == 'r')
    {
      // print
      string u = s.substr(6, s.size() - 6 - 1);
      int i = 0;
      int v = trans(expr(u, i));
      res.push_back({v, 1});
      assert(i == u.size());
    }
    else if (s.size() >= 2 && s[0] == 'f' && s[1] == 'o')
    {
      // for
      int pro = 1;
      int loc = 0;
      while (true)
      {
        if (loc + 1 < s.size() && s[loc] == 'f' && s[loc + 1] == 'o')
        {
          loc += 4;

          int ri = loc;
          while (s[ri] != ')')
            ri++;
          string u = s.substr(loc, ri - loc);
          int i = 0;
          pro *= trans(expr(u, i));
          assert(i == u.size());

          loc = ri;
          loc++;
        }
        else
          break;
      }
      if (pro > 0)
      {
        if (s[loc] == 'p' && s[loc + 1] == 'r')
        {
          // print
          loc += 6;
          string u = s.substr(loc, s.size() - 1 - loc);
          int i = 0;
          int v = trans(expr(u, i));
          if (pro > 0)
            res.push_back({v, pro});
          assert(i == u.size());
        }
        else
        {
          // assign
          int id = s[loc] - 'a';
          assert(0 <= id && id < 26);
          assert(s[loc + 1] == '<' && s[loc + 2] == '-');
          loc += 3;
          string u = s.substr(loc, s.size() - loc);
          int i = 0;
          auto a = expr(u, i);
          assert(i == u.size());
          modint sum = 0;
          rep(j, 27) if (j != id) sum += a[j];
          {
            mat A = {{a[id], sum}, {0, 1}};
            A = mtpow(A, pro);
            val[id] = A[0][0] * (modint)val[id] + A[0][1];
          }
        }
      }
    }
    else
    {
      // assign
      int id = s[0] - 'a';
      string u = s.substr(3, s.size() - 3);
      int i = 0;
      int x = trans(expr(u, i));
      val[id] = x;
      assert(i == u.size());
    }
  }
  return res;
}
vector<LP> ttt(vector<P> v)
{
  vector<LP> res;
  rep(i, v.size())
  {
    ll sum = v[i].second;
    while (i + 1 < v.size() && v[i].first == v[i + 1].first)
    {
      i++;
      sum += v[i].second;
    }
    res.push_back({v[i].first, sum});
  }
  return res;
}
void solve()
{
  vector<string> vs(n), vt(m);
  rep(i, n)
  {
    cin >> vs[i];
  }
  rep(i, m)
  {
    cin >> vt[i];
  }
  vector<P> cs = calc(vs), ct = calc(vt);
  auto s = ttt(cs), t = ttt(ct);
  // cout << "hello s\n";
  // rep(i, s.size())cout << s[i].first << " " << s[i].second << "\n";
  // cout << "hello t\n";
  // rep(i, t.size())cout << t[i].first << " " << t[i].second << "\n";
  if (s == t)
  {
    cout << "Yes\n";
  }
  else
  {
    cout << "No\n";
  }
}

signed main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout << fixed << setprecision(10);
  // init_f();
  // init();
  // while(true)
  // expr();
  // int t; cin >> t; rep(i, t)
  while (cin >> n >> m, n)
    solve();
  return 0;
}
accuracy: 0.02631578947368421
time_ms: 10
memory_kb: 19848
final_rank: 16

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
//constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

template<typename T>
void chmin(T& a, T b) {
	a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
	a = max(a, b);
}
template<typename T>
void cinarray(vector<T>& v) {
	rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
	rep(i, v.size()) {
		if (i > 0)cout << " "; cout << v[i];
	}
	cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	//if (x == 0)return 0;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	int n;
	modint() :n(0) { ; }
	modint(ll m) {
		if (m < 0 || mod <= m) {
			m %= mod; if (m < 0)m += mod;
		}
		n = m;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template<typename T>
void addv(vector<T>& v, int loc, T val) {
	if (loc >= v.size())v.resize(loc + 1, 0);
	v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
	fill(isp + 2, isp + mn, true);
	for (int i = 2; i < mn; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j < mn; j += i) {
			isp[j] = false;
		}
	}
}*/

//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	if (res == st.begin())return st.end();
	res--; return res;
}

//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	return res;
}
using mP = pair<modint, modint>;


int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
//-----------------------------------------

typedef vector<vector<modint>> mat;
typedef vector<modint> vec;
mat mtmul(mat& A, mat& B) {
	mat C(A.size(), vec(B[0].size()));
	rep(i, (int)A.size()) {
		rep(k, (int)B.size()) {
			rep(j, (int)B[0].size()) {
				C[i][j] += A[i][k] * B[k][j];
			}
		}
	}
	return C;
}
mat mtpow(mat A, ll n) {
	mat B(A.size(), vec(A.size()));
	rep(i, (int)A.size()) {
		B[i][i] = 1;
	}
	while (n > 0) {
		if (n & (ll)1)B = mtmul(B, A);
		A = mtmul(A, A);
		n >>= 1;
	}
	return B;
}

int n, m;

modint val[26];

using ar = array<modint, 27>;

ar term(string& s, int& i) {
	ar res;
	rep(j, 27)res[j] = 0;
	int coef = 1;
	while(s[i] == '-') {
		coef *= -1;
		i++;
	}
	while (s[i] == '+')i++;
	if ('0' <= s[i] && s[i] <= '9') {
		modint cur = 0;
		while (i < s.size() && '0' <= s[i] && s[i] <= '9') {
			cur = (modint)10 * cur + (modint)(s[i] - '0');
			i++;
		}
		res[26] = cur;
	}
	else {
		assert('a' <= s[i] && s[i] <= 'z');
		res[s[i] - 'a'] = 1;
		i++;
	}
	rep(j, 27)res[j] *= coef;
	return res;
}
ar expr(string& s, int& i) {
	ar res = term(s, i);
	while (i < s.size() && (s[i] == '+' || s[i] == '-')) {
		int coef = 1;
		if (s[i] == '-') {
			coef = -1;
		}
		i++;
		ar val = term(s, i);
		rep(j, 27)res[j] += val[j] * (modint)coef;
	}
	return res;
}
modint trans(ar a) {
	modint res = a[26];
	rep(i, 26)res += a[i] * (modint)val[i];
	return res;
}

vector<P> calc(vector<string> vs) {
	vector<P> res;
	rep(i, 26)val[i] = 0;
	for (auto s : vs) {
		if (s.size() == 1 && 'a' <= s[0] && s[0] <= 'z') {
			//declear
			val[s[0] - 'a'] = 0;
		}
		else if (s.size() >= 2 && s[0] == 'p' && s[1] == 'r') {
			//print
			string u = s.substr(6, s.size() - 6 - 1);
			int i = 0;
			int v = trans(expr(u, i));
			res.push_back({ v,1 });
			assert(i == u.size());
		}
		else if (s.size() >= 2 && s[0] == 'f' && s[1] == 'o') {
			//for
			int pro = 1;
			int loc = 0;
			while (true) {
				if (loc + 1 < s.size() && s[loc] == 'f' && s[loc + 1] == 'o') {
					//for(
					loc += 4;

					int ri = loc;
					while (s[ri] != ')')ri++;
					string u = s.substr(loc, ri - loc);
					int i = 0;
					pro *= trans(expr(u, i));
					assert(i == u.size());

					loc = ri;
					//)
					loc++;
				}
				else break;
			}
			if (pro > 0) {
				if (s[loc] == 'p' && s[loc + 1] == 'r') {
					//print
					loc += 6;
					string u = s.substr(loc, s.size() - 1 - loc);
					int i = 0;
					int v = trans(expr(u, i));
					if (pro > 0)res.push_back({ v,pro });
					assert(i == u.size());
				}
				else {
					//assign
					int id = s[loc] - 'a';
					assert(0 <= id && id < 26);
					assert(s[loc + 1] == '<' && s[loc + 2] == '-');
					loc += 3;
					string u = s.substr(loc, s.size() - loc);
					int i = 0;
					auto a = expr(u, i);
					assert(i == u.size());
					modint sum = 0;
					rep(j, 27)if (j != id)sum += a[j];
					//cout << "e? " << a[0] << "\n";
					//cout << "?? " << u << "\n";

					{
						mat A = { {a[id],sum},{0,1} };
						A = mtpow(A, pro);
						val[id] = A[0][0] * (modint)val[id] + A[0][1];
					}
					/*{
						if (a[id] == (modint)1) {
							val[id] += (modint)pro * sum;
						}
						else {
							modint p = a[id];
							modint q = sum;
							modint x = mod_pow(p, pro) - 1;
							x /= (p - (modint)1);
							x *= q;
							x += val[id] * (modint)mod_pow(p, pro);
							val[id] = x;
						}
					}*/
				}
			}
		}
		else {
			//assign
			int id = s[0] - 'a';
			assert(0 <= id && id < 26);
			assert(s[1] == '<');
			assert(s[2] == '-');
			string u = s.substr(3, s.size() - 3);
			int i = 0;
			int x = trans(expr(u, i));
			val[id] = x;
			assert(i == u.size());
		}
		//cout << "fin " << s << "\n";
	}
	return res;
}
vector<LP> ttt(vector<P> v) {
	vector<LP> res;
	rep(i, v.size()) {
		ll sum = v[i].second;
		while (i + 1 < v.size() && v[i].first == v[i + 1].first) {
			i++; sum += v[i].second;
		}
		res.push_back({ v[i].first,sum });
	}
	return res;
}
void solve() {
	vector<string> vs(n), vt(m);
	rep(i, n)cin >> vs[i];
	rep(i, m)cin >> vt[i];
	vector<P> cs = calc(vs), ct = calc(vt);
	auto s = ttt(cs), t = ttt(ct);
	//cout << "hello s\n";
	//rep(i, s.size())cout << s[i].first << " " << s[i].second << "\n";
	//cout << "hello t\n";
	//rep(i, t.size())cout << t[i].first << " " << t[i].second << "\n";
	if (s == t) {
		cout << "Yes\n";
	}
	else {
		cout << "No\n";
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//init_f();
	//init();
	//while(true)
	//expr();
	//int t; cin >> t; rep(i, t)
	while (cin >> n >> m, n)
		solve();
	return 0;
}
accuracy: 0.02631578947368421
time_ms: 10
memory_kb: 11608
final_rank: 9

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
//constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

template<typename T>
void chmin(T& a, T b) {
	a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
	a = max(a, b);
}
template<typename T>
void cinarray(vector<T>& v) {
	rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
	rep(i, v.size()) {
		if (i > 0)cout << " "; cout << v[i];
	}
	cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	//if (x == 0)return 0;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	int n;
	modint() :n(0) { ; }
	modint(ll m) {
		if (m < 0 || mod <= m) {
			m %= mod; if (m < 0)m += mod;
		}
		n = m;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template<typename T>
void addv(vector<T>& v, int loc, T val) {
	if (loc >= v.size())v.resize(loc + 1, 0);
	v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
	fill(isp + 2, isp + mn, true);
	for (int i = 2; i < mn; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j < mn; j += i) {
			isp[j] = false;
		}
	}
}*/

//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	if (res == st.begin())return st.end();
	res--; return res;
}

//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	return res;
}
using mP = pair<modint, modint>;


int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
//-----------------------------------------

typedef vector<vector<modint>> mat;
typedef vector<modint> vec;
mat mtmul(mat& A, mat& B) {
	mat C(A.size(), vec(B[0].size()));
	rep(i, (int)A.size()) {
		rep(k, (int)B.size()) {
			rep(j, (int)B[0].size()) {
				C[i][j] += A[i][k] * B[k][j];
			}
		}
	}
	return C;
}
mat mtpow(mat A, ll n) {
	mat B(A.size(), vec(A.size()));
	rep(i, (int)A.size()) {
		B[i][i] = 1;
	}
	while (n > 0) {
		if (n & (ll)1)B = mtmul(B, A);
		A = mtmul(A, A);
		n >>= 1;
	}
	return B;
}

int n, m;

modint val[26];

using ar = array<modint, 27>;

ar term(string& s, int& i) {
	ar res;
	rep(j, 27)res[j] = 0;
	int coef = 1;
	while(s[i] == '-') {
		coef *= -1;
		i++;
	}
	if ('0' <= s[i] && s[i] <= '9') {
		modint cur = 0;
		while (i < s.size() && '0' <= s[i] && s[i] <= '9') {
			cur = (modint)10 * cur + (modint)(s[i] - '0');
			i++;
		}
		res[26] = cur;
	}
	else {
		assert('a' <= s[i] && s[i] <= 'z');
		res[s[i] - 'a'] = 1;
		i++;
	}
	rep(j, 27)res[j] *= coef;
	return res;
}
ar expr(string& s, int& i) {
	ar res = term(s, i);
	while (i < s.size() && (s[i] == '+' || s[i] == '-')) {
		int coef = 1;
		if (s[i] == '-') {
			coef = -1;
		}
		i++;
		ar val = term(s, i);
		rep(j, 27)res[j] += val[j] * (modint)coef;
	}
	return res;
}
modint trans(ar a) {
	modint res = a[26];
	rep(i, 26)res += a[i] * (modint)val[i];
	return res;
}

vector<P> calc(vector<string> vs) {
	vector<P> res;
	rep(i, 26)val[i] = 0;
	for (auto s : vs) {
		if (s.size() == 1 && 'a' <= s[0] && s[0] <= 'z') {
			//declear
			val[s[0] - 'a'] = 0;
		}
		else if (s.size() >= 2 && s[0] == 'p' && s[1] == 'r') {
			//print
			string u = s.substr(6, s.size() - 6 - 1);
			int i = 0;
			int v = trans(expr(u, i));
			res.push_back({ v,1 });
			assert(i == u.size());
		}
		else if (s.size() >= 2 && s[0] == 'f' && s[1] == 'o') {
			//for
			int pro = 1;
			int loc = 0;
			while (true) {
				if (loc + 1 < s.size() && s[loc] == 'f' && s[loc + 1] == 'o') {
					//for(
					loc += 4;

					int ri = loc;
					while (s[ri] != ')')ri++;
					string u = s.substr(loc, ri - loc);
					int i = 0;
					pro *= trans(expr(u, i));
					assert(i == u.size());

					loc = ri;
					//)
					loc++;
				}
				else break;
			}
			if (pro > 0) {
				if (s[loc] == 'p' && s[loc + 1] == 'r') {
					//print
					loc += 6;
					string u = s.substr(loc, s.size() - 1 - loc);
					int i = 0;
					int v = trans(expr(u, i));
					if (pro > 0)res.push_back({ v,pro });
					assert(i == u.size());
				}
				else {
					//assign
					int id = s[loc] - 'a';
					assert(0 <= id && id < 26);
					assert(s[loc + 1] == '<' && s[loc + 2] == '-');
					loc += 3;
					string u = s.substr(loc, s.size() - loc);
					int i = 0;
					auto a = expr(u, i);
					assert(i == u.size());
					modint sum = 0;
					rep(j, 27)if (j != id)sum += a[j];
					//cout << "e? " << a[0] << "\n";
					//cout << "?? " << u << "\n";

					{
						mat A = { {a[id],sum},{0,1} };
						A = mtpow(A, pro);
						val[id] = A[0][0] * (modint)val[id] + A[0][1];
					}
					/*{
						if (a[id] == (modint)1) {
							val[id] += (modint)pro * sum;
						}
						else {
							modint p = a[id];
							modint q = sum;
							modint x = mod_pow(p, pro) - 1;
							x /= (p - (modint)1);
							x *= q;
							x += val[id] * (modint)mod_pow(p, pro);
							val[id] = x;
						}
					}*/
				}
			}
		}
		else {
			//assign
			int id = s[0] - 'a';
			assert(0 <= id && id < 26);
			assert(s[1] == '<');
			assert(s[2] == '-');
			string u = s.substr(3, s.size() - 3);
			int i = 0;
			int x = trans(expr(u, i));
			val[id] = x;
			assert(i == u.size());
		}
		//cout << "fin " << s << "\n";
	}
	return res;
}
vector<LP> ttt(vector<P> v) {
	vector<LP> res;
	rep(i, v.size()) {
		ll sum = v[i].second;
		while (i + 1 < v.size() && v[i].first == v[i + 1].first) {
			i++; sum += v[i].second;
		}
		res.push_back({ v[i].first,sum });
	}
	return res;
}
void solve() {
	vector<string> vs(n), vt(m);
	rep(i, n)cin >> vs[i];
	rep(i, m)cin >> vt[i];
	vector<P> cs = calc(vs), ct = calc(vt);
	auto s = ttt(cs), t = ttt(ct);
	//cout << "hello s\n";
	//rep(i, s.size())cout << s[i].first << " " << s[i].second << "\n";
	//cout << "hello t\n";
	//rep(i, t.size())cout << t[i].first << " " << t[i].second << "\n";
	if (s == t) {
		cout << "Yes\n";
	}
	else {
		cout << "No\n";
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//init_f();
	//init();
	//while(true)
	//expr();
	//int t; cin >> t; rep(i, t)
	while (cin >> n >> m, n)
		solve();
	return 0;
}
accuracy: 0.02631578947368421
time_ms: 10
memory_kb: 11644
final_rank: 10

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <queue>
#include <ciso646>
#include <random>
#include <map>
#include <set>
#include <bitset>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <cassert>
#include <complex>
#include <numeric>
#include <array>
#include <chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
// constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<long long, long long> P;

#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define all(v) (v).begin(), (v).end()
typedef pair<ll, ll> LP;

template <typename T>
void chmin(T &a, T b)
{
  a = min(a, b);
}
template <typename T>
void chmax(T &a, T b)
{
  a = max(a, b);
}
template <typename T>
void cinarray(vector<T> &v)
{
  rep(i, v.size()) cin >> v[i];
}
template <typename T>
void coutarray(vector<T> &v)
{
  rep(i, v.size())
  {
    if (i > 0)
      cout << " ";
    cout << v[i];
  }
  cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod)
{
  if (n < 0)
  {
    ll res = mod_pow(x, -n, m);
    return mod_pow(res, m - 2, m);
  }
  if (abs(x) >= m)
    x %= m;
  if (x < 0)
    x += m;
  // if (x == 0)return 0;
  ll res = 1;
  while (n)
  {
    if (n & 1)
      res = res * x % m;
    x = x * x % m;
    n >>= 1;
  }
  return res;
}
struct modint
{
  int n;
  modint() : n(0) { ; }
  modint(ll m)
  {
    if (m < 0 || mod <= m)
    {
      m %= mod;
      if (m < 0)
        m += mod;
    }
    n = m;
  }
  operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint &a, modint b)
{
  a.n += b.n;
  if (a.n >= mod)
    a.n -= mod;
  return a;
}
modint operator-=(modint &a, modint b)
{
  a.n -= b.n;
  if (a.n < 0)
    a.n += mod;
  return a;
}
modint operator*=(modint &a, modint b)
{
  a.n = ((ll)a.n * b.n) % mod;
  return a;
}
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n)
{
  if (n == 0)
    return modint(1);
  modint res = (a * a) ^ (n / 2);
  if (n % 2)
    res = res * a;
  return res;
}

ll inv(ll a, ll p)
{
  return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint &a, modint b)
{
  a = a / b;
  return a;
}
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f()
{
  fact[0] = modint(1);
  for (int i = 0; i < max_n - 1; i++)
  {
    fact[i + 1] = fact[i] * modint(i + 1);
  }
  factinv[max_n - 1] = modint(1) / fact[max_n - 1];
  for (int i = max_n - 2; i >= 0; i--)
  {
    factinv[i] = factinv[i + 1] * modint(i + 1);
  }
}
modint comb(int a, int b)
{
  if (a < 0 || b < 0 || a < b)
    return 0;
  return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b)
{
  if (a < 0 || b < 0 || a < b)
    return 0;
  return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b)
{
  a = abs(a);
  b = abs(b);
  if (a < b)
    swap(a, b);
  while (b)
  {
    ll r = a % b;
    a = b;
    b = r;
  }
  return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template <typename T>
void addv(vector<T> &v, int loc, T val)
{
  if (loc >= v.size())
    v.resize(loc + 1, 0);
  v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
  fill(isp + 2, isp + mn, true);
  for (int i = 2; i < mn; i++) {
    if (!isp[i])continue;
    ps.push_back(i);
    for (int j = 2 * i; j < mn; j += i) {
      isp[j] = false;
    }
  }
}*/

//[,val)
template <typename T>
auto prev_itr(set<T> &st, T val)
{
  auto res = st.lower_bound(val);
  if (res == st.begin())
    return st.end();
  res--;
  return res;
}

//[val,)
template <typename T>
auto next_itr(set<T> &st, T val)
{
  auto res = st.lower_bound(val);
  return res;
}
using mP = pair<modint, modint>;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
//-----------------------------------------

typedef vector<vector<modint>> mat;
typedef vector<modint> vec;
mat mtmul(mat &A, mat &B)
{
  mat C(A.size(), vec(B[0].size()));
  rep(i, (int)A.size())
  {
    rep(k, (int)B.size())
    {
      rep(j, (int)B[0].size())
      {
        C[i][j] += A[i][k] * B[k][j];
      }
    }
  }
  return C;
}
mat mtpow(mat A, ll n)
{
  mat B(A.size(), vec(A.size()));
  rep(i, (int)A.size())
  {
    B[i][i] = 1;
  }
  while (n > 0)
  {
    if (n & (ll)1)
      B = mtmul(B, A);
    A = mtmul(A, A);
    n >>= 1;
  }
  return B;
}

int n, m;

modint val[26];

using ar = array<modint, 27>;

ar term(string &s, int &i)
{
  ar res;
  rep(j, 27) res[j] = 0;
  int coef = 1;
  while (s[i] == '-')
  {
    coef *= -1;
    i++;
  }
  if ('0' <= s[i] && s[i] <= '9')
  {
    modint cur = 0;
    while (i < s.size() && '0' <= s[i] && s[i] <= '9')
    {
      cur = (modint)10 * cur + (modint)(s[i] - '0');
      i++;
    }
    res[26] = cur;
  }
  else
  {
    assert('a' <= s[i] && s[i] <= 'z');
    res[s[i] - 'a'] = 1;
    i++;
  }
  rep(j, 27) res[j] *= coef;
  return res;
}
ar expr(string &s, int &i)
{
  ar res = term(s, i);
  while (i < s.size() && (s[i] == '+' || s[i] == '-'))
  {
    int coef = 1;
    if (s[i] == '-')
    {
      coef = -1;
    }
    i++;
    ar val = term(s, i);
    rep(j, 27) res[j] += val[j] * (modint)coef;
  }
  return res;
}
modint trans(ar a)
{
  modint res = a[26];
  rep(i, 26) res += a[i] * (modint)val[i];
  return res;
}

vector<P> calc(vector<string> vs)
{
  vector<P> res;
  rep(i, 26) val[i] = 0;
  for (auto s : vs)
  {
    if (s.size() == 1 && 'a' <= s[0] && s[0] <= 'z')
    {
      // declear
      val[s[0] - 'a'] = 0;
    }
    else if (s.size() >= 2 && s[0] == 'p' && s[1] == 'r')
    {
      // print
      string u = s.substr(6, s.size() - 6 - 1);
      int i = 0;
      int v = trans(expr(u, i));
      res.push_back({v, 1});
      assert(i == u.size());
    }
    else if (s.size() >= 2 && s[0] == 'f' && s[1] == 'o')
    {
      // for
      int pro = 1;
      int loc = 0;
      while (true)
      {
        if (loc + 1 < s.size() && s[loc] == 'f' && s[loc + 1] == 'o')
        {
          loc += 4;

          int ri = loc;
          while (s[ri] != ')')
            ri++;
          string u = s.substr(loc, ri - loc);
          int i = 0;
          pro *= trans(expr(u, i));
          assert(i == u.size());

          loc = ri;
          loc++;
        }
        else
          break;
      }
      if (pro > 0)
      {
        if (s[loc] == 'p' && s[loc + 1] == 'r')
        {
          // print
          loc += 6;
          string u = s.substr(loc, s.size() - 1 - loc);
          int i = 0;
          int v = trans(expr(u, i));
          if (pro > 0)
            res.push_back({v, pro});
          assert(i == u.size());
        }
        else
        {
          // assign
          int id = s[loc] - 'a';
          assert(0 <= id && id < 26);
          assert(s[loc + 1] == '<' && s[loc + 2] == '-');
          loc += 3;
          string u = s.substr(loc, s.size() - loc);
          int i = 0;
          auto a = expr(u, i);
          assert(i == u.size());
          modint sum = 0;
          rep(j, 27) if (j != id) sum += a[j];
          {
            mat A = {{a[id], sum}, {0, 1}};
            A = mtpow(A, pro);
            val[id] = A[0][0] * (modint)val[id] + A[0][1];
          }
        }
      }
    }
    else
    {
      // assign
      int id = s[0] - 'a';
      string u = s.substr(3, s.size() - 3);
      int i = 0;
      int x = trans(expr(u, i));
      val[id] = x;
      assert(i == u.size());
    }
    // cout << "fin " << s << "\n";
  }
  return res;
}
vector<LP> ttt(vector<P> v)
{
  vector<LP> res;
  rep(i, v.size())
  {
    ll sum = v[i].second;
    while (i + 1 < v.size() && v[i].first == v[i + 1].first)
    {
      i++;
      sum += v[i].second;
    }
    res.push_back({v[i].first, sum});
  }
  return res;
}
void solve()
{
  vector<string> vs(n), vt(m);
  rep(i, n) cin >> vs[i];
  rep(i, m) cin >> vt[i];
  vector<P> cs = calc(vs), ct = calc(vt);
  auto s = ttt(cs), t = ttt(ct);
  // cout << "hello s\n";
  // rep(i, s.size())cout << s[i].first << " " << s[i].second << "\n";
  // cout << "hello t\n";
  // rep(i, t.size())cout << t[i].first << " " << t[i].second << "\n";
  if (s == t)
  {
    cout << "Yes\n";
  }
  else
  {
    cout << "No\n";
  }
}

signed main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout << fixed << setprecision(10);
  // init_f();
  // init();
  // while(true)
  // expr();
  // int t; cin >> t; rep(i, t)
  while (cin >> n >> m, n)
    solve();
  return 0;
}
accuracy: 0.02631578947368421
time_ms: 10
memory_kb: 11592
final_rank: 8

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <queue>
#include <ciso646>
#include <random>
#include <map>
#include <set>
#include <bitset>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <cassert>
#include <complex>
#include <numeric>
#include <array>
#include <chrono>
using namespace std;

#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
// constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int> P;

#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define all(v) (v).begin(), (v).end()
typedef pair<ll, ll> LP;

template <typename T>
void chmin(T &a, T b)
{
  a = min(a, b);
}
template <typename T>
void chmax(T &a, T b)
{
  a = max(a, b);
}
template <typename T>
void cinarray(vector<T> &v)
{
  rep(i, v.size()) cin >> v[i];
}
template <typename T>
void coutarray(vector<T> &v)
{
  rep(i, v.size())
  {
    if (i > 0)
      cout << " ";
    cout << v[i];
  }
  cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod)
{
  if (n < 0)
  {
    ll res = mod_pow(x, -n, m);
    return mod_pow(res, m - 2, m);
  }
  if (abs(x) >= m)
    x %= m;
  if (x < 0)
    x += m;
  // if (x == 0)return 0;
  ll res = 1;
  while (n)
  {
    if (n & 1)
      res = res * x % m;
    x = x * x % m;
    n >>= 1;
  }
  return res;
}
struct modint
{
  int n;
  modint() : n(0) { ; }
  modint(ll m)
  {
    if (m < 0 || mod <= m)
    {
      m %= mod;
      if (m < 0)
        m += mod;
    }
    n = m;
  }
  operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint &a, modint b)
{
  a.n += b.n;
  if (a.n >= mod)
    a.n -= mod;
  return a;
}
modint operator-=(modint &a, modint b)
{
  a.n -= b.n;
  if (a.n < 0)
    a.n += mod;
  return a;
}
modint operator*=(modint &a, modint b)
{
  a.n = ((ll)a.n * b.n) % mod;
  return a;
}
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n)
{
  if (n == 0)
    return modint(1);
  modint res = (a * a) ^ (n / 2);
  if (n % 2)
    res = res * a;
  return res;
}

ll inv(ll a, ll p)
{
  return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint &a, modint b)
{
  a = a / b;
  return a;
}
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f()
{
  fact[0] = modint(1);
  for (int i = 0; i < max_n - 1; i++)
  {
    fact[i + 1] = fact[i] * modint(i + 1);
  }
  factinv[max_n - 1] = modint(1) / fact[max_n - 1];
  for (int i = max_n - 2; i >= 0; i--)
  {
    factinv[i] = factinv[i + 1] * modint(i + 1);
  }
}
modint comb(int a, int b)
{
  if (a < 0 || b < 0 || a < b)
    return 0;
  return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b)
{
  if (a < 0 || b < 0 || a < b)
    return 0;
  return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b)
{
  a = abs(a);
  b = abs(b);
  if (a < b)
    swap(a, b);
  while (b)
  {
    ll r = a % b;
    a = b;
    b = r;
  }
  return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template <typename T>
void addv(vector<T> &v, int loc, T val)
{
  if (loc >= v.size())
    v.resize(loc + 1, 0);
  v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
  fill(isp + 2, isp + mn, true);
  for (int i = 2; i < mn; i++) {
    if (!isp[i])continue;
    ps.push_back(i);
    for (int j = 2 * i; j < mn; j += i) {
      isp[j] = false;
    }
  }
}*/

//[,val)
template <typename T>
auto prev_itr(set<T> &st, T val)
{
  auto res = st.lower_bound(val);
  if (res == st.begin())
    return st.end();
  res--;
  return res;
}

//[val,)
template <typename T>
auto next_itr(set<T> &st, T val)
{
  auto res = st.lower_bound(val);
  return res;
}
using mP = pair<modint, modint>;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
//-----------------------------------------

typedef vector<vector<modint>> mat;
typedef vector<modint> vec;
mat mtmul(mat &A, mat &B)
{
  mat C(A.size(), vec(B[0].size()));
  rep(i, (int)A.size())
  {
    rep(k, (int)B.size())
    {
      rep(j, (int)B[0].size())
      {
        C[i][j] += A[i][k] * B[k][j];
      }
    }
  }
  return C;
}
mat mtpow(mat A, ll n)
{
  mat B(A.size(), vec(A.size()));
  rep(i, (int)A.size())
  {
    B[i][i] = 1;
  }
  while (n > 0)
  {
    if (n & (ll)1)
      B = mtmul(B, A);
    A = mtmul(A, A);
    n >>= 1;
  }
  return B;
}

int n, m;

modint val[26];

using ar = array<modint, 27>;

ar term(string &s, int &i)
{
  ar res;
  rep(j, 27) res[j] = 0;
  int coef = 1;
  if (s[i] == '-')
  {
    coef *= -1;
    i++;
  }
  if ('0' <= s[i] && s[i] <= '9')
  {
    modint cur = 0;
    while (i < s.size() && '0' <= s[i] && s[i] <= '9')
    {
      cur = (modint)10 * cur + (modint)(s[i] - '0');
      i++;
    }
    res[26] = cur;
  }
  else
  {
    assert('a' <= s[i] && s[i] <= 'z');
    res[s[i] - 'a'] = 1;
    i++;
  }
  rep(j, 27) res[j] *= coef;
  return res;
}
ar expr(string &s, int &i)
{
  ar res = term(s, i);
  while (i < s.size() && (s[i] == '+' || s[i] == '-'))
  {
    int coef = 1;
    if (s[i] == '-')
    {
      coef = -1;
    }
    i++;
    ar val = term(s, i);
    rep(j, 27) res[j] += val[j] * (modint)coef;
  }
  return res;
}
modint trans(ar a)
{
  modint res = a[26];
  rep(i, 26) res += a[i] * (modint)val[i];
  return res;
}

vector<P> calc(vector<string> vs)
{
  vector<P> res;
  rep(i, 26) val[i] = 0;
  for (auto s : vs)
  {
    if (s.size() == 1 && 'a' <= s[0] && s[0] <= 'z')
    {
      // declear
      val[s[0] - 'a'] = 0;
    }
    else if (s.size() >= 2 && s[0] == 'p' && s[1] == 'r')
    {
      // print
      string u = s.substr(6, s.size() - 6 - 1);
      int i = 0;
      int v = trans(expr(u, i));
      res.push_back({v, 1});
      assert(i == u.size());
    }
    else if (s.size() >= 2 && s[0] == 'f' && s[1] == 'o')
    {
      // for
      ll pro = 1;
      int loc = 0;
      while (true)
      {
        if (loc + 1 < s.size() && s[loc] == 'f' && s[loc + 1] == 'o')
        {
          // for(
          loc += 4;

          int ri = loc;
          while (s[ri] != ')')
            ri++;
          string u = s.substr(loc, ri - loc);
          int i = 0;
          pro *= trans(expr(u, i));
          assert(i == u.size());

          loc = ri;
          //)
          loc++;
        }
        else
          break;
      }
      if (pro > 0)
      {
        if (s[loc] == 'p' && s[loc + 1] == 'r')
        {
          // print
          loc += 6;
          string u = s.substr(loc, s.size() - 1 - loc);
          int i = 0;
          int v = trans(expr(u, i));
          res.push_back({v, pro});
          assert(i == u.size());
        }
        else
        {
          // assign
          int id = s[loc] - 'a';
          assert(0 <= id && id < 26);
          loc += 3;
          string u = s.substr(loc, s.size() - loc);
          int i = 0;
          auto a = expr(u, i);
          assert(i == u.size());
          modint sum = 0;
          rep(i, 27) if (i != id) sum += a[i] * val[i];
          // cout << "e? " << a[0] << "\n";
          // cout << "?? " << u << "\n";
          mat A = {{a[id], sum}, {0, 1}};
          A = mtpow(A, pro);
          val[id] = A[0][0] * (modint)val[id] + A[0][1];
        }
      }
    }
    else
    {
      // assign
      int v = s[0] - 'a';
      string u = s.substr(3, s.size() - 3);
      int i = 0;
      int x = trans(expr(u, i));
      val[v] = x;
      assert(i == u.size());
    }
    // cout << "fin " << s << "\n";
  }
  return res;
}
vector<LP> ttt(vector<P> v)
{
  vector<LP> res;
  rep(i, v.size())
  {
    ll sum = v[i].second;
    while (i + 1 < v.size() && v[i].first == v[i + 1].first)
    {
      sum += v[i + 1].second;
      i++;
    }
    res.push_back({v[i].first, sum});
  }
  return res;
}
void solve()
{
  vector<string> vs(n), vt(m);
  rep(i, n) cin >> vs[i];
  rep(i, m) cin >> vt[i];
  vector<P> cs = calc(vs), ct = calc(vt);
  auto s = ttt(cs), t = ttt(ct);
  // cout << "hello s\n";
  // rep(i, s.size())cout << s[i].first << " " << s[i].second << "\n";
  // cout << "hello t\n";
  // rep(i, t.size())cout << t[i].first << " " << t[i].second << "\n";
  if (s == t)
  {
    cout << "Yes\n";
  }
  else
  {
    cout << "No\n";
  }
}

signed main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout << fixed << setprecision(10);
  // init_f();
  // init();
  // while(true)
  // expr();
  // int t; cin >> t; rep(i, t)
  while (cin >> n >> m, n)
    solve();
  return 0;
}
accuracy: 0.02631578947368421
time_ms: 10
memory_kb: 19856
final_rank: 18

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
//constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

template<typename T>
void chmin(T& a, T b) {
	a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
	a = max(a, b);
}
template<typename T>
void cinarray(vector<T>& v) {
	rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
	rep(i, v.size()) {
		if (i > 0)cout << " "; cout << v[i];
	}
	cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	//if (x == 0)return 0;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	int n;
	modint() :n(0) { ; }
	modint(ll m) {
		if (m < 0 || mod <= m) {
			m %= mod; if (m < 0)m += mod;
		}
		n = m;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template<typename T>
void addv(vector<T>& v, int loc, T val) {
	if (loc >= v.size())v.resize(loc + 1, 0);
	v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
	fill(isp + 2, isp + mn, true);
	for (int i = 2; i < mn; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j < mn; j += i) {
			isp[j] = false;
		}
	}
}*/

//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	if (res == st.begin())return st.end();
	res--; return res;
}

//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	return res;
}
using mP = pair<modint, modint>;


int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
//-----------------------------------------

typedef vector<vector<modint>> mat;
typedef vector<modint> vec;
mat mtmul(mat& A, mat& B) {
	mat C(A.size(), vec(B[0].size()));
	rep(i, (int)A.size()) {
		rep(k, (int)B.size()) {
			rep(j, (int)B[0].size()) {
				C[i][j] += A[i][k] * B[k][j];
			}
		}
	}
	return C;
}
mat mtpow(mat A, ll n) {
	mat B(A.size(), vec(A.size()));
	rep(i, (int)A.size()) {
		B[i][i] = 1;
	}
	while (n > 0) {
		if (n & (ll)1)B = mtmul(B, A);
		A = mtmul(A, A);
		n >>= 1;
	}
	return B;
}

int n, m;

modint val[26];

using ar = array<modint, 27>;

ar term(string& s, int& i) {
	ar res;
	rep(j, 27)res[j] = 0;
	int coef = 1;
	while(s[i] == '-') {
		coef *= -1;
		i++;
	}
	if ('0' <= s[i] && s[i] <= '9') {
		modint cur = 0;
		while (i < s.size() && '0' <= s[i] && s[i] <= '9') {
			cur = (modint)10 * cur + (modint)(s[i] - '0');
			i++;
		}
		res[26] = cur;
	}
	else {
		assert('a' <= s[i] && s[i] <= 'z');
		res[s[i] - 'a'] = 1;
		i++;
	}
	rep(j, 27)res[j] *= coef;
	return res;
}
ar expr(string& s, int& i) {
	ar res = term(s, i);
	while (i < s.size() && (s[i] == '+' || s[i] == '-')) {
		int coef = 1;
		if (s[i] == '-') {
			coef = -1;
		}
		i++;
		ar val = term(s, i);
		rep(j, 27)res[j] += val[j] * (modint)coef;
	}
	return res;
}
modint trans(ar a) {
	modint res = a[26];
	rep(i, 26)res += a[i] * (modint)val[i];
	return res;
}

vector<P> calc(vector<string> vs) {
	vector<P> res;
	rep(i, 26)val[i] = 0;
	for (auto s : vs) {
		if (s.size() == 1 && 'a' <= s[0] && s[0] <= 'z') {
			//declear
			val[s[0] - 'a'] = 0;
		}
		else if (s.size() >= 2 && s[0] == 'p' && s[1] == 'r') {
			//print
			string u = s.substr(6, s.size() - 6 - 1);
			int i = 0;
			int v = trans(expr(u, i));
			res.push_back({ v,1 });
			assert(i == u.size());
		}
		else if (s.size() >= 2 && s[0] == 'f' && s[1] == 'o') {
			//for
			int pro = 1;
			int loc = 0;
			while (true) {
				if (loc + 1 < s.size() && s[loc] == 'f' && s[loc + 1] == 'o') {
					//for(
					loc += 4;

					int ri = loc;
					while (s[ri] != ')')ri++;
					string u = s.substr(loc, ri - loc);
					int i = 0;
					pro *= trans(expr(u, i));
					assert(i == u.size());

					loc = ri;
					//)
					loc++;
				}
				else break;
			}
			if (pro > 0) {
				if (s[loc] == 'p' && s[loc + 1] == 'r') {
					//print
					loc += 6;
					string u = s.substr(loc, s.size() - 1 - loc);
					int i = 0;
					int v = trans(expr(u, i));
					if (pro > 0)res.push_back({ v,pro });
					assert(i == u.size());
				}
				else {
					//assign
					int id = s[loc] - 'a';
					assert(0 <= id && id < 26);
					assert(s[loc + 1] == '<' && s[loc + 2] == '-');
					loc += 3;
					string u = s.substr(loc, s.size() - loc);
					int i = 0;
					auto a = expr(u, i);
					assert(i == u.size());
					modint sum = 0;
					rep(j, 27)if (j != id)sum += a[j];
					//cout << "e? " << a[0] << "\n";
					//cout << "?? " << u << "\n";

					{
						mat A = { {a[id],sum},{0,1} };
						A = mtpow(A, pro);
						val[id] = A[0][0] * (modint)val[id] + A[0][1];
					}
					/*{
						if (a[id] == (modint)1) {
							val[id] += (modint)pro * sum;
						}
						else {
							modint p = a[id];
							modint q = sum;
							modint x = mod_pow(p, pro) - 1;
							x /= (p - (modint)1);
							x *= q;
							x += val[id] * (modint)mod_pow(p, pro);
							val[id] = x;
						}
					}*/
				}
			}
		}
		else {
			//assign
			int id = s[0] - 'a';
			string u = s.substr(3, s.size() - 3);
			int i = 0;
			int x = trans(expr(u, i));
			val[id] = x;
			assert(i == u.size());
		}
		//cout << "fin " << s << "\n";
	}
	return res;
}
vector<LP> ttt(vector<P> v) {
	vector<LP> res;
	rep(i, v.size()) {
		ll sum = v[i].second;
		while (i + 1 < v.size() && v[i].first == v[i + 1].first) {
			i++; sum += v[i].second;
		}
		res.push_back({ v[i].first,sum });
	}
	return res;
}
void solve() {
	vector<string> vs(n), vt(m);
	rep(i, n)cin >> vs[i];
	rep(i, m)cin >> vt[i];
	vector<P> cs = calc(vs), ct = calc(vt);
	auto s = ttt(cs), t = ttt(ct);
	//cout << "hello s\n";
	//rep(i, s.size())cout << s[i].first << " " << s[i].second << "\n";
	//cout << "hello t\n";
	//rep(i, t.size())cout << t[i].first << " " << t[i].second << "\n";
	if (s == t) {
		cout << "Yes\n";
	}
	else {
		cout << "No\n";
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//init_f();
	//init();
	//while(true)
	//expr();
	//int t; cin >> t; rep(i, t)
	while (cin >> n >> m, n)
		solve();
	return 0;
}
accuracy: 0.02631578947368421
time_ms: 10
memory_kb: 11584
final_rank: 7

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
//constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

template<typename T>
void chmin(T& a, T b) {
	a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
	a = max(a, b);
}
template<typename T>
void cinarray(vector<T>& v) {
	rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
	rep(i, v.size()) {
		if (i > 0)cout << " "; cout << v[i];
	}
	cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	//if (x == 0)return 0;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	int n;
	modint() :n(0) { ; }
	modint(ll m) {
		if (m < 0 || mod <= m) {
			m %= mod; if (m < 0)m += mod;
		}
		n = m;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template<typename T>
void addv(vector<T>& v, int loc, T val) {
	if (loc >= v.size())v.resize(loc + 1, 0);
	v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
	fill(isp + 2, isp + mn, true);
	for (int i = 2; i < mn; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j < mn; j += i) {
			isp[j] = false;
		}
	}
}*/

//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	if (res == st.begin())return st.end();
	res--; return res;
}

//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	return res;
}
using mP = pair<modint, modint>;


int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
//-----------------------------------------

typedef vector<vector<modint>> mat;
typedef vector<modint> vec;
mat mtmul(mat& A, mat& B) {
	mat C(A.size(), vec(B[0].size()));
	rep(i, (int)A.size()) {
		rep(k, (int)B.size()) {
			rep(j, (int)B[0].size()) {
				C[i][j] += A[i][k] * B[k][j];
			}
		}
	}
	return C;
}
mat mtpow(mat A, ll n) {
	mat B(A.size(), vec(A.size()));
	rep(i, (int)A.size()) {
		B[i][i] = 1;
	}
	while (n > 0) {
		if (n & (ll)1)B = mtmul(B, A);
		A = mtmul(A, A);
		n >>= 1;
	}
	return B;
}

int n, m;

modint val[26];

using ar = array<modint, 27>;

ar term(string& s, int& i) {
	ar res;
	rep(j, 27)res[j] = 0;
	int coef = 1;
	while(s[i] == '-') {
		coef *= -1;
		i++;
	}
	if ('0' <= s[i] && s[i] <= '9') {
		modint cur = 0;
		while (i < s.size() && '0' <= s[i] && s[i] <= '9') {
			cur = (modint)10 * cur + (modint)(s[i] - '0');
			i++;
		}
		res[26] = cur;
	}
	else {
		assert('a' <= s[i] && s[i] <= 'z');
		res[s[i] - 'a'] = 1;
		i++;
	}
	rep(j, 27)res[j] *= coef;
	return res;
}
ar expr(string& s, int& i) {
	ar res = term(s, i);
	while (i < s.size() && (s[i] == '+' || s[i] == '-')) {
		int coef = 1;
		if (s[i] == '-') {
			coef = -1;
		}
		i++;
		ar val = term(s, i);
		rep(j, 27)res[j] += val[j] * (modint)coef;
	}
	return res;
}
modint trans(ar a) {
	modint res = a[26];
	rep(i, 26)res += a[i] * (modint)val[i];
	return res;
}

vector<P> calc(vector<string> vs) {
	vector<P> res;
	//rep(i, 26)val[i] = 0;
	for (auto s : vs) {
		if (s.size() == 1 && 'a' <= s[0] && s[0] <= 'z') {
			//declear
			val[s[0] - 'a'] = 0;
		}
		else if (s.size() >= 2 && s[0] == 'p' && s[1] == 'r') {
			//print
			string u = s.substr(6, s.size() - 6 - 1);
			int i = 0;
			int v = trans(expr(u, i));
			res.push_back({ v,1 });
			assert(i == u.size());
		}
		else if (s.size() >= 2 && s[0] == 'f' && s[1] == 'o') {
			//for
			int pro = 1;
			int loc = 0;
			while (true) {
				if (loc + 1 < s.size() && s[loc] == 'f' && s[loc + 1] == 'o') {
					//for(
					loc += 4;

					int ri = loc;
					while (s[ri] != ')')ri++;
					string u = s.substr(loc, ri - loc);
					int i = 0;
					pro *= trans(expr(u, i));
					assert(i == u.size());

					loc = ri;
					//)
					loc++;
				}
				else break;
			}
			if (pro > 0) {
				if (s[loc] == 'p' && s[loc + 1] == 'r') {
					//print
					loc += 6;
					string u = s.substr(loc, s.size() - 1 - loc);
					int i = 0;
					int v = trans(expr(u, i));
					if (pro > 0)res.push_back({ v,pro });
					assert(i == u.size());
				}
				else {
					//assign
					int id = s[loc] - 'a';
					assert(0 <= id && id < 26);
					assert(s[loc + 1] == '<' && s[loc + 2] == '-');
					loc += 3;
					string u = s.substr(loc, s.size() - loc);
					int i = 0;
					auto a = expr(u, i);
					assert(i == u.size());
					modint sum = 0;
					rep(j, 27)if (j != id)sum += a[j];
					//cout << "e? " << a[0] << "\n";
					//cout << "?? " << u << "\n";

					{
						mat A = { {a[id],sum},{0,1} };
						A = mtpow(A, pro);
						val[id] = A[0][0] * (modint)val[id] + A[0][1];
					}
					/*{
						if (a[id] == (modint)1) {
							val[id] += (modint)pro * sum;
						}
						else {
							modint p = a[id];
							modint q = sum;
							modint x = mod_pow(p, pro) - 1;
							x /= (p - (modint)1);
							x *= q;
							x += val[id] * (modint)mod_pow(p, pro);
							val[id] = x;
						}
					}*/
				}
			}
		}
		else {
			//assign
			int id = s[0] - 'a';
			string u = s.substr(3, s.size() - 3);
			int i = 0;
			int x = trans(expr(u, i));
			val[id] = x;
			assert(i == u.size());
		}
		//cout << "fin " << s << "\n";
	}
	return res;
}
vector<LP> ttt(vector<P> v) {
	vector<LP> res;
	rep(i, v.size()) {
		ll sum = v[i].second;
		while (i + 1 < v.size() && v[i].first == v[i + 1].first) {
			i++; sum += v[i].second;
		}
		res.push_back({ v[i].first,sum });
	}
	return res;
}
void solve() {
	vector<string> vs(n), vt(m);
	rep(i, n)cin >> vs[i];
	rep(i, m)cin >> vt[i];
	vector<P> cs = calc(vs), ct = calc(vt);
	auto s = ttt(cs), t = ttt(ct);
	//cout << "hello s\n";
	//rep(i, s.size())cout << s[i].first << " " << s[i].second << "\n";
	//cout << "hello t\n";
	//rep(i, t.size())cout << t[i].first << " " << t[i].second << "\n";
	if (s == t) {
		cout << "Yes\n";
	}
	else {
		cout << "No\n";
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//init_f();
	//init();
	//while(true)
	//expr();
	//int t; cin >> t; rep(i, t)
	while (cin >> n >> m, n)
		solve();
	return 0;
}
accuracy: 0.02631578947368421
time_ms: 10
memory_kb: 11720
final_rank: 13