#include <bits/stdc++.h>
using namespace std;
int main() {
  string in1, in2;
  cin >> in1 >> in2;
  int dist[30][30];
  for (int i = 0; i < 30; i++) {
    for (int j = 0; j < 30; j++) dist[i][j] = 1 << 28;
    dist[i][i] = 0;
  }
  int n, w;
  char a, b;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a >> b >> w;
    dist[a - 'a'][b - 'a'] = min(w, dist[a - 'a'][b - 'a']);
  }
  for (int k = 0; k < 26; k++)
    for (int i = 0; i < 26; i++)
      for (int j = 0; j < 26; j++)
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
  if (in1.size() != in2.size()) {
    cout << "-1\n";
    return 0;
  }
  for (int i = 0; i < in1.size(); i++) {
    bool mila = false;
    for (int j = 0; j < 26; j++) {
      if (dist[in1[i] - 'a'][j] != 1 << 28 && dist[in2[i] - 'a'][j] != 1 << 28)
        mila = true;
    }
    if (!mila) {
      cout << "-1\n";
      return 0;
    }
  }
  char ans[100009];
  int tt = 0;
  for (int i = 0; i < in1.size(); i++) {
    int mx = 1 << 28;
    for (int j = 0; j < 26; j++)
      if (dist[in1[i] - 'a'][j] + dist[in2[i] - 'a'][j] < mx) {
        ans[i] = 'a' + j;
        mx = dist[in1[i] - 'a'][j] + dist[in2[i] - 'a'][j];
      }
    tt += mx;
  }
  cout << tt << endl;
  for (int i = 0; i < in1.size(); i++) cout << ans[i];
  cout << endl;
  return 0;
}
