#include <bits/stdc++.h>
using namespace std;
string A, B, s;
int N;
int mat[31][31];
void Citire() {
  for (int i = 0; i <= 29; i++) {
    for (int j = 0; j <= 29; j++) mat[i][j] = 200000000;
    mat[i][i] = 0;
  }
  cin >> A >> B >> N;
  char a, b;
  int c;
  for (int i = 0; i < N; i++) {
    cin >> a >> b >> c;
    mat[a - 'a'][b - 'a'] = min(mat[a - 'a'][b - 'a'], c);
  }
}
void Roy_Floyd() {
  for (int k = 0; k <= 29; k++) {
    for (int i = 0; i <= 29; i++) {
      for (int j = 0; j <= 29; j++) {
        mat[i][j] = min(mat[i][j], mat[i][k] + mat[k][j]);
      }
    }
  }
}
void Business() {
  int a = A.size(), b = B.size(), mic, Suma = 0;
  char Litera;
  if (a != b) {
    cout << "-1\n";
    return;
  }
  for (int i = 0; i <= a; i++) {
    if (A[i] == B[i]) {
      s.push_back(A[i]);
      continue;
    }
    mic = 200000000;
    for (int j = 0; j <= 29; j++) {
      if (mic > mat[A[i] - 'a'][j] + mat[B[i] - 'a'][j]) {
        mic = mat[A[i] - 'a'][j] + mat[B[i] - 'a'][j];
        Litera = (char)j + 'a';
      }
    }
    if (mic == 200000000) {
      cout << "-1\n";
      return;
    }
    Suma += mic;
    s.push_back(Litera);
  }
  cout << Suma << "\n" << s << "\n";
}
int main() {
  Citire();
  Roy_Floyd();
  int a = A.size(), b = B.size(), Sum = 0, mic;
  string s;
  char litera;
  if (a != b) {
    cout << "-1";
    return 0;
  }
  for (int i = 0; i < a; i++) {
    mic = 200000000;
    for (int j = 0; j <= 29; j++) {
      if (mat[A[i] - 'a'][j] + mat[B[i] - 'a'][j] < mic) {
        mic = mat[A[i] - 'a'][j] + mat[B[i] - 'a'][j];
        litera = j + 'a';
      }
    }
    if (mic == 200000000) {
      cout << "-1";
      return 0;
    }
    Sum += mic;
    s.push_back(litera);
  }
  cout << Sum << "\n" << s;
  return 0;
}
