Problem: Boy Valera likes strings. And even more he likes them, when they are identical. That's why in his spare time Valera plays the following game. He takes any two strings, consisting of lower case Latin letters, and tries to make them identical. According to the game rules, with each move Valera can change one arbitrary character Ai in one of the strings into arbitrary character Bi, but he has to pay for every move a particular sum of money, equal to Wi. He is allowed to make as many moves as he needs. Since Valera is a very economical boy and never wastes his money, he asked you, an experienced programmer, to help him answer the question: what minimum amount of money should Valera have to get identical strings. 

Input

The first input line contains two initial non-empty strings s and t, consisting of lower case Latin letters. The length of each string doesn't exceed 105. The following line contains integer n (0 ≤ n ≤ 500) — amount of possible changings. Then follow n lines, each containing characters Ai and Bi (lower case Latin letters) and integer Wi (0 ≤ Wi ≤ 100), saying that it's allowed to change character Ai into character Bi in any of the strings and spend sum of money Wi.

Output

If the answer exists, output the answer to the problem, and the resulting string. Otherwise output -1 in the only line. If the answer is not unique, output any.

Examples

Input

uayd
uxxd
3
a x 8
x y 13
d c 3


Output

21
uxyd


Input

a
b
3
a b 2
a b 3
b a 5


Output

2
b


Input

abc
ab
6
a b 4
a b 7
b a 8
c b 11
c a 3
a c 0


Output

-1

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9;
map<long long, string> m;
void doo() {
  string s = "abcdefghijklmnopqrstuvwxyz";
  for (long long i = 1; i <= 26; ++i) {
    m[i] = s[i - 1];
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  doo();
  string s1, s2;
  cin >> s1 >> s2;
  long long n;
  cin >> n;
  if (s1.length() != s2.length()) {
    cout << -1 << endl;
    cout.flush();
    return 0;
  }
  long long t = n;
  long long g[30][30];
  for (long long i = 0; i < 30; ++i) {
    for (long long j = 0; j < 30; ++j) {
      g[i][j] = inf;
    }
  }
  while (t--) {
    string s1, s2;
    long long n;
    cin >> s1 >> s2 >> n;
    g[int(s1[0]) - 96][int(s2[0]) - 96] =
        min(g[int(s1[0]) - 96][int(s2[0]) - 96], n);
  }
  long long dis[30][30];
  for (long long i = 0; i < 30; ++i) {
    for (long long j = 0; j < 30; ++j) {
      dis[i][j] = inf;
    }
  }
  for (long long i = 0; i < 30; ++i) {
    for (long long j = 0; j < 30; ++j) {
      dis[i][j] = g[i][j];
    }
  }
  for (long long k = 1; k <= 26; ++k) {
    for (long long i = 1; i <= 26; ++i) {
      for (long long j = 1; j <= 26; ++j) {
        dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
      }
    }
  }
  string ss = "";
  long long ans = 0;
  for (long long i = 0; i < s1.length(); ++i) {
    if (s1[i] == s2[i]) {
      ss += s1[i];
      continue;
    } else {
      long long min1, min2, min3;
      long long a, x;
      a = int(s1[i]) - 96;
      x = int(s2[i]) - 96;
      min1 = g[a][x];
      min2 = g[x][a];
      long long k = -1;
      long long res = 1e18;
      for (long long j = 1; j <= 26; ++j) {
        {
          long long foo1 = dis[a][j];
          long long foo2 = dis[x][j];
          long long foo = 0;
          if (j == x) {
            foo = foo1;
          } else if (j == a) {
            foo = foo2;
          } else {
            foo = foo1 + foo2;
          }
          if (foo < res) {
            res = foo;
            k = j;
          }
        }
      }
      min3 = res;
      if (min1 <= (min(min2, min3))) {
        ss += s2[i];
        ans = ans + min1;
        continue;
      } else if (min2 <= min(min1, min3)) {
        ss += s1[i];
        ans = ans + min2;
        continue;
      } else {
        ans = ans + min3;
        ss += m[k];
        continue;
      }
    }
  }
  if (ans >= inf) {
    cout << -1 << endl;
    cout.flush();
    return 0;
  }
  cout << ans << endl;
  cout.flush();
  cout << ss << endl;
  cout.flush();
  return 0;
}