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 <typename T>
inline T abs(T x) {
  return x > 0 ? x : -x;
}
template <typename T>
inline T sqr(T x) {
  return x * x;
}
template <typename T>
inline T max(T a, T b, T c) {
  return max(max(a, b), c);
}
template <typename T>
inline T min(T a, T b, T c) {
  return min(min(a, b), c);
}
template <typename T>
inline T max(T a, T b, T c, T d) {
  return max(max(a, b), max(c, d));
}
template <typename T>
inline bool toMax(T& a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T>
inline bool toMin(T& a, T b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <typename T, typename T1>
inline T qpower(T x, T p, T1 mod) {
  T re = 1;
  x %= mod;
  while (p) {
    if (p & 1) re = (re * x) % mod;
    x = (x * x) % mod;
    p >>= 1;
  }
  return re;
}
template <typename T1, typename T2>
inline T1 power(T1 x, T2 p) {
  return x < 0 && p < 1 && -1 < p ? p != 0 ? -pow(-x, p) : 1 : pow(x, p);
}
inline bool mr(long long x, long long b) {
  long long k = x - 1;
  while (k) {
    long long cur = qpower(b, k, x);
    if (cur != 1 && cur != x - 1) return false;
    if ((k & 1) == 1 || cur == x - 1) return true;
    k >>= 1;
  }
  return true;
}
inline bool isprime(long long x) {
  if (x == 46856248255981ll || x < 2) return false;
  if (x == 2 || x == 3 || x == 7 || x == 61 || x == 24251) return true;
  return mr(x, 2) && mr(x, 61);
}
template <typename T>
inline void read(T& res) {
  char c;
  res = 0;
  bool f = 0;
  while ((c = getchar()) < 48)
    if (c == '-') f = 1;
  do res = (res << 1) + (res << 3) + (c ^ 48);
  while ((c = getchar()) > 47);
  if (f) res = -res;
}
template <typename T>
inline void write(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 ^ 48);
}
template <typename T>
inline string itos(T x) {
  stringstream ss;
  string str;
  ss << x;
  ss >> str;
  return str;
}
const double PI = acos(-1);
const double e = 2.718281828459;
const long long MOD = 1000000007;
const int MAXN = 205;
const int MAX = 0x3f3f3f3f;
const int INF = 0x80000000;
int dis[MAXN][MAXN];
int main() {
  string from;
  string to;
  cin >> from >> to;
  int n;
  cin >> n;
  for (register int i = 0; i < MAXN; i++)
    for (register int j = 0; j < MAXN; j++) {
      if (i == j)
        dis[i][j] = 0;
      else
        dis[i][j] = MAX;
    }
  while (n--) {
    char a, b;
    int w;
    cin >> a >> b >> w;
    dis[a][b] = min(dis[a][b], w);
  }
  if (from.size() != to.size()) {
    cout << "-1\n";
    return 0;
  }
  for (register int k = 'a'; k <= 'z'; k++)
    for (register int i = 'a'; i <= 'z'; i++)
      for (register int j = 'a'; j <= 'z'; j++)
        if (dis[i][k] + dis[k][j] < dis[i][j])
          dis[i][j] = dis[i][k] + dis[k][j];
  int sum = 0;
  bool flag = true;
  string re = from;
  for (register int i = 0; i < from.size(); i++) {
    if (from[i] != to[i]) {
      int ans = MAX;
      for (register int c = 'a'; c <= 'z'; c++) {
        if (dis[from[i]][c] != MAX && dis[to[i]][c] != MAX) {
          if (dis[from[i]][c] + dis[to[i]][c] < ans) {
            re[i] = char(c);
            ans = dis[from[i]][c] + dis[to[i]][c];
          }
        }
      }
      if (ans != MAX)
        sum += ans;
      else {
        cerr << "\tDEBUG: "
             << "i"
             << " = " << (i) << endl;
        flag = false;
        break;
      }
    }
  }
  if (!flag)
    cout << "-1\n";
  else
    cout << sum << endl << re << endl;
  return 0;
}