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;
char a[100010], b[100010];
char d[100010];
int w[30][30];
bool is[30][30];
int n;
inline int len(char a[]) {
  int i;
  for (i = 0; a[i] >= 'a' && a[i] <= 'z'; ++i) {
    ;
  }
  return i;
}
int main() {
  fgets(a, 100010, stdin);
  fgets(b, 100010, stdin);
  int na = len(a);
  int nb = len(b);
  if (na != nb) {
    printf("-1\n");
    return 0;
  }
  scanf("%d", &n);
  char buf[100];
  gets(buf);
  char c1, c2;
  int x;
  for (int i = 0; i < n; ++i) {
    scanf("%c %c %d", &c1, &c2, &x);
    gets(buf);
    c1 -= 'a';
    c2 -= 'a';
    if (is[c1][c2])
      w[c1][c2] = min(w[c1][c2], x);
    else {
      is[c1][c2] = true;
      w[c1][c2] = x;
    }
  }
  for (int i = 0; i < 30; ++i) {
    is[i][i] = true;
    w[i][i] = 0;
  }
  for (int k = 0; k < 26; ++k) {
    for (int i = 0; i < 26; ++i) {
      if (i == k) continue;
      if (!is[i][k]) continue;
      for (int j = 0; j < 26; ++j) {
        if (j == k || j == i) continue;
        if (!is[k][j]) continue;
        if (!is[i][j] || w[i][j] > w[i][k] + w[k][j]) {
          is[i][j] = true;
          w[i][j] = w[i][k] + w[k][j];
        }
      }
    }
  }
  int sum = 0;
  char c3;
  int cat;
  for (int i = 0; i < na; ++i) {
    if (a[i] == b[i]) {
      d[i] = a[i];
      continue;
    }
    cat = 210;
    c1 = a[i] - 'a';
    c2 = b[i] - 'a';
    for (char j = 'a'; j <= 'z'; ++j) {
      c3 = j - 'a';
      if (is[c1][c3] && is[c2][c3] && w[c1][c3] + w[c2][c3] < cat) {
        d[i] = j;
        cat = w[c1][c3] + w[c2][c3];
      }
    }
    if (cat == 210) {
      printf("-1\n");
      return 0;
    }
    sum += cat;
  }
  printf("%d\n", sum);
  fputs(d, stdout);
  fputc('\n', stdout);
  return 0;
}