#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
const long long MOD = 1e9;
const long long N = 100100 * 5;
char a[N], b[N], c[N];
long long n, f[33][33], ans;
bool flag;
void floyd(int n) {
  for (int k = 0; k <= n; k++)
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= n; j++) f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> a >> b >> n;
  int n1 = strlen(a);
  int n2 = strlen(b);
  for (int i = 0; i <= 27; i++)
    for (int j = 0; j <= 27; j++)
      if (i == j)
        f[i][j] = 0;
      else
        f[i][j] = INF;
  while (n--) {
    char x, y;
    long long w;
    cin >> x >> y >> w;
    f[x - 97][y - 97] = min(f[x - 97][y - 97], w);
  }
  if (n1 != n2) {
    cout << "-1" << endl;
    return 0;
  }
  floyd(25);
  for (int i = 0; i < n1; i++) {
    long long mini = INF;
    for (int j = 0; j < 26; j++) {
      long long c1 = f[a[i] - 97][j];
      long long c2 = f[b[i] - 97][j];
      if (c1 != INF && c2 != INF && mini > c1 + c2) {
        mini = c1 + c2;
        c[i] = j + 97;
      }
    }
    if (mini == INF) {
      flag = 1;
      break;
    }
    ans += mini;
  }
  if (flag) {
    cout << "-1" << endl;
    return 0;
  }
  c[n1] = '\0';
  cout << ans << endl << c << endl;
}
