/// Rustで競技プログラミングの入力をスッキリ記述するマクロ
///
/// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[inline]
#[allow(dead_code)]
fn gcd(mut m: u64, mut n: u64) -> u64 {
    if m < n {
        std::mem::swap(&mut m, &mut n);
    }

    loop {
        let k = m % n;
        if k == 0 {
            return n;
        }

        m = n;
        n = k;
    }
}

#[inline]
#[allow(dead_code)]
fn lcm(m: u64, n: u64) -> u64 {
    m / gcd(m, n) * n
}

// nc と同じ値を取れない最上位の桁を返す
fn not_equal_digit(nc: &[char], ok_vc: &[char]) -> Option<usize> {
    for (digit, c) in nc.iter().rev().enumerate().rev() {
        if !ok_vc.contains(c) {
            return Some(digit);
        }
    }
    None
}

// digit 以上の桁のうち ni より大きな値が取れる最下位の桁と、その値のタプルを返す
fn over_digit(ni: &[i64], ok_vi: &[i64], digit: usize) -> (usize, i64) {
    for (digit, &i) in ni.iter().rev().enumerate().skip(digit) {
        for &ok_i in ok_vi {
            if ok_i > i {
                return (digit, ok_i);
            }
        }
    }

    (ni.len(), *ok_vi.iter().find(|&&ok_i| ok_i > 0).unwrap())
}

fn main() {
    input!{
        nc: chars,
        k: usize,
        d_v: [i64; k],
    }

    let ni: Vec<i64> = nc.iter().map(|c| c.to_digit(10).unwrap() as i64).collect();

    let ok_vi: Vec<i64> = (0..10i64)
        .into_iter()
        .filter(|i| !d_v.contains(i))
        .collect();

    let ok_vc: Vec<char> = ok_vi
        .iter()
        .map(|&i| std::char::from_digit(i as u32, 10).unwrap())
        .collect();

    if let Some(not_equal_digit) = not_equal_digit(&nc, &ok_vc) {
        let (over_digit, over_num) = over_digit(&ni, &ok_vi, not_equal_digit);

        for digit in (0..(ni.len()+1)).rev() {
            if digit == over_digit {
                print!("{}", over_num);
            } else if digit > over_digit && digit >= ni.len() {
                continue;
            } else if digit > over_digit {
                print!("{}", nc[digit]);
            } else {
                print!("{}", ok_vc[0]);
            }
        }
        println!("");
        return;
    } else {
        for c in &nc {
            print!("{}", c);
        }
        println!("");
        return;
    }
}



Measured memory_bytes: 4352