#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int n;
string a, b, str;
int sp[100][100];
int ans;
void fl() {
  for (int k = 0; k < 26; k++)
    for (int i = 0; i < 26; i++)
      for (int j = 0; j < 26; j++)
        sp[i][j] = min(sp[i][j], sp[i][k] + sp[k][j]);
}
int main() {
  for (int i = 0; i < 26; i++)
    for (int j = 0; j < 26; j++) sp[i][j] = INF;
  cin >> a >> b >> n;
  for (int i = 0; i < n; i++) {
    char x, y;
    int val;
    cin >> x >> y >> val;
    sp[x - 'a'][y - 'a'] = min(sp[x - 'a'][y - 'a'], val);
  }
  if (a.size() != b.size()) return cout << -1 << endl, 0;
  for (int i = 0; i < 26; i++) sp[i][i] = 0;
  fl();
  for (int i = 0; i < a.size(); i++) {
    int check = INF;
    char lc;
    for (int j = 0; j < 26; j++) {
      if (sp[a[i] - 'a'][j] + sp[b[i] - 'a'][j] < check) {
        check = sp[a[i] - 'a'][j] + sp[b[i] - 'a'][j];
        lc = j + 'a';
      }
    }
    if (check >= INF) return cout << -1 << endl, 0;
    str += lc;
    ans += check;
  }
  cout << ans << '\n' << str << endl;
}
