#include <bits/stdc++.h>
using namespace std;
bool f1[55][55][30], f2[55][55][30], can[30][30][30];
int dp[55][55];
char s1[55], s2[55];
vector<pair<int, int> > cn[30];
void init() {
  for (int i = 0; i < 26; i++) cn[i].clear();
  memset(f1, 0, sizeof(f1));
  memset(f2, 0, sizeof(f2));
  memset(can, 0, sizeof(can));
}
void pre(bool f[55][55][30], char s[]) {
  int len = strlen(s);
  for (int i = 0; i < len; i++) s[i] -= 'a';
  for (int i = 0; i < len; i++) f[i][i][s[i]] = 1;
  for (int i = 0; i < len - 1; i++) {
    for (int j = 0; j < 26; j++) {
      if (can[j][s[i]][s[i + 1]]) f[i][i + 1][j] = 1;
    }
  }
  for (int k = 2; k < len; k++) {
    for (int i = 0; i + k < len; i++) {
      int j = i + k;
      for (int a = 0; a < 26; a++) {
        for (int t = i; t < j; t++) {
          bool ok = 0;
          for (int cnt = 0; cnt < cn[a].size(); cnt++) {
            int l = cn[a][cnt].first, r = cn[a][cnt].second;
            if (f[i][t][l] && f[t + 1][j][r]) {
              ok = 1;
              f[i][j][a] = 1;
              break;
            }
          }
          if (ok) break;
        }
      }
    }
  }
}
int solve(int len1, int len2) {
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < len1; i++) {
    for (int j = 0; j < len2; j++) {
      for (int a = 0; a <= i; a++) {
        for (int b = 0; b <= j; b++) {
          if ((a == 0 && b != 0) || (a != 0 && b == 0)) continue;
          bool ok = 0;
          for (int cnt = 0; cnt < 26; cnt++) {
            if (f1[a][i][cnt] && f2[b][j][cnt]) {
              ok = 1;
              break;
            }
          }
          if (ok) {
            if (a != 0 && b != 0) {
              if (dp[a - 1][b - 1] != -1) {
                if (dp[i][j] == -1)
                  dp[i][j] = dp[a - 1][b - 1] + 1;
                else
                  dp[i][j] = min(dp[i][j], dp[a - 1][b - 1] + 1);
              }
            } else
              dp[i][j] = 1;
          }
        }
      }
    }
  }
  return dp[len1 - 1][len2 - 1];
}
int main() {
  while (~scanf("%s%s", s1, s2)) {
    init();
    int n;
    char tmp[10];
    int len1 = strlen(s1), len2 = strlen(s2);
    scanf("%d", &n);
    gets(tmp);
    while (n--) {
      char a, b, c;
      scanf("%c->%c%c", &a, &b, &c);
      gets(tmp);
      a -= 'a';
      b -= 'a';
      c -= 'a';
      can[a][b][c] = 1;
      cn[a].push_back(make_pair(b, c));
    }
    pre(f1, s1);
    pre(f2, s2);
    printf("%d\n", solve(len1, len2));
  }
  return 0;
}
