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 double pi = acos(-1.0);
const double eps = 1e-6;
const int inf = 2147483647;
char s1[100005];
char tmp[100005];
char s2[100005];
int tm[30][30];
int show[28];
int check_dis[28][28];
char check_ret[28][28];
int get_dis(int st, int a, int b) {
  int n = 26;
  int i;
  int x = st;
  int dis[30];
  int vis[30];
  memset(dis, 0, sizeof(dis));
  memset(vis, 0, sizeof(vis));
  for (i = 1; i <= n; i++) {
    if (tm[x][i] != -1)
      dis[i] = tm[x][i];
    else
      dis[i] = inf;
  }
  vis[x] = 1;
  dis[x] = 0;
  for (int k = 1; k < n; k++) {
    int min_edge = inf;
    int minp = -1;
    for (i = 1; i <= n; i++) {
      if (!vis[i] && dis[i] < min_edge) {
        min_edge = dis[i];
        minp = i;
      }
    }
    if (minp == -1) break;
    vis[minp] = 1;
    for (i = 1; i <= n; i++) {
      if (!vis[i] && tm[minp][i] != -1 && dis[i] > min_edge + tm[minp][i])
        dis[i] = min_edge + tm[minp][i];
    }
  }
  if (dis[a] == inf || dis[b] == inf)
    return -1;
  else
    return dis[a] + dis[b];
}
int get_min_dis(char a, char b, char &tt) {
  int x = a - 'a' + 1;
  int y = b - 'a' + 1;
  int i;
  int mini = -1;
  int ans = inf;
  for (i = 1; i <= 26; i++) {
    if (!show[i]) continue;
    int ret = get_dis(i, x, y);
    if (ret == -1)
      continue;
    else {
      if (ret < ans) {
        mini = i;
        ans = ret;
      }
    }
  }
  if (ans == inf) return -1;
  tt = mini + 'a' - 1;
  return ans;
}
int main() {
  int i, j;
  int n;
  scanf("%s%s", s1, s2);
  cin >> n;
  getchar();
  char xx, yy;
  int x, y, w;
  memset(tm, -1, sizeof(tm));
  for (i = 1; i <= n; i++) {
    scanf("%c %c %d", &xx, &yy, &w);
    getchar();
    x = xx - 'a' + 1;
    y = yy - 'a' + 1;
    show[x] = 1;
    show[y] = 1;
    if (-1 == tm[y][x])
      tm[y][x] = w;
    else if (w < tm[y][x])
      tm[y][x] = w;
  }
  int len1 = strlen(s1);
  int len2 = strlen(s2);
  if (len1 != len2) {
    printf("-1\n");
    return 0;
  }
  for (i = 1; i <= 26; i++) {
    for (j = i + 1; j <= 26; j++) {
      char tt;
      int ret = get_min_dis(i + 'a' - 1, j + 'a' - 1, tt);
      check_dis[i][j] = check_dis[j][i] = ret;
      check_ret[i][j] = check_ret[j][i] = tt;
    }
  }
  strcpy(tmp, s1);
  int ans = 0;
  for (i = 0; i < len1; i++) {
    if (s1[i] == s2[i]) continue;
    char tt = check_ret[s1[i] - 'a' + 1][s2[i] - 'a' + 1];
    int ret = check_dis[s1[i] - 'a' + 1][s2[i] - 'a' + 1];
    if (ret == -1) {
      printf("-1\n");
      return 0;
    }
    tmp[i] = tt;
    ans += ret;
  }
  printf("%d\n%s\n", ans, tmp);
  return 0;
}