#[allow(dead_code)]
fn is_whitespace(c: &char) -> bool {
    c.is_whitespace()
}

#[allow(dead_code)]
fn is_eol(c: &char) -> bool {
    ['\r', '\n'].contains(c)
}

#[allow(unused_macros)]
macro_rules! input {
    ($var:ident, $($r:tt)*) => {
        input_inner!{$var, $($r)*}
    };

    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        #[allow(unused_mut, unused_variables, bare_trait_objects)]
        let mut next = move |f: &Fn(&char)->bool| -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|f(&c))
                .take_while(|c|!f(&c))
                .collect()
        };
        input_inner!{next, $($r)*};
    };
}

#[allow(unused_macros)]
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)*}
    };

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

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; eol ]) => {
        $next(&is_eol).split(' ').map(|x| x.parse::<$t>().unwrap()).collect::<Vec<_>>()
    };

    ($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, bytes) => {
        read_value!($next, String).into_bytes()
    };

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

    ($next:expr, parser) => {
        $next
    };

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

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{BinaryHeap, HashMap, HashSet};

#[allow(dead_code)]
struct Edge {
    from: usize,
    to: usize,
}

#[allow(dead_code)]
type Graph = Vec<Vec<usize>>;
#[allow(dead_code)]
const M: usize = 1_000_000_007;

fn apply(m: usize, value: usize) -> (usize, usize) {
    let res = m + value;
    if res >= 10 {
        (2, res % 10 + 1)
    } else {
        (1, res)
    }
}

fn applyn(m: usize, value: usize, n: usize) -> (usize, usize) {
    let mut cnt = 0;
    let mut m = m;
    for _ in 0..n {
        let (c, r) = apply(m, value);
        cnt += c;
        m = r;
    }
    (cnt, m)
}

fn main() {
    input! {
        m: usize,
        dc: [(usize, usize); m],
    }

    let mut loops = vec![vec![(0, 0, 0, 0); 10]; 10];

    for i in 0..10 {
        for j in 0..10 {
            let mut cache = vec![None; 10];
            cache[i] = Some((0, 0));
            let mut m = i;
            let mut concat_cnt = 0;
            let mut apply_cnt = 0;
            loop {
                apply_cnt += 1;
                let (c, r) = apply(m, j);
                concat_cnt += c;
                m = r;
                if cache[m].is_some() {
                    loops[i][j] = (
                        cache[m].unwrap().0,
                        concat_cnt - cache[m].unwrap().0,
                        apply_cnt - cache[m].unwrap().1,
                        cache[m].unwrap().1,
                    );
                    break;
                }
                cache[m] = Some((concat_cnt, apply_cnt));
            }
            // println!("{} {} {} {}", i, j, cnt, apply_cnt);
        }
    }

    let mut cnt = 0;
    let mut m = 0;
    for (d, c) in dc {
        let mut c = c;
        let (_, con_loop_cnt, loop_size, initial_cost) = loops[m][d];
        if c >= initial_cost {
            let (cc, mm) = applyn(m, d, initial_cost);
            c -= initial_cost;
            cnt += cc;
            m = mm;
        } else {
            let (cc, mm) = applyn(m, d, c);
            println!("hoge");
            m = mm;
            cnt += cc;
            continue;
        }
        let apply_left = c % loop_size;
        let cn = (c / loop_size) * con_loop_cnt;
        cnt += cn;
        let (cc, mm) = applyn(m, d, apply_left);
        cnt += cc;
        m = mm;
    }

    println!("{}", cnt - 1);
    // let x = applyn(0, 5, 2);
    // println!("{} {}", x.0, x.1);
}


Measured memory_bytes: 8444