#include <bits/stdc++.h>
using namespace std;
int x[100][100];
int main() {
  string s, t;
  char a, b;
  int c, num;
  memset(x, 10000, sizeof(x));
  for (int i = 0; i < 26; ++i) {
    x[i][i] = 0;
  }
  cin >> s >> t;
  cin >> num;
  for (int i = 0; i < num; ++i) {
    cin >> a;
    cin >> b;
    cin >> c;
    x[a - 'a'][b - 'a'] =
        ((x[a - 'a'][b - 'a']) < (c) ? (x[a - 'a'][b - 'a']) : (c));
  }
  if ((int)(s.size()) != (int)(t.size())) {
    cout << -1 << endl;
    return 0;
  }
  for (int k = 0; k < 26; ++k) {
    for (int i = 0; i < 26; ++i)
      if (i != k) {
        for (int j = 0; j < 26; ++j)
          if (j != k && j != i) {
            x[i][j] = min(x[i][j], x[i][k] + x[k][j]);
          }
      }
  }
  int cost = 0, tmp;
  string ret = "";
  char y;
  for (int i = 0; i < (int)(s.size()); ++i) {
    tmp = (int)1e9;
    for (int k = 0; k < 26; ++k) {
      if (x[s[i] - 'a'][k] + x[t[i] - 'a'][k] < tmp) {
        tmp = x[s[i] - 'a'][k] + x[t[i] - 'a'][k];
        y = (char)(k + 'a');
      }
    }
    ret += y;
    if (tmp > 200) {
      cout << -1 << endl;
      return 0;
    }
    cost += tmp;
  }
  cout << cost << endl << ret << endl;
  return 0;
}
