#include <bits/stdc++.h>
using namespace std;
const unsigned long long BIGEST = 1000000000000000000 + 1000000000000000000;
const long long BIGER = 1000000000000000000;
const int BIG = 1000000000;
const int MOD = 19260817;
string st1, st2;
int n;
int dp2[60][60][257];
int dp3[60][60][257];
int dp[60][60];
vector<pair<char, char> > vt1[27];
vector<char> vt2[10000];
int main() {
  cin >> st1 >> st2;
  for (int i = 0; i < st1.size(); i++) st1[i] -= 'a';
  for (int i = 0; i < st2.size(); i++) st2[i] -= 'a';
  cin >> n;
  string st;
  for (int i = 1; i <= n; i++) {
    cin >> st;
    st[0] -= 'a';
    st[3] -= 'a';
    st[4] -= 'a';
    vt1[st[0]].push_back(make_pair(st[3], st[4]));
    vt2[st[3] * 27 + st[4]].push_back(st[0]);
  }
  for (int i = 1; i < st1.size(); i++) {
    dp2[i][i - 1][0] = 1;
    dp2[i][i - 1][1] = 1;
    dp2[i][i - 1][2] = 1;
    dp2[i][i - 1][3] = 1;
    dp2[i][i - 1][4] = 1;
    dp2[i][i - 1][5] = 1;
    dp2[i][i - 1][6] = 1;
    dp2[i][i - 1][7] = 1;
    dp2[i][i - 1][8] = 1;
    dp2[i][i - 1][9] = 1;
    dp2[i][i - 1][10] = 1;
    dp2[i][i - 1][11] = 1;
    dp2[i][i - 1][12] = 1;
    dp2[i][i - 1][13] = 1;
    dp2[i][i - 1][14] = 1;
    dp2[i][i - 1][15] = 1;
    dp2[i][i - 1][16] = 1;
    dp2[i][i - 1][17] = 1;
    dp2[i][i - 1][18] = 1;
    dp2[i][i - 1][19] = 1;
    dp2[i][i - 1][20] = 1;
    dp2[i][i - 1][21] = 1;
    dp2[i][i - 1][22] = 1;
    dp2[i][i - 1][23] = 1;
    dp2[i][i - 1][24] = 1;
    dp2[i][i - 1][25] = 1;
  }
  for (int i = 0; i < st1.size(); i++) {
    for (int j = i; j >= 0; j--) {
      if (i - j == 0) {
        dp2[j][i][st1[i]] = 1;
      } else if (i - j == 1) {
        for (int k = 0; k < vt2[st1[j] * 27 + st1[i]].size(); k++) {
          dp2[j][i][vt2[st1[j] * 27 + st1[i]][k]] = 1;
        }
      } else {
        for (int k = 0; k < 26; k++) {
          for (int mid = j; mid < i; mid++)
            for (int q = 0; q < vt1[k].size(); q++) {
              dp2[j][i][k] |= dp2[j][mid][vt1[k][q].first] &&
                              dp2[mid + 1][i][vt1[k][q].second];
            }
        }
      }
    }
  }
  for (int i = 1; i < st2.size(); i++) {
    dp3[i][i - 1][0] = 1;
    dp3[i][i - 1][1] = 1;
    dp3[i][i - 1][2] = 1;
    dp3[i][i - 1][3] = 1;
    dp3[i][i - 1][4] = 1;
    dp3[i][i - 1][5] = 1;
    dp3[i][i - 1][6] = 1;
    dp3[i][i - 1][7] = 1;
    dp3[i][i - 1][8] = 1;
    dp3[i][i - 1][9] = 1;
    dp3[i][i - 1][10] = 1;
    dp3[i][i - 1][11] = 1;
    dp3[i][i - 1][12] = 1;
    dp3[i][i - 1][13] = 1;
    dp3[i][i - 1][14] = 1;
    dp3[i][i - 1][15] = 1;
    dp3[i][i - 1][16] = 1;
    dp3[i][i - 1][17] = 1;
    dp3[i][i - 1][18] = 1;
    dp3[i][i - 1][19] = 1;
    dp3[i][i - 1][20] = 1;
    dp3[i][i - 1][21] = 1;
    dp3[i][i - 1][22] = 1;
    dp3[i][i - 1][23] = 1;
    dp3[i][i - 1][24] = 1;
    dp3[i][i - 1][25] = 1;
  }
  for (int i = 0; i < st2.size(); i++) {
    for (int j = i; j >= 0; j--) {
      if (i - j == 0) {
        dp3[j][i][st2[i]] = 1;
      } else if (i - j == 1) {
        for (int k = 0; k < vt2[st2[j] * 27 + st2[i]].size(); k++) {
          dp3[j][i][vt2[st2[j] * 27 + st2[i]][k]] = 1;
        }
      } else {
        for (int k = 0; k < 26; k++) {
          for (int mid = j; mid < i; mid++)
            for (int q = 0; q < vt1[k].size(); q++) {
              dp3[j][i][k] |= dp3[j][mid][vt1[k][q].first] &&
                              dp3[mid + 1][i][vt1[k][q].second];
            }
        }
      }
    }
  }
  for (int i = 0; i < st1.size(); i++) {
    for (int j = 0; j < st2.size(); j++) {
      for (int c = 0; c < 26; c++) {
        if (dp2[0][i][c] && dp3[0][j][c]) dp[i][j] = 1;
      }
    }
  }
  for (int i = 0; i < st1.size(); i++) {
    for (int j = 0; j < st2.size(); j++) {
      if (dp[i][j] == 0) dp[i][j] = BIG;
      for (int p = 0; p < i; p++) {
        for (int q = 0; q < j; q++) {
          for (int c = 0; c < 26; c++)
            if (dp2[p + 1][i][c] && dp3[q + 1][j][c])
              dp[i][j] = min(dp[i][j], dp[p][q] + 1);
        }
      }
    }
  }
  if (dp[st1.size() - 1][st2.size() - 1] != BIG)
    printf("%d", dp[st1.size() - 1][st2.size() - 1]);
  else
    puts("-1");
  return 0;
}
