Problem: Boy Valera likes strings. And even more he likes them, when they are identical. That's why in his spare time Valera plays the following game. He takes any two strings, consisting of lower case Latin letters, and tries to make them identical. According to the game rules, with each move Valera can change one arbitrary character Ai in one of the strings into arbitrary character Bi, but he has to pay for every move a particular sum of money, equal to Wi. He is allowed to make as many moves as he needs. Since Valera is a very economical boy and never wastes his money, he asked you, an experienced programmer, to help him answer the question: what minimum amount of money should Valera have to get identical strings. 

Input

The first input line contains two initial non-empty strings s and t, consisting of lower case Latin letters. The length of each string doesn't exceed 105. The following line contains integer n (0 ≤ n ≤ 500) — amount of possible changings. Then follow n lines, each containing characters Ai and Bi (lower case Latin letters) and integer Wi (0 ≤ Wi ≤ 100), saying that it's allowed to change character Ai into character Bi in any of the strings and spend sum of money Wi.

Output

If the answer exists, output the answer to the problem, and the resulting string. Otherwise output -1 in the only line. If the answer is not unique, output any.

Examples

Input

uayd
uxxd
3
a x 8
x y 13
d c 3


Output

21
uxyd


Input

a
b
3
a b 2
a b 3
b a 5


Output

2
b


Input

abc
ab
6
a b 4
a b 7
b a 8
c b 11
c a 3
a c 0


Output

-1

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
map<int, long long> mp;
set<int> st;
vector<pair<long long, long long> > v[150];
set<int>::iterator P;
long long A[200001], n, m, i, j, t, k, w, ans;
string s, s1;
vector<pair<char, long long> > graph[501];
long long vis[150], vis1[150], sul[150][150];
char zz, z[150][150];
void update(char x, int k, char y) {
  vis1[x] = true;
  for (int j = 0; j < graph[x].size(); j++)
    if (!vis1[graph[x][j].first]) {
      for (int q = 0; q < v[y].size(); q++)
        if (v[y][q].first == graph[x][j].first &&
            v[y][q].second > k + graph[x][j].second) {
          v[y][q].second = k + graph[x][j].second;
          int vis2[150];
          for (int c = 'a'; c <= 'z'; c++) vis2[c] = vis1[c];
          memset(vis1, 0, sizeof(vis1));
          update(graph[x][j].first, k + graph[x][j].second, y);
          for (int c = 'a'; c <= 'z'; c++) vis1[c] = vis2[c];
        }
    }
}
void AllElement(char x, long long cost, char y) {
  vis[x] = true;
  for (int j = 0; j < graph[x].size(); j++)
    if (!vis[graph[x][j].first]) {
      v[y].push_back({graph[x][j].first, cost + graph[x][j].second});
      AllElement(graph[x][j].first, cost + graph[x][j].second, y);
    } else {
      for (int q = 0; q < v[y].size(); q++)
        if (v[y][q].first == graph[x][j].first &&
            v[y][q].second > cost + graph[x][j].second) {
          v[y][q].second = cost + graph[x][j].second;
          memset(vis1, 0, sizeof(vis1));
          update(v[y][q].first, v[y][q].second, y);
        }
    }
}
void short_path(char x, char y) {
  memset(vis, 0, sizeof(vis));
  AllElement(y, (long long)0, y);
  memset(vis, 0, sizeof(vis));
  AllElement(x, (long long)0, x);
  for (int i = 0; i < v[x].size(); i++)
    if (v[x][i].first == y && k > v[x][i].second) k = v[x][i].second, zz = y;
  for (int i = 0; i < v[y].size(); i++)
    if (v[y][i].first == x && k > v[y][i].second) k = v[y][i].second, zz = x;
  for (int i = 0; i < v[x].size(); i++)
    for (int j = 0; j < v[y].size(); j++)
      if (v[x][i].first == v[y][j].first) {
        int xx = v[x][i].second + v[y][j].second;
        if (k > xx) k = xx, zz = v[x][i].first;
      }
  v[y].clear();
  v[x].clear();
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  string s, t;
  char x, y;
  cin >> s >> t >> n;
  for (i = 0; i < n; i++) {
    bool l = false;
    cin >> x >> y >> w;
    if (x == y) continue;
    for (j = 0; j < graph[x].size(); j++)
      if (graph[x][j].first == y)
        graph[x][j].second = min(w, graph[x][j].second), l = true;
    if (!l) graph[x].push_back({y, w});
  }
  if (s.size() != t.size()) return cout << -1 << endl, 0;
  memset(sul, -1, sizeof(sul));
  for (i = 0; i < s.size(); i++)
    if (s[i] == t[i])
      continue;
    else {
      if (sul[t[i]][s[i]] != -1) swap(s[i], t[i]);
      if (sul[s[i]][t[i]] == -1) {
        k = 1e18;
        short_path(s[i], t[i]);
        sul[s[i]][t[i]] = k;
        z[s[i]][t[i]] = zz;
      }
      if (sul[s[i]][t[i]] == 1e18) return cout << -1, 0;
      ans += sul[s[i]][t[i]];
      s[i] = z[s[i]][t[i]];
    }
  cout << ans << endl << s;
  return 0;
}