/* 
TASK:gen
LANG:C++
*/

#include <cstdio>
#include <cstring>

using namespace std;

#define MAXN 128
#define MAXQUEUE 1000000

bool ans[32];
char n[MAXN];
char c[MAXN][2];
char m[MAXN][8];
char mp[MAXN];
int C, M;

char q[MAXQUEUE][MAXN];
int top, f;

void bfs(void) {
    top = f = 0;
    strcpy(q[top++], n);
    int len;
    int i, j;
    bool flag;
    
    memset(ans, false, sizeof(ans));
    
    while(f < top) {
        len = strlen(q[f]);
        flag = false;
        for (i = 0; i < len; i++){
            for (j = 0; j < C; j++) {
                if (c[j][1] == q[f][i]) {
                    strcpy(q[top], q[f]);
                    q[top][i] = c[j][0];
                    q[top][i+1] = '\0';
                    strcat(q[top], q[f]+i+1);
                    top++;
                    flag = true;
                }
            }
        }
        for (i = 0; i < len-1; i++) {
            for (j = 0; j < M; j++) {
                if (m[j][0] == q[f][i] && m[j][1] == q[f][i+1]) {
                    strcpy(q[top], q[f]);
                    q[top][i] = mp[j];
                    q[top][i+1] = '\0';
                    strcat(q[top], q[f]+i+2);
                    top++;
                    flag = true;
                }
            }
        }

        if (flag == false) {
            if(q[f][1] == '\0') {
                ans[q[f][0] - 'A'] = true;
            }
        }
        f++;
    }
}


int main() {
    
    scanf("%s", n);
    
    scanf("%d", &C);
    for (int i = 0; i < C; i++) {
        scanf("\n%c%c", &c[i][0], &c[i][1]);
    }
    
    scanf("%d\n", &M);
    for (int i = 0; i < M; i++) {
        scanf("%c%s\n", &mp[i], m[i]);
    }

    bfs();
    
    bool haveans = false;
    
    for (int i = 0; i < 26; i++) {
        if (ans[i]) {
            haveans = true;
            printf("%c", i+'A');
        }
    }
    if (!haveans) {
        printf("0");
    }
    printf("\n");
    return 0;
}

