#include <bits/stdc++.h>
using namespace std;
long long int distancia[(int)30][(int)30];
long long int graph[(int)30][(int)30];
void floydWarshall() {
  for (int i = 0; i < (int)30; i++) {
    for (int j = 0; j < (int)30; j++) {
      distancia[i][j] = 1e5;
      if (i == j) distancia[i][j] = 0;
    }
  }
  for (int i = 0; i < (int)30; i++) {
    for (int j = 0; j < (int)30; j++) {
      if (graph[i][j] != 0) distancia[i][j] = graph[i][j] - 1;
      if (i == j) distancia[i][j] = 0;
    }
  }
  for (int k = 0; k < (int)30; k++) {
    for (int i = 0; i < (int)30; i++) {
      for (int j = 0; j < (int)30; j++) {
        distancia[i][j] =
            min(distancia[i][j], distancia[i][k] + distancia[k][j]);
      }
    }
  }
}
int main() {
  string s, t;
  long long int n, z;
  char x, y;
  long long int ans = 0;
  cin >> s >> t;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x >> y >> z;
    if (graph[x - 'a'][y - 'a'] == 0) {
      graph[x - 'a'][y - 'a'] = z + 1;
    } else {
      graph[x - 'a'][y - 'a'] = min(graph[x - 'a'][y - 'a'], z + 1);
    }
  }
  floydWarshall();
  char current;
  int changeCost;
  long long int a, b;
  if (s.size() != t.size()) {
    ans = -1;
  } else {
    for (int i = 0; i < s.size() && ans != -1; i++) {
      a = s[i] - 'a';
      b = t[i] - 'a';
      if (a != b) {
        changeCost = 1e5;
        for (int j = 0; j < (int)30; j++) {
          if ((distancia[a][j] + distancia[b][j]) < changeCost) {
            changeCost = distancia[a][j] + distancia[b][j];
            current = j + 'a';
          }
        }
        if (changeCost == 1e5)
          ans = -1;
        else {
          ans += changeCost;
          s[i] = current;
        }
      }
    }
  }
  if (ans != -1) {
    cout << ans << endl << s << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}
