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 word_1[100001];
char word_2[100001];
int INF = 10000000;
const int ALPH_SIZE = 26;
int d[30][30];
int p[30][30];
vector<char> anssio;
void findMin(char ch1, char ch2, int& val, char& ret) {
  val = INF;
  for (int i = 0; i < 30; ++i) {
    int d1 = d[i][ch1 - 'a'];
    int d2 = d[i][ch2 - 'a'];
    if (d1 < INF && d2 < INF) {
      if (val > d1 + d2) {
        val = d1 + d2;
        ret = i + 'a';
      }
    }
    d1 = d[ch1 - 'a'][i];
    d2 = d[ch2 - 'a'][i];
    if (d1 < INF && d2 < INF) {
      if (val > d1 + d2) {
        val = d1 + d2;
        ret = i + 'a';
      }
    }
  }
}
int main(void) {
  ios_base::sync_with_stdio(0);
  cin >> word_1 >> word_2;
  long N;
  cin >> N;
  if (strlen(word_1) != strlen(word_2)) {
    cout << -1;
    return 0;
  }
  for (int i = 0; i < ALPH_SIZE; ++i)
    for (int j = 0; j < ALPH_SIZE; ++j) {
      if (i != j)
        d[i][j] = INF;
      else
        d[i][j] = 0;
    }
  for (int i = 0; i < N; ++i) {
    char ch1, ch2;
    int cc;
    cin >> ch1 >> ch2 >> cc;
    if (d[ch1 - 'a'][ch2 - 'a'] > cc) {
      d[ch1 - 'a'][ch2 - 'a'] = cc;
    }
  }
  for (int k = 0; k < ALPH_SIZE; ++k) {
    for (int from = 0; from < ALPH_SIZE; ++from) {
      for (int to = 0; to < ALPH_SIZE; ++to) {
        int relax_length = d[from][k] + d[k][to];
        if (relax_length < d[from][to]) {
          d[from][to] = relax_length;
          p[from][to] = k;
        }
      }
    }
  }
  int dfdf = d['c' - 'a']['e' - 'a'];
  int ans = 0;
  int sss = strlen(word_1);
  for (int i = 0; i < sss; ++i) {
    int k1 = word_1[i] - 'a';
    int k2 = word_2[i] - 'a';
    if (word_1[i] != word_2[i]) {
      int v = 0;
      int min = INF;
      for (int h = 0; h < ALPH_SIZE; ++h) {
        if (min > d[k1][h] + d[k2][h]) {
          min = d[k1][h] + d[k2][h];
          word_1[i] = h + 'a';
        }
      }
      if (min >= INF) {
        cout << -1;
        return 0;
      }
      ans += min;
    }
  }
  cout << ans << endl;
  for (int i = 0; i < sss; ++i) {
    cout << word_1[i];
  }
  return 0;
}