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 double eps = 1e-7;
const string alpha = "abcdefghijklmnopqrstuvwxyz";
int dx[8] = {+1, +1, +1, 0, 0, -1, -1, -1};
int dy[8] = {+1, 0, -1, +1, -1, +1, 0, -1};
long long power(long long x, long long y) {
  long long res = 1;
  x = x;
  while (y > 0) {
    if (y & 1) res = (res * x);
    y = y >> 1;
    x = (x * x);
  }
  return res;
}
template <typename T>
T gcd(T a, T mp) {
  if (a == 0) return mp;
  return gcd(mp % a, a);
}
template <typename T>
T powm(T a, T mp, long long m) {
  T cnt = 1;
  while (mp > 0) {
    if (mp % 2 == 1) cnt = (cnt * a) % m;
    mp /= 2;
    a = (a * a) % m;
  }
  return cnt % m;
}
long long ncr(long long n, long long r) {
  long long res = 1;
  if (r > n) return 0;
  if (r > n - r) r = n - r;
  for (long long i = 0; i < r; i++) {
    res *= (n - i);
    res /= (i + 1);
  }
  return res;
}
long long sqr(long long x) { return x * x; }
const int mod = 998244353;
const int N = 1000005;
void prec() {}
void run() {
  string s, t;
  cin >> s >> t;
  int dp[26][26];
  for (int i = 0; i <= 25; ++i) {
    for (int j = 0; j <= 25; ++j) {
      if (i == j)
        dp[i][j] = 0;
      else
        dp[i][j] = 1e7;
    }
  }
  int n, ans = 0;
  cin >> n;
  while (n--) {
    char ch1, ch2;
    int x;
    cin >> ch1 >> ch2 >> x;
    dp[ch1 - 'a'][ch2 - 'a'] = min(dp[ch1 - 'a'][ch2 - 'a'], x);
  }
  for (int i = 0; i <= 25; ++i) {
    for (int j = 0; j <= 25; ++j) {
      for (int k = 0; k <= 25; ++k) {
        if (j == k) continue;
        dp[j][k] = min(dp[j][k], dp[j][i] + dp[i][k]);
      }
    }
  }
  int l1 = (long long)s.size(), l2 = (long long)t.size();
  if (l1 != l2) {
    cout << -1;
    return;
  }
  string final = "";
  for (int i = 0; i < l1; ++i) {
    int temp = 1e9;
    char ch;
    for (int j = 0; j <= 25; ++j) {
      if ((dp[s[i] - 'a'][j] + dp[t[i] - 'a'][j]) < temp) {
        temp = dp[s[i] - 'a'][j] + dp[t[i] - 'a'][j];
        ch = 'a' + j;
      }
    }
    if (temp > 1e5) {
      cout << -1 << "\n";
      return;
    }
    final += ch;
    ans += temp;
  }
  cout << ans << "\n";
  cout << final;
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  int tc = 1;
  prec();
  for (long long i = 0; i < tc; ++i) run();
  return 0;
}