#include <bits/stdc++.h>
using namespace std;
long long int mpow(long long int a, long long int b) {
  if (!b) return 1;
  long long int temp = mpow(a, b / 2);
  temp = (temp * temp) % 1000000007;
  if (b % 2) return (a * temp) % 1000000007;
  return temp;
}
long long int mod_in(long long int n) { return mpow(n, 1000000007 - 2); }
long long int cl(long long int a, long long int b) {
  if (a % b) return 1 + a / b;
  return a / b;
}
int main() {
  string s, t;
  cin >> s >> t;
  if (s.length() != t.length()) {
    cout << -1;
    exit(0);
  }
  long long int d[30][30], i, j;
  for (i = 0; i < 30; i++)
    for (j = 0; j < 30; j++) {
      d[i][j] = 1e+18;
      if (i == j) d[i][j] = 0;
    }
  long long int n;
  cin >> n;
  while (n--) {
    char c1, c2;
    cin >> c1 >> c2 >> i;
    d[c1 - 'a'][c2 - 'a'] = min(i, d[c1 - 'a'][c2 - 'a']);
  }
  long long int tt = 100;
  for (n = 0; n < 26; n++)
    for (j = 0; j < 26; j++)
      for (i = 0; i < 26; i++) d[i][j] = min(d[i][n] + d[n][j], d[i][j]);
  long long int ans = 0;
  for (i = 0; i < s.size(); i++) {
    if (s[i] != t[i]) {
      long long int minn = 1e+18, id = -1;
      for (j = 0; j < 26; j++) {
        if (minn > d[s[i] - 'a'][j] + d[t[i] - 'a'][j]) {
          id = j;
          minn = d[s[i] - 'a'][j] + d[t[i] - 'a'][j];
        }
      }
      if (id != -1) s[i] = (char)('a' + id);
      if (minn >= 1e+18) {
        cout << -1;
        exit(0);
      }
      ans += minn;
    }
  }
  if (ans >= 1e+18) ans = -1;
  cout << ans << "\n";
  if (ans > -1) cout << s;
}
