#include <bits/stdc++.h>
using namespace std;
template <typename TF>
void debug(ostream &out, TF const &f) {
  out << f << std::endl;
}
template <typename TF, typename... TR>
void debug(ostream &out, TF const &f, TR const &...rest) {
  out << f << " ";
  debug(out, rest...);
}
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using vi = vector<int>;
using vii = vector<pii>;
using vll = vector<ll>;
using vs = vector<string>;
using pcc = pair<int, int>;
const int DR[] = {-1, 0, 1, 0, -1, 1, 1, -1};
const int DC[] = {0, -1, 0, 1, -1, -1, 1, 1};
const double PI = acos(-1.0);
const double EPS = 1e-9;
const int MOD = 1e9 + 7;
const int INF = 1073741823;
const ll INFLL = 4e18;
const int MAX = 50;
const int CMAX = 26;
int len[2], str[2][MAX + 5];
map<int, vii> sub;
bool dp0[2][MAX + 5][MAX + 5][CMAX + 5];
int dp1[MAX + 5][MAX + 5];
inline int get(const char &c) { return c - 'a'; }
void read() {
  string tmp;
  for (int t = 0; t < (int)2; t++) {
    cin >> tmp;
    len[t] = (int)tmp.size();
    for (int i = 0; i < (int)len[t]; i++) str[t][i] = get(tmp[i]);
  }
  int k;
  cin >> k;
  while (k--) {
    cin >> tmp;
    int a = get(tmp[0]), b = get(tmp[3]), c = get(tmp[4]);
    sub[a].push_back({b, c});
  }
}
void solve() {
  for (int t = 0; t < (int)2; t++) {
    for (int i = 0; i < (int)len[t]; i++) dp0[t][i][i][str[t][i]] = true;
    for (int l = (int)2; l <= (int)len[t]; l++) {
      for (int i = 0; i + l - 1 < len[t]; i++) {
        int j = i + l - 1;
        for (int chr = 0; chr < (int)CMAX; chr++) {
          bool flag = false;
          for (int bound = (int)i; bound <= (int)j - 1; bound++) {
            for (pii p : sub[chr]) {
              int a = p.first, b = p.second;
              if (dp0[t][i][bound][a] && dp0[t][bound + 1][j][b]) {
                flag = true;
                break;
              }
            }
            if (flag) break;
          }
          if (flag) dp0[t][i][j][chr] = true;
        }
      }
    }
  }
  for (int i = 0; i < (int)MAX + 3; i++)
    for (int j = 0; j < (int)MAX + 3; j++) dp1[i][j] = INF;
  for (int i = 0; i < (int)len[0]; i++)
    for (int j = 0; j < (int)len[1]; j++)
      for (int i0 = (int)0; i0 <= (int)i; i0++)
        for (int j0 = (int)0; j0 <= (int)j; j0++)
          for (int chr = 0; chr < (int)CMAX; chr++) {
            if (i0 == 0 || j0 == 0) {
              if (i0 == 0 && j0 == 0)
                if (dp0[0][0][i][chr] && dp0[1][0][j][chr]) dp1[i][j] = 1;
              continue;
            }
            if (dp0[0][i0][i][chr] && dp0[1][j0][j][chr]) {
              dp1[i][j] = min(dp1[i][j], 1 + dp1[i0 - 1][j0 - 1]);
            }
          }
  int ans = dp1[len[0] - 1][len[1] - 1];
  cout << (ans > 50 ? -1 : ans) << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int TC = 1;
  for (int tc = (int)1; tc <= (int)TC; tc++) {
    read();
    solve();
  }
}
