#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
int n;
int d[26][26];
char s1[111000];
char s2[111000];
int main() {
  gets(s1);
  gets(s2);
  scanf("%d", &n);
  for (int i = 0, _n = (26); i < _n; i++)
    for (int j = 0, _n = (26); j < _n; j++) d[i][j] = i == j ? 0 : 1000000000;
  for (int i = 0, _n = (n); i < _n; i++) {
    char c1, c2;
    do c1 = getc(stdin);
    while (!isalpha(c1));
    do c2 = getc(stdin);
    while (!isalpha(c2));
    int w;
    scanf("%d", &w);
    d[c1 - 'a'][c2 - 'a'] = min(w, d[c1 - 'a'][c2 - 'a']);
  }
  for (int k = 0, _n = (26); k < _n; k++)
    for (int i = 0, _n = (26); i < _n; i++)
      for (int j = 0, _n = (26); j < _n; j++)
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
  int len = strlen(s1);
  int len2 = strlen(s2);
  if (len != len2) {
    printf("-1\n");
    return 0;
  }
  long long res = 0;
  for (int i = 0, _n = (len); i < _n; i++) {
    int w = 1000000000;
    char ch = 0;
    for (int j = 0, _n = (26); j < _n; j++)
      if (d[s1[i] - 'a'][j] + d[s2[i] - 'a'][j] < w) {
        w = d[s1[i] - 'a'][j] + d[s2[i] - 'a'][j];
        ch = 'a' + j;
      }
    if (w == 1000000000) {
      printf("-1\n");
      return 0;
    }
    res += w;
    s1[i] = ch;
  }
  printf("%lld\n%s\n", res, s1);
  return 0;
}
