#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, w[26][26];
  int money, cost, temp;
  char a[100002], b[100002], x, y;
  char result[100002];
  for (int i = 0; i < 26; ++i) {
    for (int j = 0; j < 26; ++j) {
      w[i][j] = 1073741823;
    }
    w[i][i] = 0;
  }
  cin >> a >> b;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> x >> y >> cost;
    x -= 'a';
    y -= 'a';
    w[x][y] = min(w[x][y], cost);
  }
  for (int k = 0; k < 26; ++k) {
    for (int i = 0; i < 26; ++i) {
      for (int j = 0; j < 26; ++j) {
        w[i][j] = min(w[i][j], w[i][k] + w[k][j]);
      }
    }
  }
  n = strlen(a);
  money = strlen(b) == n ? 0 : 1073741823;
  for (int i = 0; i < n && money < 1073741823; ++i) {
    cost = 1073741823;
    for (int j = 0; j < 26; ++j) {
      temp = w[a[i] - 'a'][j] + w[b[i] - 'a'][j];
      if (temp < cost) {
        cost = temp;
        result[i] = 'a' + j;
      }
    }
    money += cost;
  }
  if (money < 1073741823) {
    result[n] = '\0';
    cout << money << endl << result << endl;
  } else {
    cout << "-1" << endl;
  }
  return 0;
}
