#include <bits/stdc++.h>
using namespace std;
int main() {
  char s1[100001], s2[100001];
  scanf("%s", s1);
  scanf("%s", s2);
  int n1 = strlen(s1);
  int n2 = strlen(s2);
  if (n1 != n2) return puts("-1"), 0;
  int m;
  cin >> m;
  int d[26][26];
  for (int i = 0; i < 26; i++)
    for (int j = 0; j < 26; j++)
      if (i == j)
        d[i][j] = 0;
      else
        d[i][j] = d[j][i] = 1e9;
  for (int i = 0; i < m; i++) {
    char x, y;
    int w;
    cin >> x >> y >> w;
    int u = x - 'a', v = y - 'a';
    d[u][v] = min(d[u][v], w);
  }
  for (int v = 0; v < 26; v++) {
    for (int u = 0; u < v; u++) {
      for (int s = 0; s < 26; s++) {
        d[u][v] = min(d[u][v], d[u][s] + d[s][v]);
        d[v][u] = min(d[v][u], d[v][s] + d[s][u]);
      }
    }
    for (int i = 0; i < 26; i++) {
      for (int j = 0; j < 26; j++) {
        d[i][j] = min(d[i][j], d[i][v] + d[v][j]);
        d[j][i] = min(d[j][i], d[j][v] + d[v][i]);
      }
    }
  }
  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 ans = 0;
  string res;
  for (int i = 0; i < n1; i++) {
    int flag = 0;
    int u = s1[i] - 'a', v = s2[i] - 'a';
    char c;
    int temp = 1e9;
    for (int j = 0; j < 26; j++) {
      if (d[u][j] + d[v][j] < temp) {
        temp = d[u][j] + d[v][j];
        c = j + 'a';
        flag = 1;
      }
    }
    if (!flag) return puts("-1"), 0;
    ans += temp;
    res += c;
  }
  cout << ans << "\n" << res;
  return 0;
}
