#include <bits/stdc++.h>
using namespace std;
long long dist[27][27];
int main() {
  ios::sync_with_stdio(false);
  string a, b;
  int n, d;
  char c1, c2;
  cin >> a >> b;
  if (a.size() != b.size()) {
    cout << -1;
    return 0;
  }
  for (int i(0); i < 27; ++i)
    for (int j(0); j < 27; ++j) dist[i][j] = i == j ? 0 : 1000000000000000;
  cin >> n;
  while (n--) {
    cin >> c1 >> c2 >> d;
    c1 -= 'a', c2 -= 'a';
    dist[c1][c2] = min(dist[c1][c2], (long long)d);
  }
  for (int k(0); k < 27; ++k)
    for (int i(0); i < 27; ++i)
      for (int j(0); j < 27; ++j)
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
  string s;
  long long ans(0);
  for (int i(0); i < a.size(); ++i) {
    long long cur(1000000000000000);
    char add;
    for (int j(0); j < 27; ++j)
      if (cur > dist[a[i] - 'a'][j] + dist[b[i] - 'a'][j]) {
        cur = dist[a[i] - 'a'][j] + dist[b[i] - 'a'][j];
        add = j + 'a';
      }
    if (cur == 1000000000000000) {
      cout << -1;
      return 0;
    }
    ans += cur;
    s += add;
  }
  cout << ans << endl << s;
}
