Problem: The DNA sequence for every living creature in Berland can be represented as a non-empty line consisting of lowercase Latin letters. Berland scientists found out that all the creatures evolve by stages. During one stage exactly one symbol of the DNA line is replaced by exactly two other ones. At that overall there are n permissible substitutions. The substitution ai->bici means that any one symbol ai can be replaced with two symbols bici. Every substitution could happen an unlimited number of times.

They say that two creatures with DNA sequences s1 and s2 can have a common ancestor if there exists such a DNA sequence s3 that throughout evolution it can result in s1 and s2, perhaps after a different number of stages. Your task is to find out by the given s1 and s2 whether the creatures possessing such DNA sequences can have a common ancestor. If the answer is positive, you have to find the length of the shortest sequence of the common ancestor’s DNA.

Input

The first line contains a non-empty DNA sequence s1, the second line contains a non-empty DNA sequence s2. The lengths of these lines do not exceed 50, the lines contain only lowercase Latin letters. The third line contains an integer n (0 ≤ n ≤ 50) — the number of permissible substitutions. Then follow n lines each of which describes a substitution in the format ai->bici. The characters ai, bi, and ci are lowercase Latin letters. Lines s1 and s2 can coincide, the list of substitutions can contain similar substitutions.

Output

If s1 and s2 cannot have a common ancestor, print -1. Otherwise print the length of the shortest sequence s3, from which s1 and s2 could have evolved.

Examples

Input

ababa
aba
2
c-&gt;ba
c-&gt;cc


Output

2


Input

ababa
aba
7
c-&gt;ba
c-&gt;cc
e-&gt;ab
z-&gt;ea
b-&gt;ba
d-&gt;dd
d-&gt;ab


Output

1


Input

ababa
aba
1
c-&gt;ba


Output

-1

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
inline int in() {
  int32_t x;
  scanf("%d", &x);
  return x;
}
inline long long lin() {
  long long x;
  scanf("%lld", &x);
  return x;
}
inline string get() {
  char ch[2000010];
  scanf("%s", ch);
  return ch;
}
const int MAX_LG = 21;
const long long maxn = 2e6 + 10;
const long long base = 29;
const long long mod = 1e9 + 7;
const long long INF = 1e18 + 100;
bool ok[2][26][52][52];
vector<pair<long long, long long> > vc[30];
inline void calc(string &s, long long id) {
  for (long long i = 0; i < s.size(); i++) ok[id][s[i] - 'a'][i][i + 1] = true;
  for (long long sz = 2; sz <= s.size(); sz++) {
    for (long long l = 0; l + sz <= s.size(); l++) {
      long long r = l + sz;
      for (long long k = l + 1; k < r; k++) {
        for (long long theChar = 0; theChar < 26; theChar++) {
          for (long long pt = 0; pt < vc[theChar].size(); pt++) {
            ok[id][theChar][l][r] |= ok[id][vc[theChar][pt].first][l][k] &&
                                     ok[id][vc[theChar][pt].second][k][r];
          }
        }
      }
    }
  }
}
long long dp[100][100];
int32_t main() {
  string s = get(), t = get();
  long long n = s.size(), m = t.size();
  long long q = in();
  for (long long i = 0; i < q; i++) {
    string op = get();
    vc[op[0] - 'a'].push_back({op[3] - 'a', op[4] - 'a'});
  }
  calc(s, 0);
  calc(t, 1);
  for (long long i = 0; i <= n; i++)
    for (long long j = 0; j <= m; j++) dp[i][j] = INF;
  dp[0][0] = 0;
  for (long long r1 = 1; r1 <= n; r1++) {
    for (long long r2 = 1; r2 <= m; r2++) {
      for (long long l1 = 0; l1 < r1; l1++) {
        for (long long l2 = 0; l2 < r2; l2++) {
          for (long long theChar = 0; theChar < 26; theChar++) {
            if (ok[0][theChar][l1][r1] && ok[1][theChar][l2][r2]) {
              dp[r1][r2] = min(dp[r1][r2], dp[l1][l2] + 1);
            }
          }
        }
      }
    }
  }
  if (dp[n][m] == INF)
    cout << -1;
  else
    cout << dp[n][m] << "\n";
}