#include <bits/stdc++.h>
using namespace std;
const long long MAX = 100000, MOD = 1e9 + 7;
int main() {
  string s, t;
  cin >> s >> t;
  vector<vector<int> > arr(26, vector<int>(26, MAX));
  for (int i = 0; i < 26; i++) arr[i][i] = 0;
  int m;
  cin >> m;
  char x, y;
  int w;
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> w;
    if (w < arr[x - 'a'][y - 'a']) {
      arr[x - 'a'][y - 'a'] = w;
    }
  }
  for (int k = 0; k < 26; k++) {
    for (int i = 0; i < 26; i++) {
      for (int j = 0; j < 26; j++) {
        if (arr[i][k] != MAX && arr[k][j] != MAX) {
          if (arr[i][k] + arr[k][j] < arr[i][j]) {
            arr[i][j] = arr[i][k] + arr[k][j];
          }
        }
      }
    }
  }
  if (s.length() != t.length()) {
    cout << -1 << endl;
    return 0;
  }
  long long ans = 0;
  for (int k = 0; k < s.length(); k++) {
    if (s[k] == t[k])
      continue;
    else {
      int f = MAX, p = -1;
      for (int key = 0; key < 26; key++) {
        if (arr[s[k] - 'a'][key] + arr[t[k] - 'a'][key] < f) {
          f = arr[s[k] - 'a'][key] + arr[t[k] - 'a'][key];
          p = key;
        }
      }
      if (f == MAX) {
        cout << -1 << endl;
        return 0;
      } else {
        s[k] = p + 'a';
        ans += f;
      }
    }
  }
  cout << ans << endl;
  cout << s << endl;
  return 0;
}
