#include <bits/stdc++.h>
using namespace std;
long long maxi = 10000000000000;
void sol() {
  long long n, i, j, m, q, k;
  string a, b;
  cin >> a >> b;
  n = a.length();
  m = b.length();
  if (n != m) {
    cout << "-1";
    return;
  }
  long long dp[26][26];
  for (i = 0; i < 26; i++) {
    for (j = 0; j < 26; j++) dp[i][j] = maxi;
    dp[i][i] = 0;
  }
  cin >> q;
  while (q--) {
    char x, y;
    cin >> x >> y >> i;
    dp[x - 'a'][y - 'a'] = min(dp[x - 'a'][y - 'a'], i);
  }
  for (k = 0; k < 26; k++) {
    for (j = 0; j < 26; j++) {
      for (i = 0; i < 26; i++) {
        if (dp[i][k] != maxi && dp[k][j] != maxi)
          dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
      }
    }
  }
  long long ans = 0;
  string s = "";
  for (i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      long long sum = maxi;
      long long x;
      for (j = 0; j < 26; j++) {
        if (dp[a[i] - 'a'][j] != maxi && dp[b[i] - 'a'][j] != maxi) {
          if (dp[a[i] - 'a'][j] + dp[b[i] - 'a'][j] < sum)
            sum = dp[a[i] - 'a'][j] + dp[b[i] - 'a'][j], x = j;
        }
      }
      if (sum == maxi) {
        cout << "-1";
        return;
      }
      ans += sum;
      s += 'a' + x;
    } else
      s += a[i];
  }
  cout << ans << "\n" << s;
}
int main() {
  sol();
  return 0;
}
