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;
template <class T>
inline void checkmin(T& a, const T& b) {
  if (b < a) a = b;
}
template <class T>
inline void checkmax(T& a, const T& b) {
  if (b > a) a = b;
}
char s[100000 + 10];
char t[100000 + 10];
int n;
int len;
int cost[26][26];
int C[26][26];
int used[26];
void dejkstra() {
  memset((C), (int)1 << 5, sizeof(C));
  ;
  for (int i = 0, _n = (26); i < _n; ++i) {
    memset((used), (int)0, sizeof(used));
    ;
    int count_used = 0;
    C[i][i] = 0;
    while (count_used < 26) {
      int cur = -1;
      int curval = -1;
      for (int j = 0, _n = (26); j < _n; ++j)
        if (!used[j]) {
          if (cur == -1) {
            cur = j;
            curval = C[i][j];
          } else {
            if (C[i][j] < curval) {
              curval = C[i][j];
              cur = j;
            }
          }
        }
      for (int j = 0, _n = (26); j < _n; ++j)
        if (!used[j] && cost[cur][j] >= 0 && cur != j) {
          checkmin(C[i][j], C[i][cur] + cost[cur][j]);
        }
      used[cur] = 1;
      count_used++;
    }
  }
}
int main() {
  gets(s);
  gets(t);
  scanf("%d", &n);
  memset((cost), (int)0xFF, sizeof(cost));
  ;
  for (int i = 0, _n = (n); i < _n; ++i) {
    char a[10], b[10];
    int w;
    scanf("%s%s%d", &a, &b, &w);
    int ia = a[0] - 'a';
    int ib = b[0] - 'a';
    if (cost[ia][ib] < 0)
      cost[ia][ib] = w;
    else
      checkmin(cost[ia][ib], w);
  }
  len = strlen(s);
  if (len != strlen(t)) {
    printf("-1\n");
    return 0;
  }
  dejkstra();
  string answer = "";
  int result_cost = 0;
  for (int i = 0, _n = (len); i < _n; ++i) {
    if (s[i] == t[i]) {
      answer += s[i];
    } else {
      int cur_price;
      char cur;
      if (C[s[i] - 'a'][t[i] - 'a'] < C[t[i] - 'a'][s[i] - 'a']) {
        cur = t[i];
        cur_price = C[s[i] - 'a'][t[i] - 'a'];
      } else {
        cur = s[i];
        cur_price = C[t[i] - 'a'][s[i] - 'a'];
      }
      for (int mid = 0, _n = (26); mid < _n; ++mid) {
        if (C[s[i] - 'a'][mid] + C[t[i] - 'a'][mid] < cur_price) {
          cur_price = C[s[i] - 'a'][mid] + C[t[i] - 'a'][mid];
          cur = mid + 'a';
        }
      }
      if (cur_price > 1000000) {
        printf("-1\n");
        return 0;
      }
      answer += cur;
      result_cost += cur_price;
    }
  }
  printf("%d\n", result_cost);
  cout << answer << endl;
  return 0;
}