#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#include <assert.h>
using namespace std;

const int N = 410;
bool dp[N][N];
char s[N], str[N], ans[N];
int l;
bool vis[N];
int n, m;
bool flag;

void dfs(int pos, int f) {
	if (flag) return;
    if (pos == n + 1) {
        flag = true;
		printf("%s\n", ans + 1);
        return;
    }

    bool v = true, c = true;
    for (int i = 1; i < pos; i++) {
		int tmp = s[ans[i] - 'a'] == 'V';
		v &= !dp[2 * i + tmp][pos << 1];
		v &= !dp[2 * pos + 1][2 * i + 1 - tmp];
		c &= !dp[2 * i + tmp][pos << 1 | 1];
		c &= !dp[2 * pos][2 * i + 1 - tmp];
	}

	int st1 = 0, st2 = 0;
    for (char i = f ? 'a': str[pos]; i < 'a' + l && !flag; i++) {
	    bool tmp = s[i - 'a'] == 'V';
	    if ((tmp && v) || (!tmp && c)) {
			if (tmp && v) st1++;
			else st2++;
			if (f && (st1 > 1 || st2 > 1)) break;
	        ans[pos] = i;
			dfs(pos + 1, f | (i > str[pos]));
		}
	}
}

int main()
{
	scanf("%s", s);
	l = strlen(s);
    for (int i = 0; i < l; i++)
	    vis[s[i] == 'V'] = 1;

	scanf("%d%d", &n, &m);
    if (!vis[0] || !vis[1])
	    for (int i = 1; i <= n; i++)
		    dp[2 * i + vis[0]][2 * i + 1 - vis[0]] = true;

    while (m--) {
	    char s1[3], s2[3];
	    int x, y;
	    scanf("%d%s%d%s", &x, s1, &y, s2);
        dp[2 * x + (s1[0] == 'V')][2 * y + (s2[0] == 'V')] = true;
		dp[2 * y + 1 - (s2[0] == 'V')][2 * x + 1 - (s1[0] == 'V')] = true;
	}

	for (int i = 2; i <= 2 * n + 1; i++)
		dp[i][i] = true;

	for (int k = 2; k <= 2 * n + 1; k++)
		for (int i = 2; i <= 2 * n + 1; i++)
			for (int j = 2; j <= 2 * n + 1; j++)
				dp[i][j] |= dp[i][k] & dp[k][j];

	scanf("%s", str + 1);
	for (int i = 2; i <= 2 * n + 1; i++)
		if (dp[i][i ^ 1] && dp[i ^ 1][i]) {
			printf("-1\n");
			return 0;
		}

	dfs(1, 0);
	if (!flag) {
		printf("-1\n");
		return 0;
	}

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#include <assert.h>
using namespace std;

const int N = 410;
bool dp[N][N];
char s[N], str[N], ans[N];
int l;
bool vis[N];
int n, m;
bool flag;

void dfs(int pos, int f) {
	if (flag) return;
    if (pos == n + 1) {
        flag = true;
		printf("%s\n", ans + 1);
        return;
    }

    bool v = true, c = true;
    for (int i = 1; i < pos; i++) {
		int tmp = s[ans[i] - 'a'] == 'V';
		v &= !dp[2 * i + tmp][pos << 1];
		v &= !dp[2 * pos + 1][2 * i + 1 - tmp];
		c &= !dp[2 * i + tmp][pos << 1 | 1];
		c &= !dp[2 * pos][2 * i + 1 - tmp];
	}

	int st1 = 0, st2 = 0;
    for (char i = f ? 'a': str[pos]; i < 'a' + l && !flag; i++) {
	    bool tmp = s[i - 'a'] == 'V';
	    if ((tmp && v) || (!tmp && c)) {
			if (tmp && v) st1++;
			else st2++;
			if (f && (st1 > 1 || st2 > 1)) break;
	        ans[pos] = i;
			dfs(pos + 1, f | (i > str[pos]));
		}
	}
}

int main()
{
	scanf("%s", s);
	l = strlen(s);
    for (int i = 0; i < l; i++)
	    vis[s[i] == 'V'] = 1;

	scanf("%d%d", &n, &m);
    if (!vis[0] || !vis[1])
	    for (int i = 1; i <= n; i++)
		    dp[2 * i + vis[0]][2 * i + 1 - vis[0]] = true;

    while (m--) {
	    char s1[3], s2[3];
	    int x, y;
	    scanf("%d%s%d%s", &x, s1, &y, s2);
        dp[2 * x + (s1[0] == 'V')][2 * y + (s2[0] == 'V')] = true;
		dp[2 * y + 1 - (s2[0] == 'V')][2 * x + 1 - (s1[0] == 'V')] = true;
	}

	for (int i = 1; i <= 2 * n; i++)
		dp[i][i] = true;

	for (int k = 1; k <= 2 * n; k++)
		for (int i = 1; i <= 2 * n; i++)
			for (int j = 1; j <= 2 * n; j++)
				dp[i][j] |= dp[i][k] & dp[k][j];

	//for (int i = 1; i <= n; i++) str[i] = 'a';
	scanf("%s", str + 1);
	//cout << "begin" << endl;
	dfs(1, 0);
	if (!flag) {
		printf("-1\n");
		return 0;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 444, M = N * N * 4;

vector<int> G[N], st;
int dfn[N], low[N], vis[N], bel[N], cnt, dfc;
void tarjan(int x) {
  st.push_back(x);
  vis[x] = 1;
  dfn[x] = low[x] = ++dfc;
  for (auto y : G[x]) {
    if (!dfn[y]) {
      tarjan(y);
      low[x] = min(low[x], low[y]);
    } else if (vis[y]) {
      low[x] = min(low[x], dfn[y]);
    }
  }
  if (low[x] == dfn[x]) {
    int y;
    ++cnt;
    do {
      y = st.back();
      st.pop_back();
      bel[y] = cnt;
      vis[y] = 0;
    } while (y != x);
  }
}
int n, m, cx1[M], cy1[M], cx2[M], cy2[M];
int fix, cur[N], all = -1;
char ans[N];
bool check() {
  cnt = dfc = 0;
  st.clear();
  for (int i = 0; i < n << 1; i++) {
    dfn[i] = low[i] = vis[i] = bel[i] = 0;
    G[i].clear();
  }
  for (int i = 0; i < m; i++) {
    G[cx1[i] << 1 |  cy1[i]].push_back(cx2[i] << 1 |  cy2[i]);
    G[cx2[i] << 1 | !cy2[i]].push_back(cx1[i] << 1 | !cy1[i]);
  }
  for (int i = 0; i < fix; i++) {
    G[i << 1 | !cur[i]].push_back(i << 1 | cur[i]);
  }
  if (~all) {
    for (int i = 0; i < n; i++) {
      G[i << 1 | !all].push_back(i << 1 | all);
    }
  }
  for (int i = 0; i < n << 1; i++) if (!dfn[i]) {
    tarjan(i);
  }
  for (int i = 0; i < n; i++) {
    if (bel[i << 1] == bel[i << 1 | 1]) {
      return false;
    }
  }
  return true;
}

char typ[30], s[N];
int type[30], L;

int diff(int x) {
  for (int i = x + 1; i < L; i++) {
    if (type[i] != type[x]) return i;
  }
  return -1;
}
int main() {
  scanf("%s%d%d", typ, &n, &m);
  L = (int) strlen(typ);
  for (int i = 0; i < L; i++) {
    type[i] = typ[i] == 'V';
  }
  if (*max_element(type, type + L) == 0) all = 0;
  if (*min_element(type, type + L) == 1) all = 1;
  for (int i = 0; i < m; i++) {
    char e[3], f[3];
    scanf("%d%s%d%s", &cx1[i], e, &cx2[i], f);
    --cx1[i], --cx2[i];
    cy1[i] = e[0] == 'V';
    cy2[i] = f[0] == 'V';
  }
  scanf("%s", s);
  for (int i = 0; i < n; i++) {
    ans[i] = s[i];
    s[i] -= 'a';
    cur[i] = type[s[i]];
  }
  fix = n;
  if (check()) {
    printf("%s\n", ans);
    return 0;
  }
  int mx;
  for (mx = n - 1; mx >= 0; mx--) {
    fix = mx + 1;
    if (s[mx] == L - 1) continue;
    cur[mx] = type[s[mx] + 1];
    ans[mx] = s[mx] + 1 + 'a';
    if (check()) break;
    if (!~diff(s[mx] + 1)) continue;
    cur[mx] = !cur[mx];
    ans[mx] = diff(s[mx] + 1) + 'a';
    if (check()) break;
  }
  if (mx < 0) {
    puts("-1");
    return 0;
  }
  for (int i = mx + 1; i < n; i++) {
    cur[i] = type[0];
    ans[i] = 'a';
    fix = i + 1;
    if (check()) continue;
    cur[i] = !type[0];
    ans[i] = diff(0) + 'a';
  }
  printf("%s\n", ans);
}
// 1


#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int MN = 405;

int N , M , Sig , clk , top , cnt;
int ins[MN] , dfn[MN] , low[MN] , st[MN] , nxt[MN][2] , bel[MN];
bitset < 500 > Now[MN] , Forbid[MN] , Tmp;
vector < int > Dom[MN];
char VC[MN] , A[MN] , B[MN];
vector < int > g[MN];

template <typename T> inline void chkmax(T &x , T y) { x = max(x , y); }
template <typename T> inline void chkmin(T &x , T y) { x = min(x , y); }
template <typename T> inline void read(T &x) {
	T f = 1; x = 0;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
	x *= f;
}

inline void Tarjan(int u) {
	dfn[u] = low[u] = ++clk; ins[u] = true; st[++top] = u;
	for (auto v : g[u]) {
		if (dfn[v] == 0) {
			Tarjan(v);
			chkmin(low[u] , low[v]);
		} else if (ins[v]) chkmin(low[u] , dfn[v]);
	}
	if (dfn[u] == low[u]) {
		int v = -1; ++cnt;
		do {
			v = st[top--];  bel[v] = cnt;
			ins[v] = false;
			Dom[cnt].emplace_back(v);
		} while (v != u);
	}
}

inline bool DFS(int i , int t) {
	if (i == N) return printf("%s\n", B), 1;
	int c = 0;
	if (!t) {
		int k = VC[A[i] - 'a'] == 'C';
		if (!Now[i][i << 1 | k]) {
			Now[i + 1] = Now[i] | Forbid[i << 1 | k];
			B[i] = A[i];
			if (DFS(i + 1, 0)) return 1;
		}
		c = A[i] - 'a' + 1;
	}
	int o = nxt[c][0] > nxt[c][1];
	if (nxt[c][o] < Sig && !Now[i][i << 1 | o]) {
		Now[i + 1] = Now[i] | Forbid[i << 1 | o];
		B[i] = nxt[c][o] + 'a';
		if (DFS(i + 1, 1)) return 1;
	} o ^= 1;
	if (nxt[c][o] < Sig && !Now[i][i << 1 | o]) {
		Now[i + 1] = Now[i] | Forbid[i << 1 | o];
		B[i] = nxt[c][o] + 'a';
		if (DFS(i + 1, 1)) return 1;
	}
	return 0;
}

int main() {

   	scanf("%s", VC), Sig = strlen(VC);
	nxt[Sig][0] = nxt[Sig][1] = Sig;
	for (int i = Sig - 1; i >= 0; --i)
		nxt[i][0] = VC[i] == 'V' ? i : nxt[i + 1][0],
		nxt[i][1] = VC[i] == 'C' ? i : nxt[i + 1][1];
	scanf("%d%d", &N, &M);
	for (int i = 1; i <= M; ++i) {
		int u, v; char U[3], V[3];
		scanf("%d%s%d%s", &u, U, &v, V), --u, --v;
		(u <<= 1) |= *U == 'C', (v <<= 1) |= *V == 'C';
		g[u].push_back(v), g[v ^ 1].push_back(u ^ 1);
	}
	for (int i = 0; i < N + N; ++i) if (!dfn[i]) Tarjan(i);
	for (int i = 0; i < N; ++i)
		if (bel[i << 1] == bel[i << 1 | 1])
			return puts("-1"), 0;
	for (int i = 1; i <= cnt; ++i) {
		Tmp.reset();
		for (auto u : Dom[i]) Tmp[u ^ 1] = 1;
		for (auto u : Dom[i]) for (auto v : g[u]) Tmp |= Forbid[v];
		for (auto u : Dom[i]) Forbid[u] = Tmp;
	}
	scanf("%s", A);
	if (!DFS(0, 0)) puts("-1");
   return 0;
}


#include <bits/stdc++.h>
#define ID(x) (x<<1)
#define NOT(x) (x^1)
#define all(x) x.begin(), x.end()
using namespace std;
const int mx = 412;
int madj[mx][mx];
int assignment[mx];
void add(int u, int v, bool f = 1){
	madj[NOT(u)][v] = 1;
	if(f) add(v, u, 0);
}
void dfs(int u, int n){
	assignment[u] = 1;
	assignment[NOT(u)] = 0;
	for(int i = 0; i < 2*n; i++){
		if(i != u && madj[u][i]){
			if(assignment[i] != 1){
				assignment[i] = 1;
				assignment[NOT(i)] = 0;
			}
		}
	}
}
vector<char> vowels, consonants;
string s, t, ans;
bool build(int on, int n, bool f = 0){
	if(on == n){
		ans = min(ans, t);
		return 1;
	}
	bool found = 0;
	if(assignment[ID(on)] == -1){
		dfs(ID(on), n);
		for(auto it = vowels.begin(); it != vowels.end(); it++){
			if(*it >= s[on] || f){
				t[on] = *it;
				if(build(on+1, n, f||(*it > s[on]))){
					found = 1;
					break;
				}
			}
		}
		dfs(NOT(ID(on)), n);
		for(auto it = consonants.begin(); it != consonants.end(); it++){
			if(*it >= s[on] || f){
				t[on] = *it;
				if(build(on+1, n, f||(*it > s[on]))){
					found = 1;
					break;
				}
			}
		}
	}else{
		if(assignment[ID(on)]){
			for(auto it = vowels.begin(); it != vowels.end(); it++){
				if(*it >= s[on] || f){
					t[on] = *it;
					if(build(on+1, n, f||(*it > s[on]))){
						found = 1;
						break;
					}
				}
			}
		}else{
			for(auto it = consonants.begin(); it != consonants.end(); it++){
				if(*it >= s[on] || f){
					t[on] = *it;
					if(build(on+1, n, f||(*it > s[on]))){
						found = 1;
						break;
					}
				}
			}
		}
	}
	return found;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n, m;
	string funct;
	cin >> funct;
	for(int i = 0; i < funct.size(); i++){
		if(funct[i] == 'V') vowels.push_back(i+'a');
		else consonants.push_back(i+'a');
	}
	cin >> n >> m;
	for(int i = 0; i < m; i++){
		int pos0, pos1;
		char t0, t1;
		cin >> pos0 >> t0 >> pos1 >> t1;
		pos0--, pos1--;
		int l0 = ID(pos0), l1 = ID(pos1);
		if(t0 != 'V') l0 = NOT(l0);
		if(t1 != 'V') l1 = NOT(l1);
		add(NOT(l0), l1, 1);
	}
	for(int k = 0; k < 2*n; k++){
		for(int i = 0; i < 2*n; i++){
			for(int j = 0; j < 2*n; j++){
				madj[i][j] |= (madj[i][k] & madj[k][j]);
			}
		}
	}
	memset(assignment, -1, sizeof assignment);
	for(int i = 0; i < 2*n; i++){
		if(madj[i][NOT(i)] && madj[NOT(i)][i]){
			cout << -1 << endl;
			return 0;
		}else if(madj[i][NOT(i)]){

		}else if(madj[NOT(i)][i]){
			if(assignment[i] == -1){
				dfs(i, n);
			}
		}
	}
	cin >> s;
	for(int i = 0; i < n; i++) ans.push_back('z');
	t.resize(n);
	if(build(0, n)){
		cout << ans << endl;
	}else{
		cout << -1 << endl;
	}
}


#include <bits/stdc++.h>
#define ID(x) (x<<1)
#define NOT(x) (x^1)
#define all(x) x.begin(), x.end()
using namespace std;
const int mx = 412;
int madj[mx][mx];
int assignment[mx];
void add(int u, int v, bool f = 1){
	madj[NOT(u)][v] = 1;
	if(f) add(v, u, 0);
}
stack<int> st;
void dfs(int u, int n){
	if(~assignment[u]) return;
	st.push(u);
	assignment[u] = 1;
	assignment[NOT(u)] = 0;
	for(int i = 0; i < 2*n; i++){
		if(i != u && madj[u][i]) dfs(i, n);
	}
}
vector<char> vowels, consonants;
string s, t, ans;
bool build(int on, int n, bool f = 0){
	if(on == n){
		ans = min(ans, t);
		return 1;
	}
	bool found = 0;
	if(assignment[ID(on)] == -1){
		dfs(ID(on), n);
		for(auto it = vowels.begin(); it != vowels.end(); it++){
			if(*it >= s[on] || f){
				t[on] = *it;
				if(build(on+1, n, f||(*it > s[on]))){
					found = 1;
					break;
				}
			}
		}
		while(!st.empty()){
			int u = st.top();
			st.pop();
			assignment[u] = assignment[NOT(u)] = -1;
			if(u == ID(on)) break;
		}
		dfs(NOT(ID(on)), n);
		for(auto it = consonants.begin(); it != consonants.end(); it++){
			if(*it >= s[on] || f){
				t[on] = *it;
				if(build(on+1, n, f||(*it > s[on]))){
					found = 1;
					break;
				}
			}
		}
		while(!st.empty()){
			int u = st.top();
			st.pop();
			assignment[u] = assignment[NOT(u)] = -1;
			if(u == NOT(ID(on))) break;
		}
	}else{
		if(assignment[ID(on)]){
			for(auto it = vowels.begin(); it != vowels.end(); it++){
				if(*it >= s[on] || f){
					t[on] = *it;
					if(build(on+1, n, f||(*it > s[on]))){
						found = 1;
						break;
					}
				}
			}
		}else{
			for(auto it = consonants.begin(); it != consonants.end(); it++){
				if(*it >= s[on] || f){
					t[on] = *it;
					if(build(on+1, n, f||(*it > s[on]))){
						found = 1;
						break;
					}
				}
			}
		}
	}
	return found;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n, m;
	string funct;
	cin >> funct;
	for(int i = 0; i < funct.size(); i++){
		if(funct[i] == 'V') vowels.push_back(i+'a');
		else consonants.push_back(i+'a');
	}
	cin >> n >> m;
	for(int i = 0; i < m; i++){
		int pos0, pos1;
		char t0, t1;
		cin >> pos0 >> t0 >> pos1 >> t1;
		pos0--, pos1--;
		int l0 = ID(pos0), l1 = ID(pos1);
		if(t0 != 'V') l0 = NOT(l0);
		if(t1 != 'V') l1 = NOT(l1);
		add(NOT(l0), l1, 1);
	}
	for(int k = 0; k < 2*n; k++){
		for(int i = 0; i < 2*n; i++){
			for(int j = 0; j < 2*n; j++){
				madj[i][j] |= (madj[i][k] & madj[k][j]);
			}
		}
	}
	memset(assignment, -1, sizeof assignment);
	for(int i = 0; i < 2*n; i++){
		if(madj[i][NOT(i)] && madj[NOT(i)][i]){
			cout << -1 << endl;
			return 0;
		}else if(madj[i][NOT(i)]){
			if(assignment[i] == -1){
				dfs(i, n);
			}
		}else if(madj[NOT(i)][i]){

		}
	}
	cin >> s;
	for(int i = 0; i < n; i++) ans.push_back('z');
	t.resize(n);
	if(build(0, n)){
		cout << ans << endl;
	}else{
		cout << -1 << endl;
	}
}


#include <bits/stdc++.h>
#define ID(x) (x<<1)
#define NOT(x) (x^1)
#define all(x) x.begin(), x.end()
using namespace std;
const int mx = 412;
int madj[mx][mx];
int assignment[mx], assignmentbkp[mx];
vector<int> reachable[mx];
void add(int u, int v, bool f = 1){
    madj[NOT(u)][v] = 1;
    if(f) add(v, u, 0);
}
bool assign(int u){
    assignment[u] = 1;
    assignment[NOT(u)] = 0;
    stack<int> roll;
    for(const auto &v : reachable[u]){
        if(assignment[v] == -1 || assignment[v] == 1){
            assignment[v] = 1;
            assignment[NOT(v)] = 0;
        }else{
            while(!roll.empty()){
                int x = roll.top();
                roll.pop();
                assignment[x] = assignment[NOT(x)] = -1;
                return 0;
            }
        }
    }
    return 1;
}
vector<char> vowels, consonants;
char isV[26];
bool sat(string prefix){
    for(int i = 0; i < prefix.size(); i++){
        if(isV[prefix[i]-'a']){
            if(assignment[ID(i)] == -1) assign(ID(i));
            else if(assignment[ID(i)] != 1) return 0;
        }else{
            if(assignment[NOT(ID(i))] == -1) assign(NOT(ID(i)));
            else if(assignment[NOT(ID(i))] != 1) return 0;
        }
    }
    return 1;
}
bool build(char first, int pos, string &suffix){
    if(isV[first-'a']){
        if(assignment[ID(pos)] == -1) assign(ID(pos));
        else if(assignment[ID(pos)] != 1) return 0;
    }else{
        if(assignment[NOT(ID(pos))] == -1) assign(NOT(ID(pos)));
        else if(assignment[NOT(ID(pos))] != 1) return 0;
    }
    suffix[0] = first;
    char bestv = 0, bestc = 0;
    if(vowels.size()) bestv = vowels[0];
    if(consonants.size()) bestc = consonants[0];
    for(int i = 1+pos; i < suffix.size()+pos; i++){
       if(assignment[ID(i)] == -1){
            if(bestv && bestc){
                if(bestv < bestc && assign(ID(i))){
                    suffix[i-pos] = bestv;
                }else if(assign(NOT(ID(i)))){
                    suffix[i-pos] = bestc;
                }else{
                    return 0;
                }
            }else if(bestv){
                if(assignment[ID(i)] == -1){
                    if(assign(ID(i))){
                        suffix[i-pos] = bestv;
                    }else{
                        return 0;
                    }
                }else if(assignment[ID(i)] != 1) return 0;
            }else if(bestc){
                if(assignment[NOT(ID(i))] == -1){
                    if(assign(NOT(ID(i)))){
                        suffix[i-pos] = bestc;
                    }else{
                        return 0;
                    }
                }else if(assignment[NOT(ID(i))] != 1) return 0;
            }else{
                return 0;
            }
        }else if(assignment[ID(i)] == 1 && bestv){
            suffix[i-pos] = bestv;
        }else if(assignment[ID(i)] == 0 && bestc){
            suffix[i-pos] = bestc;
        }else{
            return 0;
        }
    }
    return 1;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    string funct;
    cin >> funct;
    for(int i = 0; i < funct.size(); i++){
        if(funct[i] == 'V') vowels.push_back(i+'a');
        else consonants.push_back(i+'a');
        isV[i] = (funct[i] == 'V');
    }
    cin >> n >> m;
    for(int i = 0; i < m; i++){
        int pos0, pos1;
        char t0, t1;
        cin >> pos0 >> t0 >> pos1 >> t1;
        pos0--, pos1--;
        int l0 = ID(pos0), l1 = ID(pos1);
        if(t0 != 'V') l0 = NOT(l0);
        if(t1 != 'V') l1 = NOT(l1);
        add(NOT(l0), l1, 1);
    }
    for(int k = 0; k < 2*n; k++){
        for(int i = 0; i < 2*n; i++){
            for(int j = 0; j < 2*n; j++){
                madj[i][j] |= (madj[i][k] & madj[k][j]);
            }
        }
    }
    for(int i = 0; i < 2*n; i++){
        for(int j = 0; j < 2*n; j++){
            if(madj[i][j]){
                reachable[i].push_back(j);
            }
        }
    }
    memset(assignment, -1, sizeof assignment);
    for(int i = 0; i < 2*n; i++){
        if(madj[i][NOT(i)] && madj[NOT(i)][i]){
            cout << -1 << endl;
            return 0;
        }else if(madj[NOT(i)][i]){
            if(assignment[i] == -1){
                if(!assign(i)){
                    cout << -1 << endl;
                    return 0;
                }
            }
        }

    }
    string s, ans;
    cin >> s;
    for(int i = 0; i < n; i++) ans.push_back('z'+1);
    string prefix, suffix;
    for(int i = 0; i < n; i++){
        if(sat(prefix)){
            char bestv = 0, bestc = 0;
            for(const char &c : vowels){
                if(c > s[i]){
                    bestv = c;
                    break;
                }
            }
            for(const char &c : consonants){
                if(c > s[i]){
                    bestc = c;
                    break;
                }
            }
            suffix.resize(n-prefix.size());
            for(int j = 0; j < 2*n; j++) assignmentbkp[j] = assignment[j];
            if(bestv && build(bestv, prefix.size(), suffix)){
                ans = min(ans, prefix+suffix);
            }
            swap(assignmentbkp, assignment);
            for(int j = 0; j < 2*n; j++) assignmentbkp[j] = assignment[j];
            if(bestc && build(bestc, prefix.size(), suffix)){
                ans = min(ans, prefix+suffix);
            }
            swap(assignmentbkp, assignment);
        }
        prefix.push_back(s[i]);
    }
    if(sat(prefix)){
        ans = min(ans, prefix);
    }
    if(ans[0] != 'z'+1){
        cout << ans << endl;
    }else{
        cout << -1 << endl;
    }
}


#include <bits/stdc++.h>
#define ID(x) (x<<1)
#define NOT(x) (x^1)
#define all(x) x.begin(), x.end()
using namespace std;
const int mx = 412;
int madj[mx][mx];
int assignment[mx];
vector<int> reachable[mx];
void add(int u, int v, bool f = 1){
	madj[NOT(u)][v] = 1;
	if(f) add(v, u, 0);
}
vector<int> assign(int u, int n){
	assignment[u] = 1;
	assignment[NOT(u)] = 0;
	vector<int> a;
	a.push_back(u);
	for(const auto &v : reachable[u]){
		if(assignment[v] != 1){
			assignment[v] = 1;
			assignment[NOT(v)] = 0;
			a.push_back(v);
		}
	}
	return a;
}
vector<char> vowels, consonants;
string s, t, ans;
bool build(int on, int n, bool f = 0){
	if(on == n){
		ans = min(ans, t);
		return 1;
	}
	bool found = 0;
	if(assignment[ID(on)] == -1){
		vector<int> a = assign(ID(on), n);
		for(auto it = vowels.begin(); it != vowels.end(); it++){
			if(*it >= s[on] || f){
				t[on] = *it;
				if(build(on+1, n, f||(*it > s[on]))){
					found = 1;
					break;
				}
			}
		}
		for(const int &v : a) assignment[v] = assignment[NOT(v)] = -1;
		a = assign(NOT(ID(on)), n);
		for(auto it = consonants.begin(); it != consonants.end(); it++){
			if(*it >= s[on] || f){
				t[on] = *it;
				if(build(on+1, n, f||(*it > s[on]))){
					found = 1;
					break;
				}
			}
		}
		for(const int &v : a) assignment[v] = assignment[NOT(v)] = -1;
	}else{
		if(assignment[ID(on)]){
			for(auto it = vowels.begin(); it != vowels.end(); it++){
				if(*it >= s[on] || f){
					t[on] = *it;
					if(build(on+1, n, f||(*it > s[on]))){
						found = 1;
						break;
					}
				}
			}
		}else{
			for(auto it = consonants.begin(); it != consonants.end(); it++){
				if(*it >= s[on] || f){
					t[on] = *it;
					if(build(on+1, n, f||(*it > s[on]))){
						found = 1;
						break;
					}
				}
			}
		}
	}
	return found;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n, m;
	string funct;
	cin >> funct;
	for(int i = 0; i < funct.size(); i++){
		if(funct[i] == 'V') vowels.push_back(i+'a');
		else consonants.push_back(i+'a');
	}
	cin >> n >> m;
	for(int i = 0; i < m; i++){
		int pos0, pos1;
		char t0, t1;
		cin >> pos0 >> t0 >> pos1 >> t1;
		pos0--, pos1--;
		int l0 = ID(pos0), l1 = ID(pos1);
		if(t0 != 'V') l0 = NOT(l0);
		if(t1 != 'V') l1 = NOT(l1);
		add(NOT(l0), l1, 1);
	}
	for(int k = 0; k < 2*n; k++){
		for(int i = 0; i < 2*n; i++){
			for(int j = 0; j < 2*n; j++){
				madj[i][j] |= (madj[i][k] & madj[k][j]);
			}
		}
	}
	memset(assignment, -1, sizeof assignment);
	for(int i = 0; i < 2*n; i++){
		if(madj[i][NOT(i)] && madj[NOT(i)][i]){
			cout << -1 << endl;
			return 0;
		}else if(madj[NOT(i)][i]){
			if(assignment[i] == -1){
				assign(i, n);
			}
		}
		for(int j = 0; j < 2*n; j++){
			if(madj[i][j]){
				reachable[i].push_back(j);
			}
		}
	}
	cin >> s;
	for(int i = 0; i < n; i++) ans.push_back('z');
	t.resize(n);
	if(build(0, n)){
		cout << ans << endl;
	}else{
		cout << -1 << endl;
	}
}


#include <bits/stdc++.h>
#define ID(x) (x<<1)
#define NOT(x) (x^1)
#define all(x) x.begin(), x.end()
using namespace std;
const int mx = 412;
int madj[mx][mx];
int assignment[mx];
void add(int u, int v, bool f = 1){
	madj[NOT(u)][v] = 1;
	if(f) add(v, u, 0);
}
stack<int> st;
void dfs(int u, int n){
	if(~assignment[u]) return;
	st.push(u);
	assignment[u] = 1;
	assignment[NOT(u)] = 0;
	for(int i = 0; i < 2*n; i++){
		if(i != u && madj[u][i]) dfs(i, n);
	}
}
vector<char> vowels, consonants;
string s, t, ans;
bool build(int on, int n, bool f = 0){
	if(on == n){
		ans = min(ans, t);
		return 1;
	}
	bool found = 0;
	if(assignment[ID(on)] == -1){
		dfs(ID(on), n);
		for(auto it = vowels.begin(); it != vowels.end(); it++){
			if(*it >= s[on] || f){
				t[on] = *it;
				if(build(on+1, n, f||(*it > s[on]))){
					found = 1;
					break;
				}
			}
		}
		while(!st.empty()){
			int u = st.top();
			st.pop();
			assignment[u] = assignment[NOT(u)] = -1;
			if(u == ID(on)) break;
		}
		dfs(NOT(ID(on)), n);
		for(auto it = consonants.begin(); it != consonants.end(); it++){
			if(*it >= s[on] || f){
				t[on] = *it;
				if(build(on+1, n, f||(*it > s[on]))){
					found = 1;
					break;
				}
			}
		}
		while(!st.empty()){
			int u = st.top();
			st.pop();
			assignment[u] = assignment[NOT(u)] = -1;
			if(u == NOT(ID(on))) break;
		}
	}else{
		if(assignment[ID(on)]){
			for(auto it = vowels.begin(); it != vowels.end(); it++){
				if(*it >= s[on] || f){
					t[on] = *it;
					if(build(on+1, n, f||(*it > s[on]))){
						found = 1;
						break;
					}
				}
			}
		}else{
			for(auto it = consonants.begin(); it != consonants.end(); it++){
				if(*it >= s[on] || f){
					t[on] = *it;
					if(build(on+1, n, f||(*it > s[on]))){
						found = 1;
						break;
					}
				}
			}
		}
	}
	return found;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n, m;
	string funct;
	cin >> funct;
	for(int i = 0; i < funct.size(); i++){
		if(funct[i] == 'V') vowels.push_back(i+'a');
		else consonants.push_back(i+'a');
	}
	cin >> n >> m;
	for(int i = 0; i < m; i++){
		int pos0, pos1;
		char t0, t1;
		cin >> pos0 >> t0 >> pos1 >> t1;
		pos0--, pos1--;
		int l0 = ID(pos0), l1 = ID(pos1);
		if(t0 != 'V') l0 = NOT(l0);
		if(t1 != 'V') l1 = NOT(l1);
		add(NOT(l0), l1, 1);
	}
	for(int k = 0; k < 2*n; k++){
		for(int i = 0; i < 2*n; i++){
			for(int j = 0; j < 2*n; j++){
				madj[i][j] |= (madj[i][k] & madj[k][j]);
			}
		}
	}
	memset(assignment, -1, sizeof assignment);
	for(int i = 0; i < 2*n; i++){
		if(madj[i][NOT(i)] && madj[NOT(i)][i]){
			cout << -1 << endl;
			return 0;
		}else if(madj[i][NOT(i)]){

		}else if(madj[NOT(i)][i]){
			if(assignment[i] == -1){
				dfs(i, n);
			}
		}
	}
	cin >> s;
	for(int i = 0; i < n; i++) ans.push_back('z');
	t.resize(n);
	if(build(0, n)){
		cout << ans << endl;
	}else{
		cout << -1 << endl;
	}
}


#include <bits/stdc++.h>
#define clr(x) memset(x,0,sizeof x)
#define For(i,a,b) for (int i=(a);i<=(b);i++)
#define Fod(i,b,a) for (int i=(b);i>=(a);i--)
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define fi first
#define se second
#define outval(x) cerr<<#x" = "<<x<<endl
#define outtag(x) cerr<<"-----------------"#x"-----------------\n"
#define outarr(a,L,R) cerr<<#a"["<<L<<".."<<R<<"] = ";\
                    For(_x,L,R) cerr<<a[_x]<<" ";cerr<<endl;
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair <int,int> pii;
LL read(){
    LL x=0,f=0;
    char ch=getchar();
    while (!isdigit(ch))
        f=ch=='-',ch=getchar();
    while (isdigit(ch))
        x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return f?-x:x;
}
const int N=205*2;
int n,m,b;
vector <int> e[N];
char s[N];
int id(int a,int t){
	return a+t*n;
}
int g[N][N];
void dfs(int s,int x){
	if (g[s][x])
		return;
	g[s][x]=1;
	for (auto y : e[x])
		dfs(s,y);
}
string mis,str;
int stp=0;
void solve(int i,string S,int flag){
//	cout<<"Solve("<<i<<","<<S<<","<<flag<<")\n";
	if (i==n){
		cout<<S<<endl;
		exit(0);
	}
	For(c,flag?'a':mis[i],'a'+b-1){
		if (S[i]!=' '&&s[c-'a']!=S[i])
			continue;
		string ss=S;
		ss[i]=c;
		int x=id(i+1,s[c-'a']=='V');
		int f=0;
		For(y,1,n)
			if (g[x][y])
				if (y>i+1){
					f|=ss[y-1]=='V';
					ss[y-1]='C';
					f|=stp==1;
				}
		For(y,n+1,n*2)
			if (g[x][y])
				if (y-n>i+1){
					f|=ss[y-n-1]=='C';
					ss[y-n-1]='V';
					f|=stp==-1;
				}
		if (f)
			continue;
		solve(i+1,ss,flag||c>mis[i]);
	}
}
void gettp(){
	For(i,1,b-1)
		if (s[i]!=s[0])
			return;
	if (s[0]=='C')
		stp=-1;
	else
		stp=1;
}
int main(){
	cin>>s;
	b=strlen(s);
	gettp();
	n=read(),m=read();
	For(i,1,m){
		char at[2],bt[2];
		int p1,p2;
		cin>>p1>>at>>p2>>bt;
		int t1=at[0]=='V',t2=bt[0]=='V';
		e[id(p1,t1)].pb(id(p2,t2));
		e[id(p2,t2^1)].pb(id(p1,t1^1));
	}
	For(i,1,n*2)
		dfs(i,i);
//	For(i,1,n*2){outarr(g[i],1,n*2);}
//	For(i,1,n*2)
//		For(j,1,n*2)
//			if (i!=j&&g[i][j])
//				printf("(%d,%d)\n",i,j);
	cin>>mis;
	For(i,1,n)
		if (g[i][i+n])
			return puts("-1"),0;
	str="";
	For(i,1,n)
		str+=" ";
	solve(0,str,0);
	puts("-1");
	return 0;
}


#include <bits/stdc++.h>
#define clr(x) memset(x,0,sizeof x)
#define For(i,a,b) for (int i=(a);i<=(b);i++)
#define Fod(i,b,a) for (int i=(b);i>=(a);i--)
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define fi first
#define se second
#define outval(x) cerr<<#x" = "<<x<<endl
#define outtag(x) cerr<<"-----------------"#x"-----------------\n"
#define outarr(a,L,R) cerr<<#a"["<<L<<".."<<R<<"] = ";\
                    For(_x,L,R) cerr<<a[_x]<<" ";cerr<<endl;
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair <int,int> pii;
LL read(){
    LL x=0,f=0;
    char ch=getchar();
    while (!isdigit(ch))
        f=ch=='-',ch=getchar();
    while (isdigit(ch))
        x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return f?-x:x;
}
const int N=205*2;
int n,m,b;
vector <int> e[N];
char s[N];
int id(int a,int t){
	return a+t*n;
}
int g[N][N];
void dfs(int s,int x){
	if (g[s][x])
		return;
	g[s][x]=1;
	for (auto y : e[x])
		dfs(s,y);
}
string mis,str;
int stp=0;
void solve(int i,string S,int flag){
//	cout<<"Solve("<<i<<","<<S<<","<<flag<<")\n";
	if (i==n){
		cout<<S<<endl;
		exit(0);
	}
	For(c,flag?'a':mis[i],'a'+b-1){
		if (S[i]!=' '&&s[c-'a']!=S[i])
			continue;
		string ss=S;
		ss[i]=c;
		int x=id(i+1,s[c-'a']=='V');
		int f=0;
		For(y,1,n)
			if (g[x][y])
				if (y>i+1){
					f|=ss[y-1]=='V';
					ss[y-1]='C';
					f|=stp==1;
				}
		For(y,n+1,n*2)
			if (g[x][y])
				if (y-n>i+1){
					f|=ss[y-n-1]=='C';
					ss[y-n-1]='V';
					f|=stp==-1;
				}
		if (f)
			continue;
		solve(i+1,ss,flag||c>mis[i]);
	}
}
void gettp(){
	For(i,1,b-1)
		if (s[i]!=s[0])
			return;
	if (s[0]=='C')
		stp=-1;
	else
		stp=1;
}
int main(){
	cin>>s;
	b=strlen(s);
	gettp();
	n=read(),m=read();
	For(i,1,m){
		char at[2],bt[2];
		int p1,p2;
		cin>>p1>>at>>p2>>bt;
		int t1=at[0]=='V',t2=bt[0]=='V';
		e[id(p1,t1)].pb(id(p2,t2));
		e[id(p2,t2^1)].pb(id(p1,t1^1));
	}
	For(i,1,n*2)
		dfs(i,i);
//	For(i,1,n*2){outarr(g[i],1,n*2);}
//	For(i,1,n*2)
//		For(j,1,n*2)
//			if (i!=j&&g[i][j])
//				printf("(%d,%d)\n",i,j);
	cin>>mis;
	str="";
	For(i,1,n)
		str+=" ";
	solve(0,str,0);
	puts("-1");
	return 0;
}


#include <bits/stdc++.h>
#define clr(x) memset(x,0,sizeof x)
#define For(i,a,b) for (int i=(a);i<=(b);i++)
#define Fod(i,b,a) for (int i=(b);i>=(a);i--)
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define fi first
#define se second
#define outval(x) cerr<<#x" = "<<x<<endl
#define outtag(x) cerr<<"-----------------"#x"-----------------\n"
#define outarr(a,L,R) cerr<<#a"["<<L<<".."<<R<<"] = ";\
                    For(_x,L,R) cerr<<a[_x]<<" ";cerr<<endl;
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair <int,int> pii;
LL read(){
    LL x=0,f=0;
    char ch=getchar();
    while (!isdigit(ch))
        f=ch=='-',ch=getchar();
    while (isdigit(ch))
        x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return f?-x:x;
}
const int N=205*2;
int n,m,b;
vector <int> e[N];
char s[N];
int id(int a,int t){
	return a+t*n;
}
int g[N][N];
void dfs(int s,int x){
	if (g[s][x])
		return;
	g[s][x]=1;
	for (auto y : e[x])
		dfs(s,y);
}
string mis,str;
int stp=0;
void solve(int i,string S,int flag){
//	cout<<"Solve("<<i<<","<<S<<","<<flag<<")\n";
	if (i==n){
		cout<<S<<endl;
		exit(0);
	}
	For(c,flag?'a':mis[i],'a'+b-1){
		if (S[i]!=' '&&s[c-'a']!=S[i])
			continue;
		string ss=S;
		ss[i]=c;
		int x=id(i+1,s[c-'a']=='V');
		int f=0;
		For(y,1,n)
			if (g[x][y])
				if (y>i+1){
					f|=ss[y-1]=='V';
					ss[y-1]='C';
					f|=stp==1;
				}
		For(y,n+1,n*2)
			if (g[x][y])
				if (y-n>i+1){
					f|=ss[y-n-1]=='C';
					ss[y-n-1]='V';
					f|=stp==-1;
				}
		if (f)
			continue;
		solve(i+1,ss,flag||c>mis[i]);
	}
}
void gettp(){
	For(i,1,b-1)
		if (s[i]!=s[0])
			return;
	if (s[0]=='C')
		stp=-1;
	else
		stp=1;
}
int main(){
	cin>>s;
	b=strlen(s);
	gettp();
	n=read(),m=read();
	For(i,1,m){
		char at[2],bt[2];
		int p1,p2;
		cin>>p1>>at>>p2>>bt;
		int t1=at[0]=='V',t2=bt[0]=='V';
		e[id(p1,t1)].pb(id(p2,t2));
		e[id(p2,t2^1)].pb(id(p1,t1^1));
	}
	For(i,1,n*2)
		dfs(i,i);
//	For(i,1,n*2){outarr(g[i],1,n*2);}
//	For(i,1,n*2)
//		For(j,1,n*2)
//			if (i!=j&&g[i][j])
//				printf("(%d,%d)\n",i,j);
	cin>>mis;
	For(i,1,n)
		if (g[i][i+n]&&g[i+n][i])
			return puts("-1"),0;
	str="";
	For(i,1,n)
		str+=" ";
	solve(0,str,0);
	puts("-1");
	return 0;
}


#include <bits/stdc++.h>
#define clr(x) memset(x,0,sizeof x)
#define For(i,a,b) for (int i=(a);i<=(b);i++)
#define Fod(i,b,a) for (int i=(b);i>=(a);i--)
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define fi first
#define se second
#define outval(x) cerr<<#x" = "<<x<<endl
#define outtag(x) cerr<<"-----------------"#x"-----------------\n"
#define outarr(a,L,R) cerr<<#a"["<<L<<".."<<R<<"] = ";\
                    For(_x,L,R) cerr<<a[_x]<<" ";cerr<<endl;
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair <int,int> pii;
LL read(){
    LL x=0,f=0;
    char ch=getchar();
    while (!isdigit(ch))
        f=ch=='-',ch=getchar();
    while (isdigit(ch))
        x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return f?-x:x;
}
const int N=205*2;
int n,m,b;
vector <int> e[N];
char s[N];
int id(int a,int t){
	return a+t*n;
}
int g[N][N];
void dfs(int s,int x){
	if (g[s][x])
		return;
	g[s][x]=1;
	for (auto y : e[x])
		dfs(s,y);
}
string mis,str;
void solve(int i,string S,int flag){
//	cout<<"Solve("<<i<<","<<S<<","<<flag<<")\n";
	if (i==n){
		cout<<S<<endl;
		exit(0);
	}
	For(c,flag?'a':mis[i],'a'+b-1){
		if (S[i]!=' '&&s[c-'a']!=S[i])
			continue;
		string ss=S;
		ss[i]=c;
		int x=id(i+1,s[c-'a']=='V');
		int f=0;
		For(y,1,n)
			if (g[x][y])
				if (y>i+1){
					f|=ss[y-1]=='V';
					ss[y-1]='C';
				}
		For(y,n+1,n*2)
			if (g[x][y])
				if (y-n>i+1){
					f|=ss[y-n-1]=='C';
					ss[y-n-1]='V';
				}
		if (f)
			continue;
		solve(i+1,ss,flag||c>mis[i]);
	}
}
int main(){
	cin>>s;
	b=strlen(s);
	n=read(),m=read();
	For(i,1,m){
		char at[2],bt[2];
		int p1,p2;
		cin>>p1>>at>>p2>>bt;
		int t1=at[0]=='V',t2=bt[0]=='V';
		e[id(p1,t1)].pb(id(p2,t2));
		e[id(p2,t2^1)].pb(id(p1,t1^1));
	}
	For(i,1,n*2)
		dfs(i,i);
//	For(i,1,n*2){outarr(g[i],1,n*2);}
	cin>>mis;
	str="";
	For(i,1,n)
		str+=" ";
	solve(0,str,0);
	puts("-1");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define ppb pop_back()
#define ppf pop_front()
#define all(vec) vec.begin(), vec.end()
#define fol(i,a,b) for(int i=a;i<b;i++)
#define loop(i,a,b) for(int i=a;i>=b;i--)
#define forr(x,arr) for(auto& x:arr)
#define mod 1000000007
#define INF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-7
#define sz(x) ((lli)(x).size())

using   lli     =   long long;
using   lld     =   long double;
using   ulli    =   unsigned long long int;
using   pll     =   pair<lli, lli>;
using   ttt     =   pair<lli, pll>;
using   vttt    =   vector<ttt>;
using   vll     =   vector<pll>;
using   vl      =   vector<lli>;
using   vi      =   vector<int>;
using   vvi     =   vector<vector<int>>;
using   cd      =   complex<lld>;
const double PI = acos(-1);

#ifndef tejasp
#define trace(...) {}
#define cotra(...) {}
#define end_routine() {}
#define endl '\n'
#endif

void dfs (int v, vvi &adj, vi& vect, int vis[], int ind) {
    vis[v] = ind;
    forr(x,adj[v]) if(vis[x]==0) dfs(x,adj,vect,vis,ind);
    vect.pb(v);
}

bool storeSCC(int st, int en, vvi &adj)
{
    vvi radj(sz(adj));
    vi ord;
    int vis[sz(adj)]={};
    fol(i,st,en) {
        if(!vis[i]) dfs(i,adj,ord,vis,1);
        forr(x,adj[i]) radj[x].pb(i);
    }
    reverse(all(ord));
    memset(vis,0,sizeof(vis));
    int ind=1;
    forr(x,ord) if(!vis[x]) {
        vi com;
        dfs(x,radj,com,vis,ind);
        ind++;
    }
    fol(i,st,en) {
        if(vis[i]==vis[i+1]) return 0;
    }
    return 1;
}

int type[30], len, n;
string giv, fin;

int get (char ch) { return (ch=='V') ? 1 : 0; }

bool check(int ind, vvi adj, string &s) {
    fol(i,0,ind)
        adj[2*i+1-type[s[i]-'a']].pb(2*i+type[s[i]-'a']);
    return storeSCC(0, 2*n, adj);
}

bool singcheck() {
    fol(i,1,len) if(type[i]!=type[i-1]) return 0;
    return 1;
}

void doit(int ind, vvi adj) {
    fin = string (n,'a');
    fol(i,0,ind) fin[i]=giv[i];
    fol(i,ind,n) {
        if(check(ind+1,adj,fin)) continue;
        fol(j,1,len){
            if(type[j]==type[j-1]) continue;
            fin[i]=j+'a';
            break;
        }
    }
}

int tejas_919(int kkkk)
{
    lli m, k, q, u, v, temp=0, ans=0;
    {
        string s;
        cin>>s;
        len=sz(s);
        fol(i,0,sz(s)) type[i] = get(s[i]);
    }
    cin>>n>>m;
    vvi adj(2*n+1);
    fol(i,0,m) {
        char ch1, ch2;
        cin>>u>>ch1>>v>>ch2;
        u--; v--;
        adj[2*u+get(ch1)].pb(2*v+get(ch2));
        adj[2*v+1-get(ch2)].pb(2*u+1-get(ch1));
    }
    cin>>giv;
    if(check(n,adj,giv)) return cout<<giv<<endl, 0;
    if(singcheck()) return cout<<-1<<endl, 0;
    loop(i,n-1,0) {
        if(giv[i]-'a'==len-1) continue;
        giv[i]++;
        if(check(i+1,adj,giv)) {
            doit(i+1,adj);
            cout<<fin<<endl;
            return 0;
        }
        fol(cur,giv[i]-'a'+1,len){
            if(type[cur]==type[cur-1]) continue;
            giv[i]='a'+cur;
            trace(i,giv)
            if(check(i+1,adj,giv)) {
                doit(i+1,adj);
                cout<<fin<<endl;
                return 0;
            }
            break;
        }
    }
    cout<<-1<<endl;
    return 0;
}

signed main()
{
    ios_base::sync_with_stdio(false);   cin.tie(NULL);  cout.tie(NULL);
    cout << fixed << setprecision(10);
    int t=1;
    //cin>>t;
    fol(i,0,t) { tejas_919(i+1); }
    end_routine();
}


#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define ppb pop_back()
#define ppf pop_front()
#define all(vec) vec.begin(), vec.end()
#define fol(i,a,b) for(int i=a;i<b;i++)
#define loop(i,a,b) for(int i=a;i>=b;i--)
#define forr(x,arr) for(auto& x:arr)
#define mod 1000000007
#define INF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-7
#define sz(x) ((lli)(x).size())

using   lli     =   long long;
using   lld     =   long double;
using   ulli    =   unsigned long long int;
using   pll     =   pair<lli, lli>;
using   ttt     =   pair<lli, pll>;
using   vttt    =   vector<ttt>;
using   vll     =   vector<pll>;
using   vl      =   vector<lli>;
using   vi      =   vector<int>;
using   vvi     =   vector<vector<int>>;
using   cd      =   complex<lld>;
const double PI = acos(-1);

#ifndef tejasp
#define trace(...) {}
#define cotra(...) {}
#define end_routine() {}
#define endl '\n'
#endif

void dfs (int v, vvi &adj, vi& vect, int vis[], int ind) {
    vis[v] = ind;
    forr(x,adj[v]) if(vis[x]==0) dfs(x,adj,vect,vis,ind);
    vect.pb(v);
}

bool storeSCC(int st, int en, vvi &adj)
{
    vvi radj(sz(adj));
    vi ord;
    int vis[sz(adj)]={};
    fol(i,st,en) {
        if(!vis[i]) dfs(i,adj,ord,vis,1);
        forr(x,adj[i]) radj[x].pb(i);
    }
    reverse(all(ord));
    memset(vis,0,sizeof(vis));
    int ind=1;
    forr(x,ord) if(!vis[x]) {
        vi com;
        dfs(x,radj,com,vis,ind);
        ind++;
    }
    fol(i,st,en) {
        if(vis[i]==vis[i+1]) return 0;
    }
    return 1;
}

int type[30], len, n;
string giv, fin;

int get (char ch) { return (ch=='V') ? 1 : 0; }

bool check(int ind, vvi adj, string &s) {
    fol(i,0,ind)
        adj[2*i+1-type[s[i]-'a']].pb(2*i+type[s[i]-'a']);
    return storeSCC(0, 2*n, adj);
}

bool singcheck() {
    fol(i,1,len) if(type[i]!=type[i-1]) return 0;
    return 1;
}

void doit(int ind, vvi adj) {
    fin = string (n,'a');
    fol(i,0,ind) fin[i]=giv[i];
    fol(i,ind,n) {
        if(check(i+1,adj,fin)) continue;
        fol(j,1,len){
            if(type[j]==type[j-1]) continue;
            fin[i]=j+'a';
            break;
        }
    }
}

int tejas_919(int kkkk)
{
    lli m, k, q, u, v, temp=0, ans=0;
    {
        string s;
        cin>>s;
        len=sz(s);
        fol(i,0,sz(s)) type[i] = get(s[i]);
    }
    cin>>n>>m;
    vvi adj(2*n+1);
    fol(i,0,m) {
        char ch1, ch2;
        cin>>u>>ch1>>v>>ch2;
        u--; v--;
        adj[2*u+get(ch1)].pb(2*v+get(ch2));
        adj[2*v+1-get(ch2)].pb(2*u+1-get(ch1));
    }
    cin>>giv;
    if(check(n,adj,giv)) return cout<<giv<<endl, 0;
    if(singcheck()) return cout<<-1<<endl, 0;
    loop(i,n-1,0) {
        if(giv[i]-'a'==len-1) continue;
        giv[i]++;
        if(check(i+1,adj,giv)) {
            doit(i+1,adj);
            cout<<fin<<endl;
            return 0;
        }
        fol(cur,giv[i]-'a'+1,len){
            if(type[cur]==type[cur-1]) continue;
            giv[i]='a'+cur;
            trace(i,giv)
            if(check(i+1,adj,giv)) {
                doit(i+1,adj);
                cout<<fin<<endl;
                return 0;
            }
            break;
        }
    }
    cout<<-1<<endl;
    return 0;
}

signed main()
{
    ios_base::sync_with_stdio(false);   cin.tie(NULL);  cout.tie(NULL);
    cout << fixed << setprecision(10);
    int t=1;
    //cin>>t;
    fol(i,0,t) { tejas_919(i+1); }
    end_routine();
}


#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define ppb pop_back()
#define ppf pop_front()
#define all(vec) vec.begin(), vec.end()
#define fol(i,a,b) for(int i=a;i<b;i++)
#define loop(i,a,b) for(int i=a;i>=b;i--)
#define forr(x,arr) for(auto& x:arr)
#define mod 1000000007
#define INF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-7
#define sz(x) ((lli)(x).size())

using   lli     =   long long;
using   lld     =   long double;
using   ulli    =   unsigned long long int;
using   pll     =   pair<lli, lli>;
using   ttt     =   pair<lli, pll>;
using   vttt    =   vector<ttt>;
using   vll     =   vector<pll>;
using   vl      =   vector<lli>;
using   vi      =   vector<int>;
using   vvi     =   vector<vector<int>>;
using   cd      =   complex<lld>;
const double PI = acos(-1);

#ifndef tejasp
#define trace(...) {}
#define cotra(...) {}
#define end_routine() {}
#define endl '\n'
#endif

void dfs (int v, vvi &adj, vi& vect, int vis[], int ind) {
    vis[v] = ind;
    forr(x,adj[v]) if(vis[x]==0) dfs(x,adj,vect,vis,ind);
    vect.pb(v);
}

bool storeSCC(int st, int en, vvi &adj)
{
    vvi radj(sz(adj));
    vi ord;
    int vis[sz(adj)]={};
    fol(i,st,en) {
        if(!vis[i]) dfs(i,adj,ord,vis,1);
        forr(x,adj[i]) radj[x].pb(i);
    }
    reverse(all(ord));
    memset(vis,0,sizeof(vis));
    int ind=1;
    forr(x,ord) if(!vis[x]) {
        vi com;
        dfs(x,radj,com,vis,ind);
        ind++;
    }
    fol(i,st,en) {
        if(vis[i]==vis[i+1]) return 0;
        i++;
    }
    return 1;
}

int type[30], len, n;
string giv, fin;

int get (char ch) { return (ch=='V') ? 1 : 0; }

bool check(int ind, vvi adj, string &s) {
    fol(i,0,ind)
        adj[2*i+1-type[s[i]-'a']].pb(2*i+type[s[i]-'a']);
    return storeSCC(0, 2*n, adj);
}

bool singcheck() {
    fol(i,1,len) if(type[i]!=type[i-1]) return 0;
    return 1;
}

void doit(int ind, vvi adj) {
    fin = string (n,'a');
    fol(i,0,ind) fin[i]=giv[i];
    fol(i,ind,n) {
        if(check(i+1,adj,fin)) continue;
        fol(j,1,len){
            if(type[j]==type[j-1]) continue;
            fin[i]=j+'a';
            break;
        }
    }
}

int tejas_919(int kkkk)
{
    lli m, k, q, u, v, temp=0, ans=0;
    {
        string s;
        cin>>s;
        len=sz(s);
        fol(i,0,sz(s)) type[i] = get(s[i]);
    }
    cin>>n>>m;
    vvi adj(2*n+1);
    fol(i,0,m) {
        char ch1, ch2;
        cin>>u>>ch1>>v>>ch2;
        u--; v--;
        adj[2*u+get(ch1)].pb(2*v+get(ch2));
        adj[2*v+1-get(ch2)].pb(2*u+1-get(ch1));
    }
    cin>>giv;
    if(check(n,adj,giv)) return cout<<giv<<endl, 0;
    if(singcheck()) return cout<<-1<<endl, 0;
    loop(i,n-1,0) {
        if(giv[i]-'a'==len-1) continue;
        giv[i]++;
        if(check(i+1,adj,giv)) {
            doit(i+1,adj);
            cout<<fin<<endl;
            return 0;
        }
        fol(cur,giv[i]-'a'+1,len){
            if(type[cur]==type[cur-1]) continue;
            giv[i]='a'+cur;
            trace(i,giv)
            if(check(i+1,adj,giv)) {
                doit(i+1,adj);
                cout<<fin<<endl;
                return 0;
            }
            break;
        }
    }
    cout<<-1<<endl;
    return 0;
}

signed main()
{
    ios_base::sync_with_stdio(false);   cin.tie(NULL);  cout.tie(NULL);
    cout << fixed << setprecision(10);
    int t=1;
    //cin>>t;
    fol(i,0,t) { tejas_919(i+1); }
    end_routine();
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define y second

using namespace std;

typedef long long ll;

int typ[31], mas[205];
char strok[205];
bool used[505];
int color[505];
vector <int> G[505], R[505], tps;
vector <pair <int, int> > spis[3][205], rev[3][205];
pair <pair <int, int>, pair <int, int> > rebr[160005];
queue <int> qu;

void topsort(int v)
{
    used[v] = true;
    for (int i = 0; i < (int) G[v].size(); i++)
    {
        int u = G[v][i];
        if (!used[u]) topsort(u);
    }
    tps.pb(v);
    return;
}

void dfsik(int v, int cnt)
{
    color[v] = cnt;
    for (int i = 0; i < (int) R[v].size(); i++)
    {
        int u = R[v][i];
        if (color[u] == 0) dfsik(u, cnt);
    }
    return;
}

bool ok(int n, int m)
{
    //cout << "AAA\n";
    tps.clear();
    while (!qu.empty()) qu.pop();
    for (int i = 1; i <= 2 * n; i++) used[i] = false, color[i] = 0, G[i].clear(), R[i].clear();

    for (int i = 1; i <= n; i++)
        if (mas[i] != -1) qu.push(i);
    while (!qu.empty())
    {
        int v = qu.front();
        qu.pop();
        for (int i = 0; i < (int) spis[mas[v]][v].size(); i++)
        {
            int u = spis[mas[v]][v][i].y, tp = spis[mas[v]][v][i].x;
            if (mas[u] == -1)
            {
                mas[u] = tp;
                qu.push(u);
            }
            else if (mas[u] != tp) return false;
        }
        for (int i = 0; i < (int) rev[mas[v] ^ 1][v].size(); i++)
        {
            int u = rev[mas[v] ^ 1][v][i].y, tp = rev[mas[v] ^ 1][v][i].x;
            if (mas[u] == -1)
            {
                mas[u] = tp ^ 1;
                qu.push(u);
            }
            else if (mas[u] == tp) return false;
        }
    }
    //cout << "BBB\n";

    for (int i = 1; i <= m; i++)
    {
        int a = rebr[i].x.y, b = rebr[i].y.y;
        int tp1 = rebr[i].x.x, tp2 = rebr[i].y.x;
        if (mas[a] == -1 && mas[b] == -1)
        {
            int num1 = a, num2 = b, num3 = a + n, num4 = b + n;
            if (tp1 == 1) num1 = a + n, num3 = a;
            if (tp2 == 1) num2 = b + n, num4 = b;
            G[num1].pb(num2);
            G[num4].pb(num3);
            R[num2].pb(num1);
            R[num3].pb(num4);
        }
    }

    for (int i = 1; i <= 2 * n; i++)
        if (!used[i]) topsort(i);
    int cnt = 0;
    for (int i = 2 * n - 1; i >= 0; i--)
    {
        int p = tps[i];
        if (color[p] == 0)
        {
            cnt++;
            dfsik(p, cnt);
        }
    }
    //for (int i = 1; i <= 2 * n; i++) cout << color[i] << ' ';
    //cout << '\n';
    for (int i = 1; i <= n; i++)
        if (color[i] == color[i + n]) return false;
    return true;
}

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(0); cin.tie(0);
    string s;
    cin >> s;
    int len = (int) s.length();
    for (int i = 0; i < len; i++)
        if (s[i] == 'V') typ[i + 1] = 0;
        else typ[i + 1] = 1;

    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        int x, y, typ1 = -1, typ2 = -1;
        char c1, c2;
        cin >> x >> c1 >> y >> c2;
        if (c1 == 'V') typ1 = 0;
        else typ1 = 1;
        if (c2 == 'V') typ2 = 0;
        else typ2 = 1;
        spis[typ1][x].pb(mp(typ2, y));
        rev[typ2][y].pb(mp(typ1, x));
        rebr[i] = mp(mp(typ1, x), mp(typ2, y));
    }

    string T;
    cin >> T;
    for (int i = 1; i <= n; i++)
    {
        int c = T[i - 1] - 'a' + 1;
        mas[i] = typ[c];
        strok[i] = c;
    }
    if (ok(n, m)) cout << T, exit(0);
    for (int i = n; i > 0; i--)
    {
        int c = T[i - 1] - 'a' + 1;
        for (int j = i; j <= n; j++) mas[j] = -1;
        mas[i] = 0;
        bool fl1 = ok(n, m);
        for (int j = i; j <= n; j++) mas[j] = -1;
        mas[i] = 1;
        bool fl2 = ok(n, m);
        //cout << fl1 << ' ' << fl2 << '\n';
        mas[i] = -1;

        for (int j = c + 1; j <= len; j++)
        {
            if (typ[j] == 0 && fl1) {mas[i] = 0, strok[i] = j; break;}
            if (typ[j] == 1 && fl2) {mas[i] = 1, strok[i] = j; break;}
        }
        if (mas[i] == -1) continue;

        for (int j = i + 1; j <= n; j++)
        {
            for (int z = j; z <= n; z++) mas[z] = -1;
            mas[j] = 0;
            bool fl1 = ok(n, m);
            for (int z = j; z <= n; z++) mas[z] = -1;
            mas[j] = 1;
            bool fl2 = ok(n, m);
            mas[j] = -1;
            for (int z = 1; z <= len; z++)
            {
                if (typ[z] == 0 && fl1) {mas[j] = 0, strok[j] = z; break;}
                if (typ[z] == 1 && fl2) {mas[j] = 1, strok[j] = z; break;}
            }
        }
        for (int j = 1; j <= n; j++) cout << (char) (strok[j] - 1 + 'a');
        exit(0);
    }
    cout << -1;
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define y second

using namespace std;

typedef long long ll;

int typ[31], mas[205];
char strok[205];
bool used[505];
int color[505];
vector <int> G[505], R[505], tps;
vector <pair <int, int> > spis[3][205], rev[3][205];
pair <pair <int, int>, pair <int, int> > rebr[160005];
queue <int> qu;

void topsort(int v)
{
    used[v] = true;
    for (int i = 0; i < (int) G[v].size(); i++)
    {
        int u = G[v][i];
        if (!used[u]) topsort(u);
    }
    tps.pb(v);
    return;
}

void dfsik(int v, int cnt)
{
    color[v] = cnt;
    for (int i = 0; i < (int) R[v].size(); i++)
    {
        int u = R[v][i];
        if (color[u] == 0) dfsik(u, cnt);
    }
    return;
}

bool ok(int n, int m)
{
    //cout << "AAA\n";
    tps.clear();
    while (!qu.empty()) qu.pop();
    for (int i = 1; i <= 2 * n; i++) used[i] = false, color[i] = 0, G[i].clear(), R[i].clear();

    for (int i = 1; i <= n; i++)
        if (mas[i] != -1) qu.push(i);
    while (!qu.empty())
    {
        int v = qu.front();
        qu.pop();
        for (int i = 0; i < (int) spis[mas[v]][v].size(); i++)
        {
            int u = spis[mas[v]][v][i].y, tp = spis[mas[v]][v][i].x;
            if (mas[u] == -1)
            {
                mas[u] = tp;
                qu.push(u);
            }
            else if (mas[u] != tp) return false;
        }
        for (int i = 0; i < (int) rev[mas[v] ^ 1][v].size(); i++)
        {
            int u = rev[mas[v] ^ 1][v][i].y, tp = rev[mas[v] ^ 1][v][i].x;
            if (mas[u] == -1)
            {
                mas[u] = tp ^ 1;
                qu.push(u);
            }
            else if (mas[u] == tp) return false;
        }
    }
    //cout << "BBB\n";

    for (int i = 1; i <= m; i++)
    {
        int a = rebr[i].x.y, b = rebr[i].y.y;
        int tp1 = rebr[i].x.x, tp2 = rebr[i].y.x;
        if (mas[a] == -1 && mas[b] == -1)
        {
            int num1 = a, num2 = b, num3 = a + n, num4 = b + n;
            if (tp1 == 1) num1 = a + n, num3 = a;
            if (tp2 == 1) num2 = b + n, num4 = b;
            G[num1].pb(num2);
            G[num4].pb(num3);
            R[num2].pb(num1);
            R[num3].pb(num4);
        }
    }

    for (int i = 1; i <= 2 * n; i++)
        if (!used[i]) topsort(i);
    int cnt = 0;
    for (int i = 2 * n - 1; i >= 0; i--)
    {
        int p = tps[i];
        if (color[p] == 0)
        {
            cnt++;
            dfsik(p, cnt);
        }
    }
    //for (int i = 1; i <= 2 * n; i++) cout << color[i] << ' ';
    //cout << '\n';
    for (int i = 1; i <= n; i++)
        if (color[i] == color[i + n]) return false;
    return true;
}

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(0); cin.tie(0);
    string s;
    cin >> s;
    int len = (int) s.length();
    for (int i = 0; i < len; i++)
        if (s[i] == 'V') typ[i + 1] = 0;
        else typ[i + 1] = 1;

    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        int x, y, typ1 = -1, typ2 = -1;
        char c1, c2;
        cin >> x >> c1 >> y >> c2;
        if (c1 == 'V') typ1 = 0;
        else typ1 = 1;
        if (c2 == 'V') typ2 = 0;
        else typ2 = 1;
        spis[typ1][x].pb(mp(typ2, y));
        rev[typ2][y].pb(mp(typ1, x));
        rebr[i] = mp(mp(typ1, x), mp(typ2, y));
    }

    string T;
    cin >> T;
    for (int i = 1; i <= n; i++)
    {
        int c = T[i - 1] - 'a' + 1;
        mas[i] = typ[c];
        strok[i] = c;
    }
    if (ok(n, m)) cout << T, exit(0);
    for (int i = n; i > 0; i--)
    {
        int c = T[i - 1] - 'a' + 1;
        for (int j = i; j <= n; j++) mas[j] = -1;
        mas[i] = 0;
        bool fl1 = ok(n, m);
        for (int j = i; j <= n; j++) mas[j] = -1;
        mas[i] = 1;
        bool fl2 = ok(n, m);
        //cout << fl1 << ' ' << fl2 << '\n';
        mas[i] = -1;

        for (int j = c + 1; j <= len; j++)
        {
            if (typ[j] == 0 && fl1) {mas[i] = 0, strok[i] = j; break;}
            if (typ[j] == 1 && fl2) {mas[i] = 1, strok[i] = j; break;}
        }
        if (mas[i] == -1) continue;

        for (int j = i + 1; j <= n; j++)
        {
            for (int z = j; z <= n; z++) mas[z] = -1;
            mas[j] = 0;
            bool fl1 = ok(n, m);
            for (int z = j; z <= n; z++) mas[z] = -1;
            mas[j] = 1;
            bool fl2 = ok(n, m);
            mas[j] = -1;
            for (int z = 1; z <= n; z++)
            {
                if (typ[z] == 0 && fl1) {mas[j] = 0, strok[j] = z; break;}
                if (typ[z] == 1 && fl2) {mas[j] = 1, strok[j] = z; break;}
            }
        }
        for (int j = 1; j <= n; j++) cout << (char) (strok[j] - 1 + 'a');
        exit(0);
    }
    cout << -1;
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define y second

using namespace std;

typedef long long ll;

int typ[31], mas[205];
char strok[205];
bool used[505];
int color[505];
vector <int> G[505], R[505], tps;
vector <pair <int, int> > spis[3][205], rev[3][205];
pair <pair <int, int>, pair <int, int> > rebr[160005];
queue <int> qu;

void topsort(int v)
{
    used[v] = true;
    for (int i = 0; i < (int) G[v].size(); i++)
    {
        int u = G[v][i];
        if (!used[u]) topsort(u);
    }
    tps.pb(v);
    return;
}

void dfsik(int v, int cnt)
{
    color[v] = cnt;
    for (int i = 0; i < (int) R[v].size(); i++)
    {
        int u = R[v][i];
        if (color[u] == 0) dfsik(u, cnt);
    }
    return;
}

bool ok(int n, int m)
{
    //cout << "AAA\n";
    tps.clear();
    while (!qu.empty()) qu.pop();
    for (int i = 1; i <= 2 * n; i++) used[i] = false, color[i] = 0, G[i].clear(), R[i].clear();

    for (int i = 1; i <= n; i++)
        if (mas[i] != -1) qu.push(i);
    while (!qu.empty())
    {
        int v = qu.front();
        qu.pop();
        for (int i = 0; i < (int) spis[mas[v]][v].size(); i++)
        {
            int u = spis[mas[v]][v][i].y, tp = spis[mas[v]][v][i].x;
            if (mas[u] == -1)
            {
                mas[u] = tp;
                qu.push(u);
            }
            else if (mas[u] != tp) return false;
        }
        for (int i = 0; i < (int) rev[mas[v] ^ 1][v].size(); i++)
        {
            int u = rev[mas[v] ^ 1][v][i].y, tp = rev[mas[v] ^ 1][v][i].x;
            if (mas[u] == -1)
            {
                mas[u] = tp ^ 1;
                qu.push(u);
            }
            else if (mas[u] == tp) return false;
        }
    }
    //cout << "BBB\n";

    for (int i = 1; i <= m; i++)
    {
        int a = rebr[i].x.y, b = rebr[i].y.y;
        int tp1 = rebr[i].x.x, tp2 = rebr[i].y.x;
        if (mas[a] == -1 && mas[b] == -1)
        {
            int num1 = a, num2 = b, num3 = a + n, num4 = b + n;
            if (tp1 == 1) num1 = a + n, num3 = a;
            if (tp2 == 1) num2 = b + n, num4 = b;
            G[num1].pb(num2);
            G[num4].pb(num3);
            R[num2].pb(num1);
            R[num3].pb(num4);
        }
    }

    for (int i = 1; i <= 2 * n; i++)
        if (!used[i]) topsort(i);
    int cnt = 0;
    for (int i = 2 * n - 1; i >= 0; i--)
    {
        int p = tps[i];
        if (color[p] == 0)
        {
            cnt++;
            dfsik(p, cnt);
        }
    }
    //for (int i = 1; i <= 2 * n; i++) cout << color[i] << ' ';
    //cout << '\n';
    for (int i = 1; i <= n; i++)
        if (color[i] == color[i + n]) return false;
    return true;
}

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(0); cin.tie(0);
    string s;
    cin >> s;
    int len = (int) s.length();
    bool F = false, FF = true;
    int T34 = 0;
    for (int i = 0; i < len; i++)
        if (s[i] == 'V') typ[i + 1] = 0;
        else typ[i + 1] = 1;
    for (int i = 1; i < len; i++)
        if (typ[i] != typ[i + 1]) F = true;
    if (!F) T34 = typ[1];

    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        int x, y, typ1 = -1, typ2 = -1;
        char c1, c2;
        cin >> x >> c1 >> y >> c2;
        if (c1 == 'V') typ1 = 0;
        else typ1 = 1;
        if (c2 == 'V') typ2 = 0;
        else typ2 = 1;
        spis[typ1][x].pb(mp(typ2, y));
        rev[typ2][y].pb(mp(typ1, x));
        rebr[i] = mp(mp(typ1, x), mp(typ2, y));

        if (!F && typ1 == T34 && typ2 != T34) cout << -1, exit(0);
    }

    string T;
    cin >> T;
    for (int i = 1; i <= n; i++)
    {
        int c = T[i - 1] - 'a' + 1;
        mas[i] = typ[c];
        strok[i] = c;
    }
    if (ok(n, m)) cout << T, exit(0);
    for (int i = n; i > 0; i--)
    {
        int c = T[i - 1] - 'a' + 1;
        for (int j = i; j <= n; j++) mas[j] = -1;
        mas[i] = 0;
        bool fl1 = ok(n, m);
        for (int j = i; j <= n; j++) mas[j] = -1;
        mas[i] = 1;
        bool fl2 = ok(n, m);
        //cout << fl1 << ' ' << fl2 << '\n';
        mas[i] = -1;

        for (int j = c + 1; j <= len; j++)
        {
            if (typ[j] == 0 && fl1) {mas[i] = 0, strok[i] = j; break;}
            if (typ[j] == 1 && fl2) {mas[i] = 1, strok[i] = j; break;}
        }
        if (mas[i] == -1) continue;

        for (int j = i + 1; j <= n; j++)
        {
            for (int z = j; z <= n; z++) mas[z] = -1;
            mas[j] = 0;
            bool fl1 = ok(n, m);
            for (int z = j; z <= n; z++) mas[z] = -1;
            mas[j] = 1;
            bool fl2 = ok(n, m);
            mas[j] = -1;
            for (int z = 1; z <= len; z++)
            {
                if (typ[z] == 0 && fl1) {mas[j] = 0, strok[j] = z; break;}
                if (typ[z] == 1 && fl2) {mas[j] = 1, strok[j] = z; break;}
            }
        }
        for (int j = 1; j <= n; j++) cout << (char) (strok[j] - 1 + 'a');
        exit(0);
    }
    cout << -1;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define lld long double
using namespace std;
template<typename tn> void read(tn &a){
	tn x=0,f=1; char c=' ';
	for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
	for(;isdigit(c);c=getchar()) x=x*10+c-'0';
	a=x*f;
}
const int N = 510;
int dfn[N],low[N],st[N],in[N],tim,cnt,top,n,m,h[N],len;
vector<int> e[N];
char typ[30],S[N];
void tarjan(int x){
	dfn[x]=low[x]=++tim;in[x]=1;st[++top]=x;
	for(int v:e[x])
		if(!dfn[v]){
			tarjan(v);
			low[x]=min(low[x],low[v]);
		}
		else if(in[v]) low[x]=min(low[x],dfn[v]);
	if(low[x]==dfn[x]){
		h[x]=++cnt;in[x]=0;
		for(;st[top]!=x;top--)
			h[st[top]]=cnt,in[st[top]]=0;
		top--;
	}
}
bool check(){
	memset(dfn,0,sizeof(dfn));
	tim=0;cnt=0;
	for(int i=2;i<=n*2+1;i++)
		if(!dfn[i]) tarjan(i);
	for(int i=1;i<=n;i++)
		if(h[i<<1]==h[i<<1|1]) return 0;
	return 1;
}
void getans(int beg){
	for(int i=beg;i<=n;i++){
		int k=typ[i==beg?S[i]-'a'+1:0]=='C';
		e[i<<1|!k].push_back(i<<1|k);
		if(check()) putchar(i==beg?S[i]+1:'a');
		else{
			e[i<<1|!k].pop_back();
			int p=i==beg?S[i]+1-'a':0;
			while((typ[p]=='C')==k) p++;
			putchar('a'+p);
		}
	}
}
int main(){
	cin>>typ;
	len=strlen(typ);
	int flag=1;
	for(int i=1;i<len;i++)
		flag&=typ[i]==typ[0];
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int x,y;char c1,c2;
		cin>>x>>c1>>y>>c2;
		if(flag&&c1==typ[0]&&c2!=typ[0]){puts("-1");return 0;}
		int k1=c1=='C',k2=c2=='C';
		e[x<<1|k1].push_back(y<<1|k2);
		e[y<<1|!k2].push_back(x<<1|!k1);
	}
	cin>>S+1;
	if(flag){
		cout<<S+1<<'\n';
		return 0;
	}
	int mxC=len-1,mxV=len-1;
	while(typ[mxC]!='C') mxC--;
	while(typ[mxV]!='V') mxV--;
	for(int i=n;~i;i--){
		for(int j=1;j<=i;j++)
			e[j<<1|typ[S[j]-'a']!='C'].push_back(j<<1|typ[S[j]-'a']=='C');
		if(i<n&&S[i+1]-'a'>=mxC)
			e[i+1<<1|1].push_back(i+1<<1);
		if(i<n&&S[i+1]-'a'>=mxV)
			e[i+1<<1].push_back(i+1<<1|1);
		if(check()){
			for(int j=1;j<=i;j++)
				putchar(S[j]);
			getans(i+1);
			return 0;
		}
		if(i<n&&S[i+1]-'a'>=mxC)
			e[i+1<<1|1].pop_back();
		if(i<n&&S[i+1]-'a'>=mxV)
			e[i+1<<1].pop_back();
		for(int j=1;j<=i;j++)
			e[j<<1|typ[S[j]-'a']!='C'].pop_back();
	}
	puts("-1");
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

string A, V;
vector< int > g[400];
int N, M;

void rrec(int idx, bool ue, string value)
{
  if(idx == A.size()) {
    cout << value << endl;
    exit(0);
  }

  char begin = (ue ? 'a' : A[idx]) - 'a';
  bool isbeet = false, ishihu = false;
  for(int i = begin; i < V.size(); i++) {

    if(isbeet && ishihu) break;

    if(V[i] == 'C') {
      if(isbeet) continue;
      if(value[idx] == 'V') continue;
      string next = value;
      if(isalpha(value[idx])) {
        isbeet |= ue | (i != begin);
        next[idx] = (char) (i + 'a');
        rrec(idx + 1, ue | (i != begin), next);
      } else {
        bool isei = true;
        queue< int > q;
        q.push(idx);
        while(!q.empty()) {
          int p = q.front();
          q.pop();
          char cc = p < N ? 'C' : 'V';
          if(next[p % N] != ' ') {
            if(islower(next[p % N]) && V[next[p % N] - 'a'] != cc) {
              isei = false;
              break;
            } else if(isupper(next[p % N]) && next[p % N] != cc) {
              isei = false;
              break;
            }
          }
          if(next[p % N] != ' ') continue;
          next[p % N] = cc;
          for(auto &s : g[p]) if(next[s % N] != (s < N ? 'C' : 'V')) q.push(s);
        }
        if(isei) {
          next[idx] = (char) (i + 'a');
          rrec(idx + 1, ue | (i != begin), next);
        }
        isbeet |= ue | (i != begin);
      }

    } else {
      if(ishihu) continue;
      if(value[idx] == 'C') continue;
      string next = value;

      if(isalpha(value[idx])) {
        ishihu |= ue | (i != begin);
        next[idx] = (char) (i + 'a');
        rrec(idx + 1, ue | (i != begin), next);
      } else {
        bool isei = true;
        queue< int > q;
        q.push(idx + N);
        while(!q.empty()) {
          int p = q.front();
          q.pop();
          char cc = p < N ? 'C' : 'V';
          if(next[p % N] != ' ') {
            if(islower(next[p % N]) && V[next[p % N] - 'a'] != cc) {
              isei = false;
              break;
            } else if(isupper(next[p % N]) && next[p % N] != cc) {
              isei = false;
              break;
            }
          }
          if(next[p % N] != ' ') continue;
          next[p % N] = cc;
          for(auto &s : g[p]) if(next[s % N] != (s < N ? 'C' : 'V')) q.push(s);
        }
        if(isei) {
          next[idx] = (char) (i + 'a');
          rrec(idx + 1, ue | (i != begin), next);
        }
        ishihu |= ue | (i != begin);
      }
    }
  }
}

int main()
{
  cin >> V;
  cin >> N >> M;
  for(int i = 0; i < M; i++) {
    int a, b;
    char c, d;
    cin >> a >> c >> b >> d;
    --a, --b;
    if(c == 'V') a += N;
    if(d == 'V') b += N;
    g[a].push_back(b);
  }
  cin >> A;
  rrec(0, 0, string(A.size(), ' '));
  cout << -1 << endl;
}


#include <bits/stdc++.h>

using namespace std;

struct StronglyConnectedComponents
{
  vector< vector< int > > gg, rg;
  vector< pair< int, int > > edges;
  vector< int > comp, order, used;

  StronglyConnectedComponents(size_t v) : gg(v), rg(v), comp(v, -1), used(v, 0) {}

  void add_edge(int x, int y)
  {
    gg[x].push_back(y);
    rg[y].push_back(x);
    edges.emplace_back(x, y);
  }

  int operator[](int k)
  {
    return (comp[k]);
  }

  void dfs(int idx)
  {
    if(used[idx]) return;
    used[idx] = true;
    for(int to : gg[idx]) dfs(to);
    order.push_back(idx);
  }

  void rdfs(int idx, int cnt)
  {
    if(comp[idx] != -1) return;
    comp[idx] = cnt;
    for(int to : rg[idx]) rdfs(to, cnt);
  }

  void build(vector< vector< int > > &t)
  {
    for(int i = 0; i < gg.size(); i++) dfs(i);
    reverse(begin(order), end(order));
    int ptr = 0;
    for(int i : order) if(comp[i] == -1) rdfs(i, ptr), ptr++;

    t.resize(ptr);
    set< pair< int, int > > connect;
    for(auto &e : edges) {
      int x = comp[e.first], y = comp[e.second];
      if(x == y) continue;
      if(connect.count({x, y})) continue;
      t[x].push_back(y);
      connect.emplace(x, y);
    }
  }
};

string A, V;
vector< int > g[400];
int N, M;

void rrec(int idx, bool ue, string value)
{
  if(idx == A.size()) {
    cout << value << endl;
    exit(0);
  }

  char begin = (ue ? 'a' : A[idx]) - 'a';
  bool isbeet = false, ishihu = false;
  for(int i = begin; i < V.size(); i++) {

    if(isbeet && ishihu) break;

    if(V[i] == 'C') {
      if(isbeet) continue;
      if(value[idx] == 'V') continue;
      string next = value;
      if(isalpha(value[idx])) {
        isbeet |= ue | (i != begin);
        next[idx] = (char) (i + 'a');
        rrec(idx + 1, ue | (i != begin), next);
      } else {
        bool isei = true;
        queue< int > q;
        q.push(idx);
        while(!q.empty()) {
          int p = q.front();
          q.pop();
          char cc = p < N ? 'C' : 'V';
          if(next[p % N] != ' ') {
            if(islower(next[p % N]) && V[next[p % N] - 'a'] != cc) {
              isei = false;
              break;
            } else if(isupper(next[p % N]) && next[p % N] != cc) {
              isei = false;
              break;
            }
          }
          if(next[p % N] != ' ') continue;
          next[p % N] = cc;
          for(auto &s : g[p]) if(next[s % N] != (s < N ? 'C' : 'V')) q.push(s);
        }
        if(isei) {
          next[idx] = (char) (i + 'a');
          rrec(idx + 1, ue | (i != begin), next);
        }
        isbeet |= ue | (i != begin);
      }

    } else {
      if(ishihu) continue;
      if(value[idx] == 'C') continue;
      string next = value;

      if(isalpha(value[idx])) {
        ishihu |= ue | (i != begin);
        next[idx] = (char) (i + 'a');
        rrec(idx + 1, ue | (i != begin), next);
      } else {
        bool isei = true;
        queue< int > q;
        q.push(idx + N);
        while(!q.empty()) {
          int p = q.front();
          q.pop();
          char cc = p < N ? 'C' : 'V';
          if(next[p % N] != ' ') {
            if(islower(next[p % N]) && V[next[p % N] - 'a'] != cc) {
              isei = false;
              break;
            } else if(isupper(next[p % N]) && next[p % N] != cc) {
              isei = false;
              break;
            }
          }
          if(next[p % N] != ' ') continue;
          next[p % N] = cc;
          for(auto &s : g[p]) if(next[s % N] != (s < N ? 'C' : 'V')) q.push(s);
        }
        if(isei) {
          next[idx] = (char) (i + 'a');
          rrec(idx + 1, ue | (i != begin), next);
        }
        ishihu |= ue | (i != begin);
      }
    }
  }
}

int main()
{
  cin >> V;
  cin >> N >> M;
  StronglyConnectedComponents scc(N * 2);
  for(int i = 0; i < M; i++) {
    int a, b;
    char c, d;
    cin >> a >> c >> b >> d;
    --a, --b;
    if(c == 'V') a += N;
    if(d == 'V') b += N;
    g[a].push_back(b);
    scc.add_edge(a, b);
  }
  vector< vector< int > > g;
  scc.build(g);
  for(int i = 0; i < N; i++) {
    if(scc[i] == scc[i + N]) {
      cout << -1 << endl;
      return (0);
    }
  }

  cin >> A;
  rrec(0, 0, string(A.size(), ' '));
  cout << -1 << endl;
}


#include <bits/stdc++.h>

using namespace std;

string A, V;
vector< int > g[400];
int N, M;

void rrec(int idx, bool ue, string value)
{
  if(idx == A.size()) {
    cout << value << endl;
    exit(0);
  }

  char begin = (ue ? 'a' : A[idx]) - 'a';
  bool isbeet = false, ishihu = false;
  for(int i = begin; i < V.size(); i++) {
    if(V[i] == 'C') {
      if(isbeet) continue;
     // if(value[idx] == 'V') continue;
      string next = value;
      if(isalpha(value[idx])) {
        isbeet |= ue | (i != begin);
        next[idx] = (char) (i + 'a');
        rrec(idx + 1, ue | (i != begin), next);
      } else {
        bool isei = true;
        queue< int > q;
        q.push(idx);
        while(!q.empty()) {
          int p = q.front();
          q.pop();
          char cc = p < N ? 'C' : 'V';
          if(next[p % N] != ' ') {
            if(islower(next[p % N]) && V[next[p % N]] != cc) {
              isei = false;
              break;
            } else if(isupper(next[p % N]) && next[p % N] != cc) {
              isei = false;
              break;
            }
          }
          if(cc == next[p % N]) continue;
          if(islower(next[p % N])) continue;

          next[p % N] = cc;
          for(auto &s : g[p]) q.push(s);
        }
        if(isei) {
          next[idx] = (char) (i + 'a');
          rrec(idx + 1, ue | (i != begin), next);
        }
        isbeet |= ue | (i != begin);
      }

    } else {
     // if(ishihu) continue;
      if(value[idx] == 'C') continue;
      string next = value;

      if(isalpha(value[idx])) {
        ishihu |= ue | (i != begin);
        next[idx] = (char) (i + 'a');
        rrec(idx + 1, ue | (i != begin), next);
      } else {
        bool isei = true;
        queue< int > q;
        q.push(idx + N);
        while(!q.empty()) {
          int p = q.front();
          q.pop();
          char cc = p < N ? 'C' : 'V';
          if(next[p % N] != ' ') {
            if(islower(next[p % N]) && V[next[p % N]] != cc) {
              isei = false;
              break;
            } else if(isupper(next[p % N]) && next[p % N] != cc) {
              isei = false;
              break;
            }
          }
          if(cc == next[p % N]) continue;
          if(islower(next[p % N])) continue;
          next[p % N] = cc;
          for(auto &s : g[p]) q.push(s);
        }
        if(isei) {
          next[idx] = (char) (i + 'a');
          rrec(idx + 1, ue | (i != begin), next);
        }
        ishihu |= ue | (i != begin);
      }

    }
  }
}

int main()
{
  cin >> V;
  cin >> N >> M;
  for(int i = 0; i < M; i++) {
    int a, b;
    char c, d;
    cin >> a >> c >> b >> d;
    --a, --b;
    if(c == 'V') a += N;
    if(d == 'V') b += N;
    g[a].push_back(b);
  }
  cin >> A;
  rrec(0, 0, string(A.size(), ' '));
  cout << -1 << endl;
}


#include <bits/stdc++.h>

using namespace std;

string A, V;
vector< int > g[400];
int N, M;

void rrec(int idx, bool ue, string value)
{
  if(idx == A.size()) {
    cout << value << endl;
    exit(0);
  }

  char begin = (ue ? 'a' : A[idx]) - 'a';
  bool isbeet = false, ishihu = false;
  for(int i = begin; i < V.size(); i++) {
    if(V[i] == 'C') {
      if(isbeet) continue;
      if(value[idx] == 'V') continue;
      string next = value;
      if(isalpha(value[idx])) {
        isbeet |= ue | (i != begin);
        next[idx] = (char) (i + 'a');
        rrec(idx + 1, ue | (i != begin), next);
      } else {
        bool isei = true;
        queue< int > q;
        q.push(idx);
        while(!q.empty()) {
          int p = q.front();
          q.pop();
          char cc = p < N ? 'C' : 'V';
          if(next[p % N] != ' ') {

            if(islower(next[p % N]) && V[next[p % N]] != cc) {
              isei = false;
              break;
            } else if(isupper(next[p % N]) && next[p % N] != cc) {
              isei = false;
              break;
            }
          }
          if(cc == next[p % N]) continue;
          next[p % N] = cc;
          for(auto &s : g[p]) q.push(s);
        }
        if(isei) {
          next[idx] = (char) (i + 'a');
          rrec(idx + 1, ue | (i != begin), next);
        }
        isbeet |= ue | (i != begin);
      }

    } else {
      if(ishihu) continue;
      if(value[idx] == 'C') continue;
      string next = value;

      if(isalpha(value[idx])) {
        ishihu |= ue | (i != begin);
        next[idx] = (char) (i + 'a');
        rrec(idx + 1, ue | (i != begin), next);
      } else {
        bool isei = true;
        queue< int > q;
        q.push(idx + N);
        while(!q.empty()) {
          int p = q.front();
          q.pop();
          char cc = p < N ? 'C' : 'V';
          if(next[p % N] != ' ') {
            if(islower(next[p % N]) && V[next[p % N]] != cc) {
              isei = false;
              break;
            } else if(isupper(next[p % N]) && next[p % N] != cc) {
              isei = false;
              break;
            }
          }
          if(cc == next[p % N]) continue;
          next[p % N] = cc;
          for(auto &s : g[p]) q.push(s);
        }
        if(isei) {
          next[idx] = (char) (i + 'a');

          rrec(idx + 1, ue | (i != begin), next);
        }
        ishihu |= ue | (i != begin);
      }

    }
  }
}

int main()
{
  cin >> V;
  cin >> N >> M;
  for(int i = 0; i < M; i++) {
    int a, b;
    char c, d;
    cin >> a >> c >> b >> d;
    --a, --b;
    if(c == 'V') a += N;
    if(d == 'V') b += N;
    g[a].push_back(b);
  }
  cin >> A;
  rrec(0, 0, string(A.size(), ' '));
  cout << -1 << endl;
}


#include <bits/stdc++.h>

using namespace std;

struct StronglyConnectedComponents
{
  vector< vector< int > > gg, rg;
  vector< pair< int, int > > edges;
  vector< int > comp, order, used;

  StronglyConnectedComponents(size_t v) : gg(v), rg(v), comp(v, -1), used(v, 0) {}

  void add_edge(int x, int y)
  {
    gg[x].push_back(y);
    rg[y].push_back(x);
    edges.emplace_back(x, y);
  }

  int operator[](int k)
  {
    return (comp[k]);
  }

  void dfs(int idx)
  {
    if(used[idx]) return;
    used[idx] = true;
    for(int to : gg[idx]) dfs(to);
    order.push_back(idx);
  }

  void rdfs(int idx, int cnt)
  {
    if(comp[idx] != -1) return;
    comp[idx] = cnt;
    for(int to : rg[idx]) rdfs(to, cnt);
  }

  void build(vector< vector< int > > &t)
  {
    for(int i = 0; i < gg.size(); i++) dfs(i);
    reverse(begin(order), end(order));
    int ptr = 0;
    for(int i : order) if(comp[i] == -1) rdfs(i, ptr), ptr++;

    t.resize(ptr);
    set< pair< int, int > > connect;
    for(auto &e : edges) {
      int x = comp[e.first], y = comp[e.second];
      if(x == y) continue;
      if(connect.count({x, y})) continue;
      t[x].push_back(y);
      connect.emplace(x, y);
    }
  }
};

string A, V;
StronglyConnectedComponents scc(400);
vector< int > g[400];
int N, M;

void rrec(int idx, bool ue, string value)
{
  if(idx == A.size()) {
    cout << value << endl;
    exit(0);
  }
  char begin = (ue ? 'a' : A[idx]) - 'a';
  bool isbeet = false, ishihu = false;
  for(int i = begin; i < V.size(); i++) {
    if(V[i] == 'C') {
      if(isbeet) continue;
      if(value[idx] == 'V') continue;

      string next = value;

      if(isalpha(value[idx])) {

        isbeet |= ue | (i != begin);
        next[idx] = (char) (i + 'a');
        rrec(idx + 1, ue | (i != begin), next);
      } else {
        bool isei = true;
        queue< int > q;
        q.push(idx);
        while(!q.empty()) {
          int p = q.front();
          q.pop();
          char cc = p < N ? 'C' : 'V';
          if(next[p % N] != ' ' && (cc != next[p % N] || cc != V[next[p % N]])) {
            isei = false;
            break;
          }
          if(cc == next[p % N]) continue;
          next[p % N] = cc;
          for(auto &s : g[p]) q.push(s);
        }

        if(isei) {
          isbeet |= ue | (i != begin);
          next[idx] = (char) (i + 'a');
          rrec(idx + 1, ue | (i != begin), next);
        }
      }

    } else {
      if(ishihu) continue;
      if(value[idx] == 'C') continue;
      string next = value;

      if(isalpha(value[idx])) {
        ishihu |= ue | (i != begin);
        next[idx] = (char) (i + 'a');
        rrec(idx + 1, ue | (i != begin), next);
      } else {
        bool isei = true;

        queue< int > q;
        q.push(idx + N);
        while(!q.empty()) {
          int p = q.front();
          q.pop();
          char cc = p < N ? 'C' : 'V';
          if(next[p % N] != ' ' && (cc != next[p % N] || cc != V[next[p % N]])) {
            isei = false;
            break;
          }
          if(cc == next[p % N])         continue;
          next[p % N] = cc;
          for(auto &s : g[p]) {
            q.push(s);
          }
        }
        if(isei) {
          ishihu |= ue | (i != begin);
          next[idx] = (char) (i + 'a');
          rrec(idx + 1, ue | (i != begin), next);
        }
      }

    }
  }
}

int main()
{
  cin >> V;
  cin >> N >> M;
  for(int i = 0; i < M; i++) {
    int a, b;
    char c, d;
    cin >> a >> c >> b >> d;
    --a, --b;
    if(c == 'V') a += N;
    if(d == 'V') b += N;
    scc.add_edge(a, b);
    g[a].push_back(b);
  }
  vector< vector< int > > g;
  scc.build(g);
  for(int i = 0; i < N; i++) {
    if(scc[i] == scc[i + N]) {
      cout << -1 << endl;
      return (0);
    }
  }
  cin >> A;
  rrec(0, 0, string(A.size(), ' '));
  cout << -1 << endl;
}


#include <bits/stdc++.h>

using namespace std;

string A, V;
vector< int > g[400];
int N, M;

void rrec(int idx, bool ue, string value)
{
  if(idx == A.size()) {
    cout << value << endl;
    exit(0);
  }

  char begin = (ue ? 'a' : A[idx]) - 'a';
  bool isbeet = false, ishihu = false;
  for(int i = begin; i < V.size(); i++) {
    if(V[i] == 'C') {
      //if(isbeet) continue;
      if(value[idx] == 'V') continue;
      string next = value;
      if(isalpha(value[idx])) {
        isbeet |= ue | (i != begin);
        next[idx] = (char) (i + 'a');
        rrec(idx + 1, ue | (i != begin), next);
      } else {
        bool isei = true;
        queue< int > q;
        q.push(idx);
        while(!q.empty()) {
          int p = q.front();
          q.pop();
          char cc = p < N ? 'C' : 'V';
          if(next[p % N] != ' ') {
            if(islower(next[p % N]) && V[next[p % N]] != cc) {
              isei = false;
              break;
            } else if(isupper(next[p % N]) && next[p % N] != cc) {
              isei = false;
              break;
            }
          }
          if(cc == next[p % N]) continue;
          if(islower(next[p % N])) continue;

          next[p % N] = cc;
          for(auto &s : g[p]) q.push(s);
        }
        if(isei) {
          next[idx] = (char) (i + 'a');
          rrec(idx + 1, ue | (i != begin), next);
        }
        isbeet |= ue | (i != begin);
      }

    } else {
     // if(ishihu) continue;
      if(value[idx] == 'C') continue;
      string next = value;

      if(isalpha(value[idx])) {
        ishihu |= ue | (i != begin);
        next[idx] = (char) (i + 'a');
        rrec(idx + 1, ue | (i != begin), next);
      } else {
        bool isei = true;
        queue< int > q;
        q.push(idx + N);
        while(!q.empty()) {
          int p = q.front();
          q.pop();
          char cc = p < N ? 'C' : 'V';
          if(next[p % N] != ' ') {
            if(islower(next[p % N]) && V[next[p % N]] != cc) {
              isei = false;
              break;
            } else if(isupper(next[p % N]) && next[p % N] != cc) {
              isei = false;
              break;
            }
          }
          if(cc == next[p % N]) continue;
          if(islower(next[p % N])) continue;
          next[p % N] = cc;
          for(auto &s : g[p]) q.push(s);
        }
        if(isei) {
          next[idx] = (char) (i + 'a');
          rrec(idx + 1, ue | (i != begin), next);
        }
        ishihu |= ue | (i != begin);
      }

    }
  }
}

int main()
{
  cin >> V;
  cin >> N >> M;
  for(int i = 0; i < M; i++) {
    int a, b;
    char c, d;
    cin >> a >> c >> b >> d;
    --a, --b;
    if(c == 'V') a += N;
    if(d == 'V') b += N;
    g[a].push_back(b);
  }
  cin >> A;
  rrec(0, 0, string(A.size(), ' '));
  cout << -1 << endl;
}


#include <bits/stdc++.h>

using namespace std;

string A, V;
vector< int > g[400];
int N, M;

void rrec(int idx, bool ue, string value)
{
  if(idx == A.size()) {
    cout << value << endl;
    exit(0);
  }

  char begin = (ue ? 'a' : A[idx]) - 'a';
  bool isbeet = false, ishihu = false;
  for(int i = begin; i < V.size(); i++) {
    if(V[i] == 'C') {
      //if(isbeet) continue;
      if(value[idx] == 'V') continue;
      string next = value;
      if(isalpha(value[idx])) {
        isbeet |= ue | (i != begin);
        next[idx] = (char) (i + 'a');
        rrec(idx + 1, ue | (i != begin), next);
      } else {
        bool isei = true;
        queue< int > q;
        q.push(idx);
        while(!q.empty()) {
          int p = q.front();
          q.pop();
          char cc = p < N ? 'C' : 'V';
          if(next[p % N] != ' ') {
            if(islower(next[p % N]) && V[next[p % N]-'a'] != cc) {
              isei = false;
              break;
            } else if(isupper(next[p % N]) && next[p % N] != cc) {
              isei = false;
              break;
            }
          }
          if(next[p % N] != ' ') continue;
          next[p % N] = cc;
          for(auto &s : g[p]) q.push(s);
        }
        if(isei) {
          next[idx] = (char) (i + 'a');
          rrec(idx + 1, ue | (i != begin), next);
        }
        isbeet |= ue | (i != begin);
      }

    } else {
     // if(ishihu) continue;
      if(value[idx] == 'C') continue;
      string next = value;

      if(isalpha(value[idx])) {
        ishihu |= ue | (i != begin);
        next[idx] = (char) (i + 'a');
        rrec(idx + 1, ue | (i != begin), next);
      } else {
        bool isei = true;
        queue< int > q;
        q.push(idx + N);
        while(!q.empty()) {
          int p = q.front();
          q.pop();
          char cc = p < N ? 'C' : 'V';
          if(next[p % N] != ' ') {
            if(islower(next[p % N]) && V[next[p % N]-'a'] != cc) {
              isei = false;
              break;
            } else if(isupper(next[p % N]) && next[p % N] != cc) {
              isei = false;
              break;
            }
          }
          if(next[p % N] != ' ') continue;
          next[p % N] = cc;
          for(auto &s : g[p]) q.push(s);
        }
        if(isei) {
          next[idx] = (char) (i + 'a');
          rrec(idx + 1, ue | (i != begin), next);
        }
        ishihu |= ue | (i != begin);
      }

    }
  }
}

int main()
{
  cin >> V;
  cin >> N >> M;
  for(int i = 0; i < M; i++) {
    int a, b;
    char c, d;
    cin >> a >> c >> b >> d;
    --a, --b;
    if(c == 'V') a += N;
    if(d == 'V') b += N;
    g[a].push_back(b);
  }
  cin >> A;
  rrec(0, 0, string(A.size(), ' '));
  cout << -1 << endl;
}


#include <bits/stdc++.h>

using namespace std;

string A, V;
vector< int > g[400];
int N, M;

void rrec(int idx, bool ue, string value)
{
  if(idx == A.size()) {
    cout << value << endl;
    exit(0);
  }

  char begin = (ue ? 'a' : A[idx]) - 'a';
  bool isbeet = false, ishihu = false;
  for(int i = begin; i < V.size(); i++) {
    if(V[i] == 'C') {
      if(isbeet) continue;
      if(value[idx] == 'V') continue;
      string next = value;
      if(isalpha(value[idx])) {
        isbeet |= ue | (i != begin);
        next[idx] = (char) (i + 'a');
        rrec(idx + 1, ue | (i != begin), next);
      } else {
        bool isei = true;
        queue< int > q;
        q.push(idx);
        while(!q.empty()) {
          int p = q.front();
          q.pop();
          char cc = p < N ? 'C' : 'V';
          if(next[p % N] != ' ') {
            if(islower(next[p % N]) && V[next[p % N]-'a'] != cc) {
              isei = false;
              break;
            } else if(isupper(next[p % N]) && next[p % N] != cc) {
              isei = false;
              break;
            }
          }
          if(next[p % N] != ' ') continue;
          next[p % N] = cc;
          for(auto &s : g[p]) if(next[s%N]!=(s < N ? 'C' : 'V')) q.push(s);
        }
        if(isei) {
          next[idx] = (char) (i + 'a');
          rrec(idx + 1, ue | (i != begin), next);
        }
        isbeet |= ue | (i != begin);
      }

    } else {
      if(ishihu) continue;
      if(value[idx] == 'C') continue;
      string next = value;

      if(isalpha(value[idx])) {
        ishihu |= ue | (i != begin);
        next[idx] = (char) (i + 'a');
        rrec(idx + 1, ue | (i != begin), next);
      } else {
        bool isei = true;
        queue< int > q;
        q.push(idx + N);
        while(!q.empty()) {
          int p = q.front();
          q.pop();
          char cc = p < N ? 'C' : 'V';
          if(next[p % N] != ' ') {
            if(islower(next[p % N]) && V[next[p % N]-'a'] != cc) {
              isei = false;
              break;
            } else if(isupper(next[p % N]) && next[p % N] != cc) {
              isei = false;
              break;
            }
          }
          if(next[p % N] != ' ') continue;
          next[p % N] = cc;
          for(auto &s : g[p]) if(next[s%N]!=(s < N ? 'C' : 'V')) q.push(s);
        }
        if(isei) {
          next[idx] = (char) (i + 'a');
          rrec(idx + 1, ue | (i != begin), next);
        }
        ishihu |= ue | (i != begin);
      }

    }
  }
}

int main()
{
  cin >> V;
  cin >> N >> M;
  for(int i = 0; i < M; i++) {
    int a, b;
    char c, d;
    cin >> a >> c >> b >> d;
    --a, --b;
    if(c == 'V') a += N;
    if(d == 'V') b += N;
    g[a].push_back(b);
  }
  cin >> A;
  rrec(0, 0, string(A.size(), ' '));
  cout << -1 << endl;
}


#include <bits/stdc++.h>

using namespace std;

string A, V;
vector< int > g[400];
int N, M;

void rrec(int idx, bool ue, string value)
{
  if(idx == A.size()) {
    cout << value << endl;
    exit(0);
  }

  char begin = (ue ? 'a' : A[idx]) - 'a';
  bool isbeet = false, ishihu = false;
  for(int i = begin; i < V.size(); i++) {
    if(V[i] == 'C') {
      if(isbeet) continue;
      if(value[idx] == 'V') continue;
      string next = value;
      if(isalpha(value[idx])) {
        isbeet |= ue | (i != begin);
        next[idx] = (char) (i + 'a');
        rrec(idx + 1, ue | (i != begin), next);
      } else {
        bool isei = true;
        queue< int > q;
        q.push(idx);
        while(!q.empty()) {
          int p = q.front();
          q.pop();
          char cc = p < N ? 'C' : 'V';
          if(next[p % N] != ' ') {
            if(islower(next[p % N]) && V[next[p % N]] != cc) {
              isei = false;
              break;
            } else if(isupper(next[p % N]) && next[p % N] != cc) {
              isei = false;
              break;
            }
          }
          if(cc == next[p % N]) continue;
          if(islower(next[p % N])) continue;

          next[p % N] = cc;
          for(auto &s : g[p]) q.push(s);
        }
        if(isei) {
          next[idx] = (char) (i + 'a');
          rrec(idx + 1, ue | (i != begin), next);
        }
        isbeet |= ue | (i != begin);
      }

    } else {
      if(ishihu) continue;
      if(value[idx] == 'C') continue;
      string next = value;

      if(isalpha(value[idx])) {
        ishihu |= ue | (i != begin);
        next[idx] = (char) (i + 'a');
        rrec(idx + 1, ue | (i != begin), next);
      } else {
        bool isei = true;
        queue< int > q;
        q.push(idx + N);
        while(!q.empty()) {
          int p = q.front();
          q.pop();
          char cc = p < N ? 'C' : 'V';
          if(next[p % N] != ' ') {
            if(islower(next[p % N]) && V[next[p % N]] != cc) {
              isei = false;
              break;
            } else if(isupper(next[p % N]) && next[p % N] != cc) {
              isei = false;
              break;
            }
          }
          if(cc == next[p % N]) continue;
          if(islower(next[p % N])) continue;
          next[p % N] = cc;
          for(auto &s : g[p]) q.push(s);
        }
        if(isei) {
          next[idx] = (char) (i + 'a');
          rrec(idx + 1, ue | (i != begin), next);
        }
        ishihu |= ue | (i != begin);
      }

    }
  }
}

int main()
{
  cin >> V;
  cin >> N >> M;
  for(int i = 0; i < M; i++) {
    int a, b;
    char c, d;
    cin >> a >> c >> b >> d;
    --a, --b;
    if(c == 'V') a += N;
    if(d == 'V') b += N;
    g[a].push_back(b);
  }
  cin >> A;
  rrec(0, 0, string(A.size(), ' '));
  cout << -1 << endl;
}


#include <bits/stdc++.h>

using namespace std;

string A, V;
vector< int > g[400];
int N, M;

void rrec(int idx, bool ue, string value)
{
  if(idx == A.size()) {
    cout << value << endl;
    exit(0);
  }

  char begin = (ue ? 'a' : A[idx]) - 'a';
  bool isbeet = false, ishihu = false;
  for(int i = begin; i < V.size(); i++) {
    if(V[i] == 'C') {
      if(isbeet) continue;
      if(value[idx] == 'V') continue;
      string next = value;
      if(isalpha(value[idx])) {
        isbeet |= ue | (i != begin);
        next[idx] = (char) (i + 'a');
        rrec(idx + 1, ue | (i != begin), next);
      } else {
        bool isei = true;
        queue< int > q;
        q.push(idx);
        while(!q.empty()) {
          int p = q.front();
          q.pop();
          char cc = p < N ? 'C' : 'V';
          if(next[p % N] != ' ') {
            if(islower(next[p % N]) && V[next[p % N]-'a'] != cc) {
              isei = false;
              break;
            } else if(isupper(next[p % N]) && next[p % N] != cc) {
              isei = false;
              break;
            }
          }
          if(next[p % N] != ' ') continue;
          next[p % N] = cc;
          for(auto &s : g[p]) q.push(s);
        }
        if(isei) {
          next[idx] = (char) (i + 'a');
          rrec(idx + 1, ue | (i != begin), next);
        }
        isbeet |= ue | (i != begin);
      }

    } else {
      if(ishihu) continue;
      if(value[idx] == 'C') continue;
      string next = value;

      if(isalpha(value[idx])) {
        ishihu |= ue | (i != begin);
        next[idx] = (char) (i + 'a');
        rrec(idx + 1, ue | (i != begin), next);
      } else {
        bool isei = true;
        queue< int > q;
        q.push(idx + N);
        while(!q.empty()) {
          int p = q.front();
          q.pop();
          char cc = p < N ? 'C' : 'V';
          if(next[p % N] != ' ') {
            if(islower(next[p % N]) && V[next[p % N]-'a'] != cc) {
              isei = false;
              break;
            } else if(isupper(next[p % N]) && next[p % N] != cc) {
              isei = false;
              break;
            }
          }
          if(next[p % N] != ' ') continue;
          next[p % N] = cc;
          for(auto &s : g[p]) q.push(s);
        }
        if(isei) {
          next[idx] = (char) (i + 'a');
          rrec(idx + 1, ue | (i != begin), next);
        }
        ishihu |= ue | (i != begin);
      }

    }
  }
}

int main()
{
  cin >> V;
  cin >> N >> M;
  for(int i = 0; i < M; i++) {
    int a, b;
    char c, d;
    cin >> a >> c >> b >> d;
    --a, --b;
    if(c == 'V') a += N;
    if(d == 'V') b += N;
    g[a].push_back(b);
  }
  cin >> A;
  rrec(0, 0, string(A.size(), ' '));
  cout << -1 << endl;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<bitset>
using namespace std;
const int N=405;
int n,m,l;
char t[30],p[2],q[2],str[N],ans[N];
bitset<N>g[N];
int chk(char x){
	return t[x-'a']=='V';
}
void dfs(int x,int y){
	if(x==n){
		printf("%s",ans);
		exit(0);
	}
	int vow=1,con=1;
	for(int i=0;i<x;++i){
		vow&=!g[i<<1|chk(ans[i])][x<<1];
		con&=!g[i<<1|chk(ans[i])][x<<1|1];
	}
	for(char i=y?'a':str[x];i<'a'+l;++i)
		if((chk(i)&&vow)||(!chk(i)&&con))
			ans[x]=i,dfs(x+1,y|(ans[x]>str[x]));
}
int main(){
	scanf("%s%d%d",t,&n,&m);l=strlen(t);
	for(int i=1,x,y;i<=m;++i){
		scanf("%d%s%d%s",&x,p,&y,q);
		x=x-1<<1|(p[0]=='V');y=y-1<<1|(q[0]=='V');
		g[x][y]=g[y^1][x^1]=1;
	}
	for(int i=0;i<n+n;++i)g[i][i]=1;
	for(int k=0;k<n+n;++k)
		for(int i=0;i<n+n;++i)
			if(g[i][k])
				g[i]|=g[k];
	for(int i=0;i<n+n;++i)
		if(g[i][i^1]&&g[i^1][i])
			return puts("-1"),0;
	scanf("%s",str);dfs(0,0);
	puts("-1");return 0;
}


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
	private static final int MAX = 101;
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		boolean[] primes = new boolean[MAX];
		primes[0] = true;
		primes[1] = true;
		for (int i = 2; i < MAX; i++) {
			if (!primes[i]) {
				for (int j = i * i; j < MAX; j += i) {
					primes[j] = true;
				}
			}
		}
		String res = "";
		int yes = 0;
		int n = MAX;
		for (int i = 0; i < n; i++) {
			if (!primes[i]) {
				System.out.println(i);
				System.out.flush();
				res = sc.nextLine();
				if (res.equals("yes")) {
					n = MAX / i;
					yes++;
				}
			}
			if (yes > 1) {
				break;
			}
			if (i == 11 && yes < 2) {
				break;
			}
		}
		System.out.println(yes > 1 ? "composite" : "prime");
		System.out.flush();
	}
}


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
	private static final int MAX = 101;
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		boolean[] primes = new boolean[MAX];
		primes[0] = true;
		primes[1] = true;
		for (int i = 2; i < MAX; i++) {
			if (!primes[i]) {
				for (int j = i * i; j < MAX; j += i) {
					primes[j] = true;
				}
			}
		}
		String res = "";
		int yes = 0;
		int n = 50;
		for (int i = 2; i <= n; i++) {
			if (!primes[i]) {
				System.out.println(i);
				System.out.flush();
				res = sc.nextLine();
				if (res.equals("yes") || res.equals("YES")) {
					yes++;
					if (i * i < MAX) {
						System.out.println(i * i);
						System.out.flush();
						res = sc.nextLine();
						if (res.equals("yes")) {
							yes++;
						}
					}
				}
			}
			if (yes > 1) {
				break;
			}
		}
		System.out.println(yes > 1 ? "composite" : "prime");
		System.out.flush();
	}
}


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
	private static final int MAX = 101;
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		boolean[] primes = new boolean[MAX];
		primes[0] = true;
		primes[1] = true;
		for (int i = 2; i < MAX; i++) {
			if (!primes[i]) {
				for (int j = i * i; j < MAX; j += i) {
					primes[j] = true;
				}
			}
		}
		String res = "";
		int yes = 0;
		int n = 50;
		for (int i = 0; i < n; i++) {
			if (!primes[i]) {
				System.out.println(i);
				System.out.flush();
				res = sc.nextLine();
				if (res.equals("yes")) {
					yes++;
				}
			}
			if (yes > 1) {
				break;
			}
		}
		System.out.println(yes > 1 ? "composite" : "prime");
		System.out.flush();
	}
}


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
	private static final int MAX = 101;
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		boolean[] primes = new boolean[MAX];
		primes[0] = true;
		primes[1] = true;
		for (int i = 2; i < MAX; i++) {
			if (!primes[i]) {
				for (int j = i * i; j < MAX; j += i) {
					primes[j] = true;
				}
			}
		}
		String res = "";
		int yes = 0;
		int n = 50;
		for (int i = 0; i <= n; i++) {
			if (!primes[i]) {
				System.out.println(i);
				System.out.flush();
				res = sc.nextLine();
				if (res.equals("yes")) {
					yes++;
					if (i * i <= MAX) {
						System.out.println(i);
						System.out.flush();
						res = sc.nextLine();
						if (res.equals("yes")) {
							yes++;
						}
					}
				}
			}
			if (yes > 1) {
				break;
			}
		}
		System.out.println(yes > 1 ? "composite" : "prime");
		System.out.flush();
	}
}