1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
#include <bits/stdc++.h> #define all(vec) vec.begin(),vec.end() #define CLR(i,a) memset(i,a,sizeof(i)) #define fi first #define se second #define pb push_back #define SZ(a) ((int) a.size()) #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define debug(var) cerr << #var <<":"<<var<<"\n";
#define lson(var) (var<<1) #define rson(var) ((var<<1)+1)
using namespace std;
typedef long long ll;typedef unsigned long long ull; typedef double DB;typedef long double LD; typedef __int128 i128;typedef pair<DB,DB> pdd;typedef pair<ll,bool> plb; using arc2=array<char,2>; typedef pair<ll,ll> pll; typedef array<ll,3> arr3;typedef array<ll,2> arr2; constexpr ll MAXN=static_cast<ll>(2e5)+10,INF=static_cast<ll>(1e9)+9; constexpr ll mod=static_cast<ll>(1e9)+7; constexpr double eps=1e-8;
ll N,M,Q,X,K,lT;
set<ll> g[50]; inline void solve(){ cin>>N>>Q; FOR(i, 0, 50-1) g[i].clear(); string s; cin>>s; FOR(i,1,Q){ char x,y; cin>>x>>y; g[(x-'a')*5+y-'a'].insert(i); } FOR(i,0,N-1){ if(s[i]=='a'){ continue; } if(s[i]=='c'){ char x='c',y='a'; ll idx=(x-'a')*5+y-'a'; if(!g[idx].empty()){ g[idx].erase(g[idx].begin()); s[i]='a'; }else{ ll idx2=('c'-'a')*5+'b'-'a',idx3=('b'-'a')*5; if(g[idx2].empty()) continue; if(g[idx3].empty()){ g[idx2].erase(prev(g[idx2].end())); s[i]='b'; }else{ if(*g[idx2].begin()<*g[idx3].rbegin()){ auto it=g[idx3].lower_bound(*g[idx2].begin()); g[idx2].erase(g[idx2].begin()); g[idx3].erase(it); s[i]='a'; }else{ g[idx2].erase(g[idx2].begin()); s[i]='b'; } } } }else{ char x='b',y='a'; ll idx=(x-'a')*5+y-'a';
if(!g[idx].empty()){ g[idx].erase(g[idx].begin()); s[i]='a'; }else{ ll idx2=('b'-'a')*5+'c'-'a',idx3=('c'-'a')*5; if(g[idx2].empty()) continue; if(g[idx3].empty()){ g[idx2].erase(prev(g[idx2].end())); s[i]='b'; }else{ if(*g[idx2].begin()<*g[idx3].rbegin()){ auto it=g[idx3].lower_bound(*g[idx2].begin()); g[idx2].erase(g[idx2].begin()); g[idx3].erase(it); s[i]='a'; } } } } } cout<<s<<"\n"; }
int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>lT; while(lT--){ solve(); } return 0; }
|