#include <bits/stdc++.h>
using namespace std;
string s1, s2;
vector<vector<int>> dp;
map<pair<string, char>, bool> deriveDp;
map<char, vector<string>> rules;
bool derives(char c, string str) {
  pair<string, char> index = make_pair(str, c);
  if (deriveDp.count(index)) {
    return deriveDp[index];
  }
  if (str.size() == 1 && str[0] == c)
    return true;
  else {
    if (!rules.count(c)) return false;
    for (const string &s : rules[c]) {
      for (int x = 0; x < str.size() - 1; x++) {
        if (derives(s[0], str.substr(0, x + 1)) &&
            derives(s[1], str.substr(x + 1))) {
          deriveDp[index] = true;
          return true;
        }
      }
    }
    deriveDp[index] = false;
    return false;
  }
}
int shortestCommon(int i, int j) {
  if (dp[i][j] != -1) {
    return dp[i][j];
  }
  int minL = 100000;
  for (int x = 'a'; x <= 'z'; x++) {
    if (derives(x, s1.substr(0, i)) && derives(x, s2.substr(0, j))) {
      dp[i][j] = 1;
      return 1;
    }
    for (int y = 1; y < i; y++) {
      for (int z = 1; z < j; z++) {
        int common = shortestCommon(y, z);
        if (common == 100000) continue;
        if (derives(x, s1.substr(y, i - y)) &&
            derives(x, s2.substr(z, j - z))) {
          minL = min(minL, common + 1);
        }
      }
    }
  }
  dp[i][j] = minL;
  return minL;
}
int main() {
  int n;
  cin >> s1 >> s2 >> n;
  for (int x = 0; x < n; x++) {
    string rule;
    cin >> rule;
    char ai = rule[0];
    string bc = rule.substr(3);
    if (rules.count(ai)) {
      vector<string> temp = rules[ai];
      temp.push_back(bc);
      rules[ai] = temp;
    } else {
      rules[ai] = vector<string>{bc};
    }
  }
  dp = vector<vector<int>>(s1.size() + 1, vector<int>(s2.size() + 1, -1));
  int ans = shortestCommon(s1.size(), s2.size());
  if (ans == 100000) ans = -1;
  cout << ans << endl;
}
