#include <bits/stdc++.h>
using namespace std;
char word_1[100001];
char word_2[100001];
int INF = 10000000;
const int ALPH_SIZE = 26;
int d[30][30];
int p[30][30];
vector<char> anssio;
void findMin(char ch1, char ch2, int& val, char& ret) {
  val = INF;
  for (int i = 0; i < 30; ++i) {
    int d1 = d[i][ch1 - 'a'];
    int d2 = d[i][ch2 - 'a'];
    if (d1 < INF && d2 < INF) {
      if (val > d1 + d2) {
        val = d1 + d2;
        ret = i + 'a';
      }
    }
    d1 = d[ch1 - 'a'][i];
    d2 = d[ch2 - 'a'][i];
    if (d1 < INF && d2 < INF) {
      if (val > d1 + d2) {
        val = d1 + d2;
        ret = i + 'a';
      }
    }
  }
}
int main(void) {
  ios_base::sync_with_stdio(0);
  cin >> word_1 >> word_2;
  long N;
  cin >> N;
  if (strlen(word_1) != strlen(word_2)) {
    cout << -1;
    return 0;
  }
  for (int i = 0; i < ALPH_SIZE; ++i)
    for (int j = 0; j < ALPH_SIZE; ++j) {
      if (i != j)
        d[i][j] = INF;
      else
        d[i][j] = 0;
    }
  for (int i = 0; i < N; ++i) {
    char ch1, ch2;
    int cc;
    cin >> ch1 >> ch2 >> cc;
    if (d[ch1 - 'a'][ch2 - 'a'] > cc) {
      d[ch1 - 'a'][ch2 - 'a'] = cc;
    }
  }
  for (int k = 0; k < ALPH_SIZE; ++k) {
    for (int from = 0; from < ALPH_SIZE; ++from) {
      for (int to = 0; to < ALPH_SIZE; ++to) {
        int relax_length = d[from][k] + d[k][to];
        if (relax_length < d[from][to]) {
          d[from][to] = relax_length;
          p[from][to] = k;
        }
      }
    }
  }
  int dfdf = d['c' - 'a']['e' - 'a'];
  int ans = 0;
  int sss = strlen(word_1);
  for (int i = 0; i < sss; ++i) {
    int k1 = word_1[i] - 'a';
    int k2 = word_2[i] - 'a';
    if (word_1[i] != word_2[i]) {
      int v = 0;
      int min = INF;
      for (int h = 0; h < ALPH_SIZE; ++h) {
        if (min > d[k1][h] + d[k2][h]) {
          min = d[k1][h] + d[k2][h];
          word_1[i] = h + 'a';
        }
      }
      if (min >= INF) {
        cout << -1;
        return 0;
      }
      ans += min;
    }
  }
  cout << ans << endl;
  for (int i = 0; i < sss; ++i) {
    cout << word_1[i];
  }
  return 0;
}
