#include <bits/stdc++.h>
using namespace std;
int n;
char a[100005], b[100005];
int d[30][30];
char recout[100005];
int main() {
  cin >> a >> b;
  long al = strlen(a);
  long bl = strlen(b);
  if (al != bl) {
    cout << -1 << endl;
    return 0;
  }
  memset(d, 0x3f, sizeof(d));
  for (int i = 1; i <= 26; ++i) {
    d[i][i] = 0;
  }
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    char u, v;
    int w;
    cin >> u >> v >> w;
    d[u - 'a' + 1][v - 'a' + 1] = min(d[u - 'a' + 1][v - 'a' + 1], w);
  }
  for (int k = 1; k <= 26; ++k) {
    for (int i = 1; i <= 26; ++i) {
      for (int j = 1; j <= 26; ++j) {
        d[i][j] = min(d[i][k] + d[k][j], d[i][j]);
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < al; ++i) {
    int temp = 0x3f3f3f3f;
    int replace = 0;
    for (int x = 1; x <= 26; ++x) {
      if (temp > d[a[i] - 'a' + 1][x] + d[b[i] - 'a' + 1][x]) {
        temp = d[a[i] - 'a' + 1][x] + d[b[i] - 'a' + 1][x];
        replace = x;
      }
    }
    if (temp == 0x3f3f3f3f) {
      cout << -1 << endl;
      return 0;
    }
    ans += temp;
    recout[i] = replace - 1 + 'a';
  }
  cout << ans << endl;
  cout << recout << endl;
  return 0;
}
