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>
template <class t>
inline void read(t &s) {
  s = 0;
  register int f = 1;
  register char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();
  s *= f;
  return;
}
template <class t, class... A>
inline void read(t &x, A &...a) {
  read(x);
  read(a...);
}
template <class t>
inline void write(t x) {
  if (x < 0) putchar('-'), x = -x;
  int buf[21], top = 0;
  while (x) buf[++top] = x % 10, x /= 10;
  if (!top) buf[++top] = 0;
  while (top) putchar(buf[top--] ^ '0');
  return;
}
inline void setIn(std::string s) {
  freopen(s.c_str(), "r", stdin);
  return;
}
inline void setOut(std::string s) {
  freopen(s.c_str(), "w", stdout);
  return;
}
inline void setIO(std::string s = "") {
  setIn(s + ".in");
  setOut(s + ".out");
  return;
}
template <class t>
inline bool checkmin(t &x, t y) {
  if (x > y) {
    x = y;
    return 1;
  }
  return 0;
}
template <class t>
inline bool checkmax(t &x, t y) {
  if (x < y) {
    x = y;
    return 1;
  }
  return 0;
}
inline int lowbit(int x) { return x & (-x); }
template <class t>
inline void Read(t &s) {
  register char c = getchar();
  while (!isalpha(c)) c = getchar();
  s = c;
}
char a[55], b[55];
std::vector<std::pair<int, int> > nxt[26];
int f[55][55][26], g[55][55][26], h[55][55];
int n, m, q;
inline int dfs(int f[55][55][26], char *a, int l, int r, int o) {
  if (~f[l][r][o]) return f[l][r][o];
  if (l == r) return ((a[l] - 'a') == o) ? 1 : 0;
  f[l][r][o] = 0;
  for (auto i : nxt[o])
    for (int k = l; !f[l][r][o] && k < r; ++k)
      f[l][r][o] |= dfs(f, a, l, k, i.first) & dfs(f, a, k + 1, r, i.second);
  return f[l][r][o];
}
signed main(void) {
  std::memset(f, -1, sizeof f), std::memset(g, -1, sizeof g);
  std::scanf("%s%s", a + 1, b + 1);
  n = std::strlen(a + 1), m = std::strlen(b + 1);
  read(q);
  while (q--) {
    register char c, u, v;
    Read(c), Read(u), Read(v);
    nxt[c - 'a'].push_back(std::make_pair(u - 'a', v - 'a'));
  }
  std::memset(h, 0x3f, sizeof h);
  h[0][0] = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      for (int k = 0; k < i; ++k)
        for (int l = 0; l < j; ++l)
          for (int o = 0; o < 26; ++o)
            if (dfs(f, a, k + 1, i, o) && dfs(g, b, l + 1, j, o))
              h[i][j] = std::min(h[i][j], h[k][l] + 1);
  if (h[n][m] > 1e9)
    std::puts("-1");
  else
    write(h[n][m]), std::puts("");
  return 0;
}