func_code_string: #include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ull = unsigned long long;   using ld = long double;   const ll llinf = (1ll << 61) - 1;   const double eps = 1e-6, ldeps = 1e-9;   template <class C, size_t Y>   struct tp {     void operator()(ostream &os, const C &t) {       tp<C, Y - 1>()(os, t);       os <<  ,   << get<Y - 1>(t);     }   };   template <class C>   struct tp<C, 1> {     void operator()(ostream &os, const C &t) { os << get<0>(t); }   };   template <class... C>   ostream &operator<<(ostream &os, const tuple<C...> &t) {     os <<  { , tp<decltype(t), sizeof...(C)>()(os, t);     return os <<  } ;   }   struct Dbg {     static constexpr auto &os = cout;     template <class C>     static auto dud(C *x) -> decltype(os << *x, 0);     template <class C>     static char dud(...);     template <class C>     typename enable_if<sizeof dud<C>(0) != 1, Dbg &>::type operator<<(         const C &x) {       os << x;       return *this;     }     template <class C>     Dbg &dump(C b, C e) {       *this <<   n[ ;       int cur = 0;       for (C i = b; i != e and cur < 20; i++, cur++)         *this <<  ,   + 2 * (i == b) << *i;       return *this <<  ] ;     }     template <class C>     typename enable_if<sizeof dud<C>(0) == 1, Dbg &>::type operator<<(         const C &x) {       return dump(begin(x), end(x));     }     template <class C, size_t X>     typename enable_if<sizeof(C) != 1, Dbg &>::type operator<<(C (&x)[X]) {       return dump(begin(x), end(x));     }     template <class B, class C>     Dbg &operator<<(const pair<B, C> &x) {       return *this <<  {  << x.first <<  ,   << x.second <<  } ;     }     Dbg &operator<<(ostream &(*x)(std::ostream &)) {       os << x;       return *this;     }   } dbg;   void err(istringstream *iss) { delete iss; }   template <class C, class... Args>   void err(istringstream *iss, const C &val, const Args &...args) {     string name;     *iss >> name;     if (name.back() ==  , ) name.pop_back();     dbg << name <<   =   << val <<  ;  , err(iss, args...);   }   int n, m, K, T, Q, cn;   const int inf = 1000000007, mxn = 53;   char s1[mxn], s2[mxn];   int s1l, s2l, rp[30][30], dp1[mxn][mxn], dp2[mxn][mxn];   int memo[mxn][mxn];   bitset<mxn> vstd[mxn];   int dp(int at1, int at2) {     if (at1 == s1l or at2 == s2l) return at1 == s1l and at2 == s2l ? 0 : inf;     int &ret = memo[at1][at2];     if (vstd[at1][at2]) return ret;     vstd[at1][at2] = 1;     ret = inf;     for (int i = at1; i <= s1l - 1; i++) {       for (int j = at2; j <= s2l - 1; j++) {         if ((dp1[at1][i] & dp2[at2][j])) ret = min(ret, 1 + dp(i + 1, j + 1));       }     }     return ret;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.precision(11);     cin >> s1 >> s2 >> n;     for (int i = 0; i < n; i++) {       string tmp;       cin >> tmp;       rp[tmp[3] -  a ][tmp[4] -  a ] |= 1 << (tmp[0] -  a );     }     s1l = int(strlen(s1)), s2l = int(strlen(s2));     for (int L = 1; L <= s1l; L += 1) {       for (int i = 0, j = i + L - 1; j < s1l; i++, j++) {         if (L == 1) {           dp1[i][j] |= 1 << (s1[i] -  a );           continue;         }         for (int k = i; k <= j - 1; k++) {           for (int l = 0; l < 26; l++) {             if ((dp1[i][k] >> l) & 1)               for (m = 0; m < 26; m++) {                 if ((dp1[k + 1][j] >> m) & 1) dp1[i][j] |= rp[l][m];               }           }         }       }     }     for (int L = 1; L <= s2l; L += 1) {       for (int i = 0, j = i + L - 1; j < s2l; i++, j++) {         if (L == 1) {           dp2[i][j] |= 1 << (s2[i] -  a );           continue;         }         for (int k = i; k <= j - 1; k++) {           for (int l = 0; l < 26; l++) {             if ((dp2[i][k] >> l) & 1)               for (m = 0; m < 26; m++) {                 if ((dp2[k + 1][j] >> m) & 1) dp2[i][j] |= rp[l][m];               }           }         }       }     }     int ans = dp(0, 0);     cout << (ans == inf ? -1 : ans) <<   n ;   }   