#include <bits/stdc++.h>
using namespace std;
const int N = 55;
const int C = 26;
const int INF = int(1e9);
int n, m, p;
int f[N][N], f1[N][N][C], f2[N][N][C];
char s1[N], s2[N], buf[N];
int to[N][3];
inline int id(char x) { return x - 'a'; }
bool fromChar(char s[N], int n, int f[N][N][C], int l, int r, int x) {
  if (f[l][r][x] != -1) {
    return f[l][r][x];
  }
  if (l == r) {
    f[l][r][x] = id(s[l]) == x;
  } else {
    f[l][r][x] = 0;
    for (int cut_pos = l; cut_pos < r && !f[l][r][x]; cut_pos++) {
      for (int i = 0; i < p; i++) {
        if (to[i][0] == x && fromChar(s, n, f, l, cut_pos, to[i][1]) &&
            fromChar(s, n, f, cut_pos + 1, r, to[i][2])) {
          f[l][r][x] = 1;
          break;
        }
      }
    }
  }
  return f[l][r][x];
}
int dp(int n, int m) {
  if (f[n][m] != -1) {
    return f[n][m];
  }
  if (n == 0 || m == 0) {
    return f[n][m] = n == 0 && m == 0 ? 0 : INF;
  }
  f[n][m] = INF;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < C; k++) {
        if (fromChar(s1, n, f1, i + 1, n, k) &&
            fromChar(s2, m, f2, j + 1, m, k)) {
          f[n][m] = min(f[n][m], dp(i, j) + 1);
        }
      }
    }
  }
  return f[n][m];
}
int main() {
  scanf("%s", s1 + 1);
  n = strlen(s1 + 1);
  scanf("%s", s2 + 1);
  m = strlen(s2 + 1);
  scanf("%d", &p);
  for (int i = 0; i < p; i++) {
    scanf("%s", buf);
    to[i][0] = id(buf[0]);
    to[i][1] = id(buf[3]);
    to[i][2] = id(buf[4]);
  }
  memset(f1, -1, sizeof(f1));
  memset(f2, -1, sizeof(f2));
  memset(f, -1, sizeof(f));
  printf("%d\n", dp(n, m) == INF ? -1 : dp(n, m));
}
