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 int inf = 100000000;
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(char b) { return to_string(b); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto& x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string a, b;
  cin >> a >> b;
  int N;
  cin >> N;
  if (a.length() != b.length()) {
    cout << -1 << '\n';
    return 0;
  }
  vector<vector<int>> dist(26, vector<int>(26));
  for (int i = 0; i < 26; ++i) {
    for (int j = 0; j < 26; ++j) {
      dist[i][j] = inf;
    }
  }
  for (int i = 0; i < N; ++i) {
    char aa, bb;
    cin >> aa >> bb;
    int z;
    cin >> z;
    dist[aa - 'a'][bb - 'a'] = min(z, dist[aa - 'a'][bb - 'a']);
  }
  for (int i = 0; i < 26; ++i) {
    dist[i][i] = 0;
  }
  for (int k = 0; k < 26; ++k) {
    for (int i = 0; i < 26; ++i) {
      for (int j = 0; j < 26; ++j) {
        if (dist[i][j] > dist[i][k] + dist[k][j]) {
          dist[i][j] = dist[i][k] + dist[k][j];
        }
      }
    }
  }
  string res = "";
  long long p = 0;
  for (int i = 0; i < (int)a.size(); ++i) {
    int x = a[i] - 'a', y = b[i] - 'a';
    int W = inf;
    char ch;
    for (int j = 0; j < 26; ++j) {
      if (dist[x][j] + dist[y][j] < W) {
        W = dist[x][j] + dist[y][j];
        ch = (char)(j + 'a');
      }
    }
    if (W == inf) {
      cout << -1 << '\n';
      return 0;
    }
    res += ch;
    p += W;
  }
  if ((int)p >= inf) {
    42;
    cout << -1 << '\n';
  } else {
    cout << p << '\n' << res << '\n';
  }
  return 0;
}