#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define debug(x) cout<<x<<"DE\n";
#define debug2(x,y) cout<<x<<" "<<y<<"DE\n";
int main()
{
ll t=1,ca=1,t2;
cin>>t;t2=t;
while(t--){
ll n;
string s,s2;
cin>>s;
s2=s;
n=s.size();
ll m;
cin>>m;
vector<char>ch[26];
for(int i=0;i<26;i++)ch[i].clear();
for(int i=1;i<=m;i++){
char u,v;
cin>>u>>v;
ch[u-'a'].push_back(v);
}
for(int i=0;i<26;i++){
sort(ch[i].begin(),ch[i].end());
}
ll zzz=1,ok=1;
for(int i=0;i<n;i++){
if(i==0){
while(1){
ll a=s[i]-'a';
if(ch[a].size()==0)break;
for(int j=0;j<ch[a].size();j++){
if(ch[a][j]<s[i]){
s[i]=ch[a][j];break;
}
}
ll z4=s[i]-'a';
if(z4==a)break;
}
}
else{
ll b=s[i-1]-'a';
ll a=s[i]-'a';
if(b>a){zzz=0;
while(1){
a=s[i]-'a';
if(ch[a].size()==0)break;
else if(b<=a){
while(1){
a=s[i]-'a';
if(ch[a].size()==0)break;
for(int j=0;j<ch[a].size();j++){
if(ch[a][j]<s[i]&&ch[a][j]>=s[i-1]){
s[i]=ch[a][j];break;
}
}
ll z4=s[i]-'a';
if(z4==a)break;
}
}
for(int j=0;j<ch[a].size();j++){
if(ch[a][j]>=s[i-1]){
zzz=1,s[i]=ch[a][j];break;
}
}
ll z4=s[i]-'a';
if(z4==a)break;
}
}
else if(a>b){
while(1){
a=s[i]-'a';
if(ch[a].size()==0)break;
for(int j=0;j<ch[a].size();j++){
if(ch[a][j]<s[i]&&ch[a][j]>=s[i-1]){
s[i]=ch[a][j];break;
}
}
ll z4=s[i]-'a';
if(z4==a)break;
}
}
}
}
ok=1;
for(int i=1;i<n;i++){
if(s[i]<s[i-1]){ok=0;break;}
}
if(ok)cout<<"YES\n";
else cout<<"NO\n";
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgZGVidWcoeCkgY291dDw8eDw8IkRFXG4iOwojZGVmaW5lIGRlYnVnMih4LHkpIGNvdXQ8PHg8PCIgIjw8eTw8IkRFXG4iOwoKCgppbnQgbWFpbigpCnsKICAgIGxsIHQ9MSxjYT0xLHQyOwogICAgY2luPj50O3QyPXQ7CiAgICB3aGlsZSh0LS0pewogICAgICAgIGxsIG47CiAgICAgICAgc3RyaW5nIHMsczI7CiAgICAgICAgY2luPj5zOwogICAgICAgIHMyPXM7CiAgICAgICAgbj1zLnNpemUoKTsKICAgICAgICBsbCBtOwogICAgICAgIGNpbj4+bTsKICAgICAgICB2ZWN0b3I8Y2hhcj5jaFsyNl07CiAgICAgICAgZm9yKGludCBpPTA7aTwyNjtpKyspY2hbaV0uY2xlYXIoKTsKICAgICAgICBmb3IoaW50IGk9MTtpPD1tO2krKyl7CiAgICAgICAgICAgIGNoYXIgdSx2OwogICAgICAgICAgICBjaW4+PnU+PnY7CiAgICAgICAgICAgIGNoW3UtJ2EnXS5wdXNoX2JhY2sodik7CiAgICAgICAgfQogICAgICAgIGZvcihpbnQgaT0wO2k8MjY7aSsrKXsKICAgICAgICAgICAgc29ydChjaFtpXS5iZWdpbigpLGNoW2ldLmVuZCgpKTsKICAgICAgICB9CgogICAgICAgIGxsIHp6ej0xLG9rPTE7CiAgICAgICAgZm9yKGludCBpPTA7aTxuO2krKyl7CiAgICAgICAgICAgIGlmKGk9PTApewogICAgICAgICAgICAgICAgd2hpbGUoMSl7CiAgICAgICAgICAgICAgICAgICAgbGwgYT1zW2ldLSdhJzsKICAgICAgICAgICAgICAgICAgICBpZihjaFthXS5zaXplKCk9PTApYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgZm9yKGludCBqPTA7ajxjaFthXS5zaXplKCk7aisrKXsKICAgICAgICAgICAgICAgICAgICAgICAgaWYoY2hbYV1bal08c1tpXSl7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzW2ldPWNoW2FdW2pdO2JyZWFrOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGxsIHo0PXNbaV0tJ2EnOwogICAgICAgICAgICAgICAgICAgIGlmKHo0PT1hKWJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2V7CiAgICAgICAgICAgICAgICBsbCBiPXNbaS0xXS0nYSc7CiAgICAgICAgICAgICAgICBsbCBhPXNbaV0tJ2EnOwogICAgICAgICAgICAgICAgaWYoYj5hKXt6eno9MDsKICAgICAgICAgICAgICAgICAgICB3aGlsZSgxKXsKICAgICAgICAgICAgICAgICAgICAgICAgYT1zW2ldLSdhJzsKICAgICAgICAgICAgICAgICAgICAgICAgaWYoY2hbYV0uc2l6ZSgpPT0wKWJyZWFrOwogICAgICAgICAgICAgICAgICAgICAgICBlbHNlIGlmKGI8PWEpewogICAgICAgICAgICAgICAgICAgICAgICAgICAgd2hpbGUoMSl7CiAgICAgICAgICAgICAgICAgICAgICAgIGE9c1tpXS0nYSc7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmKGNoW2FdLnNpemUoKT09MClicmVhazsKICAgICAgICAgICAgICAgICAgICAgICAgZm9yKGludCBqPTA7ajxjaFthXS5zaXplKCk7aisrKXsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmKGNoW2FdW2pdPHNbaV0mJmNoW2FdW2pdPj1zW2ktMV0pewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHNbaV09Y2hbYV1bal07YnJlYWs7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgbGwgejQ9c1tpXS0nYSc7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmKHo0PT1hKWJyZWFrOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICBmb3IoaW50IGo9MDtqPGNoW2FdLnNpemUoKTtqKyspewogICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYoY2hbYV1bal0+PXNbaS0xXSl7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgenp6PTEsc1tpXT1jaFthXVtqXTticmVhazsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICBsbCB6ND1zW2ldLSdhJzsKICAgICAgICAgICAgICAgICAgICAgICAgaWYoejQ9PWEpYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgfQoKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGVsc2UgaWYoYT5iKXsKICAgICAgICAgICAgICAgICAgICB3aGlsZSgxKXsKICAgICAgICAgICAgICAgICAgICAgICAgYT1zW2ldLSdhJzsKICAgICAgICAgICAgICAgICAgICAgICAgaWYoY2hbYV0uc2l6ZSgpPT0wKWJyZWFrOwogICAgICAgICAgICAgICAgICAgICAgICBmb3IoaW50IGo9MDtqPGNoW2FdLnNpemUoKTtqKyspewogICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYoY2hbYV1bal08c1tpXSYmY2hbYV1bal0+PXNbaS0xXSl7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgc1tpXT1jaFthXVtqXTticmVhazsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICBsbCB6ND1zW2ldLSdhJzsKICAgICAgICAgICAgICAgICAgICAgICAgaWYoejQ9PWEpYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIAogICAgICAgIG9rPTE7CiAgICAgICAgZm9yKGludCBpPTE7aTxuO2krKyl7CiAgICAgICAgICAgIGlmKHNbaV08c1tpLTFdKXtvaz0wO2JyZWFrO30KICAgICAgICB9CiAgICAgICAgCiAgICAgICAgaWYob2spY291dDw8IllFU1xuIjsKICAgICAgICBlbHNlIGNvdXQ8PCJOT1xuIjsKICAgICAgICAKICAgIH0KfQo=