#![allow(non_snake_case, unused_imports)] use std::cmp::Reverse; use std::collections::{BinaryHeap, HashMap, HashSet}; use proconio::{input, marker::Usize1, marker::Chars}; use itertools::Itertools; #[allow(unused_macros)] macro_rules! d { ( $( $x:expr ),* $(,)? ) => { eprintln!( concat!( $( stringify!($x), "={:?} " ),* ), $( $x ),* ); }; } #[allow(dead_code)] fn yn(b: bool) -> &'static str { if b { "Yes" } else { "No" } } fn replace_first(s: &str, fm: char, to: &str) -> String { let mut res = String::new(); let mut replaced = false; for c in s.chars() { if !replaced && c == fm { res.push_str(to); replaced = true; } else { res.push(c) } } res } fn solve() -> String { input! { R: String, S: String, K: usize, } let prefix_count = |c: char| -> usize { R.chars() .take(K) .filter(|&x| x == c) .count() }; let suffix_count = |c: char| -> usize { R.chars() .skip(K) .filter(|&x| x == c) .count() }; match S.as_str() { "Warong" => { let mut res = String::new(); for _ in 0..K { res.push('A'); } let q = suffix_count('?'); let w = suffix_count('W'); let cs: Vec = R.chars().collect(); let mut b = q == 1 && w == 0; // ? を W に置き換える for i in K as usize..R.len() { if cs[i] == '?' && b { res.push('W'); b = false; } else { res.push(cs[i]); } } res } "NotWarong" => { if prefix_count('A') == K { return R.replace('?', "A"); } let suffix_has_w = suffix_count('W') > 0; if suffix_has_w { let a = prefix_count('A'); let q = prefix_count('?'); if a == K-1 && q == 1 { return replace_first(&R, '?', "W"); } } R } _ => unreachable!(), } } fn main() { input! { T: usize, } for _ in 0..T { let ans = solve(); println!("{}", ans); } }