#include <bits/stdc++.h>
using namespace std;
int main() {
  int w, res, a[200][200], i, j, n, k, m, jm;
  char x, y;
  string s1, s2, s3;
  for (i = 0; i <= 100; i++)
    for (j = 0; j <= 100; j++)
      if (i == j)
        a[i][j] = 0;
      else
        a[i][j] = 100500;
  cin >> s1 >> s2;
  if (s1.length() != s2.length()) {
    cout << -1;
    return 0;
  }
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> x >> y >> w;
    if (a[x - 'a'][y - 'a'] > w) a[x - 'a'][y - 'a'] = w;
  }
  for (k = 0; k <= 100; k++)
    for (i = 0; i <= 100; i++)
      for (j = 0; j <= 100; j++)
        if (a[i][k] + a[k][j] < a[i][j]) a[i][j] = a[i][k] + a[k][j];
  n = s1.length();
  res = 0;
  for (i = 0; i < n; i++) {
    if (s1[i] == s2[i]) {
      s3 += s1[i];
      continue;
    }
    m = 1000500;
    for (j = 0; j < 100; j++)
      if (a[s1[i] - 'a'][j] + a[s2[i] - 'a'][j] < m) {
        m = a[s1[i] - 'a'][j] + a[s2[i] - 'a'][j];
        jm = j;
      }
    if (m > 10000) {
      cout << -1;
      return 0;
    } else {
      res += m;
      s3 += jm + 'a';
    }
  }
  cout << res << endl;
  cout << s3;
  return 0;
}
