#include <bits/stdc++.h>
using namespace std;
inline void boostIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
const int oo = 1e9;
const int N = 256;
int n, i, j, k, ans, adjMat[N][N];
string s, t;
int main() {
  boostIO();
  cin >> s >> t >> n;
  if (s.size() != t.size()) return cout << -1, 0;
  for (i = 0; i < N; ++i)
    for (j = 0; j < N; ++j) adjMat[i][j] = oo;
  for (i = 0; i < N; ++i) adjMat[i][i] = 0;
  while (n--) {
    char x, y;
    cin >> x >> y >> k;
    adjMat[x][y] = min(adjMat[x][y], k);
  }
  for (k = 'a'; k <= 'z'; ++k)
    for (i = 'a'; i <= 'z'; ++i)
      for (j = 'a'; j <= 'z'; ++j)
        adjMat[i][j] = min(adjMat[i][j], adjMat[i][k] + adjMat[k][j]);
  for (i = 0; i < s.size(); ++i) {
    if (s[i] == t[i]) continue;
    k = oo + 1;
    char c = '*';
    for (char j = 'a'; j <= 'z'; ++j) {
      if (k > adjMat[s[i]][j] + adjMat[t[i]][j]) {
        k = adjMat[s[i]][j] + adjMat[t[i]][j];
        c = j;
      }
    }
    s[i] = t[i] = c;
    ans += k;
    if (ans >= oo) return cout << -1, 0;
  }
  cout << ans << endl << s;
  return 0;
}
