#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100001;
string a, b;
int n;
int d[30][30];
char ans[MAXN];
int main() {
  cin >> a >> b;
  if (a.size() != b.size()) {
    cout << -1 << endl;
    return 0;
  }
  memset(d, 0x3f, sizeof(d));
  for (int i = 0; i < 26; i++) {
    d[i][i] = 0;
  }
  cin >> n;
  for (int i = 0; i < n; i++) {
    char u, v;
    int w;
    cin >> u >> v >> w;
    d[u - 'a'][v - 'a'] = min(d[u - 'a'][v - 'a'], w);
  }
  for (int k = 0; k < 26; k++) {
    for (int i = 0; i < 26; i++) {
      for (int j = 0; j < 26; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
  int cost = 0;
  for (int i = 0; i < a.size(); i++) {
    if (a[i] == b[i]) {
      ans[i] = a[i];
      continue;
    }
    int index = 0x3f3f3f3f;
    char ch;
    for (int j = 0; j < 26; j++) {
      if (d[a[i] - 'a'][j] + d[b[i] - 'a'][j] < index) {
        index = d[a[i] - 'a'][j] + d[b[i] - 'a'][j];
        ch = j + 'a';
      }
    }
    if (index == 0x3f3f3f3f) {
      cout << -1 << endl;
      return 0;
    }
    cost += index;
    ans[i] = ch;
  }
  cout << cost << endl;
  cout << ans << endl;
  return 0;
}
