#include <bits/stdc++.h>
using namespace std;
int cost[150][150], l, n, w, res;
char s1[100001], s2[100001], s[100001], a, b, ch;
int main() {
  scanf("%s", s1);
  l = strlen(s1);
  scanf("%s", s2);
  if (l != strlen(s2)) {
    printf("-1\n");
    return 0;
  }
  for (int i = 'a'; i <= 'z'; i++) {
    for (int j = 'a'; j <= 'z'; j++) cost[i][j] = -1;
    cost[i][i] = 0;
  }
  scanf("%d", &n);
  while (n--) {
    scanf(" %c %c %d", &a, &b, &w);
    if (cost[a][b] == -1)
      cost[a][b] = w;
    else
      cost[a][b] = min(w, cost[a][b]);
  }
  for (int k = 'a'; k <= 'z'; k++)
    for (int i = 'a'; i <= 'z'; i++)
      for (int j = 'a'; j <= 'z'; j++)
        if (cost[i][k] != -1 and cost[k][j] != -1) {
          if (cost[i][j] == -1)
            cost[i][j] = cost[i][k] + cost[k][j];
          else
            cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
        }
  for (int i = 0; i < l; i++) {
    w = INT_MAX, ch = 'X';
    for (int j = 'a'; j <= 'z'; j++)
      if (cost[s1[i]][j] != -1 and cost[s2[i]][j] != -1 and
          cost[s1[i]][j] + cost[s2[i]][j] < w) {
        w = min(w, cost[s1[i]][j] + cost[s2[i]][j]);
        ch = j;
      }
    if (ch == 'X') {
      printf("-1\n");
      return 0;
    }
    res += w;
    s[i] = ch;
  }
  s[l] = '\0';
  printf("%d\n", res);
  printf("%s\n", s);
  return 0;
}
