#include <bits/stdc++.h>
using namespace std;
int mp[30][30], n, m, w;
int main() {
  char x, y, s[100010], s1[100010], s2[100010];
  int flag = 0, ans = 0;
  for (int i = 0; i < 26; i++)
    for (int j = 0; j < 26; j++) mp[i][j] = 3000;
  scanf(" %s %s %d", s1, s2, &n);
  while (n--) {
    scanf(" %c %c %d", &x, &y, &w);
    if (mp[x - 'a'][y - 'a'] > w) mp[x - 'a'][y - 'a'] = w;
  }
  for (int i = 0; i < 26; i++) mp[i][i] = 0;
  for (int k = 0; k < 26; k++)
    for (int i = 0; i < 26; i++)
      for (int j = 0; j < 26; j++)
        if (mp[i][j] > mp[i][k] + mp[k][j]) mp[i][j] = mp[i][k] + mp[k][j];
  int len1 = strlen(s1), len2 = strlen(s2);
  if (len1 != len2) flag = 1;
  for (int i = 0; i < len1; i++) {
    int d = 3000, K;
    for (int k = 0; k < 26; k++)
      if (d > mp[s1[i] - 'a'][k] + mp[s2[i] - 'a'][k])
        d = mp[s1[i] - 'a'][k] + mp[s2[i] - 'a'][k], K = k;
    if (d >= 3000 || d < 0) {
      flag = 1;
      break;
    } else
      ans += d, s[i] = K + 'a';
  }
  if (flag)
    printf("-1\n");
  else
    printf("%d\n%s\n", ans, s);
}
