Problem:
Search II

You are given a sequence of n integers S and a sequence of different q integers T. Write a program which outputs C, the number of integers in T which are also in the set S.

Input

In the first line n is given. In the second line, n integers are given. In the third line q is given. Then, in the fourth line, q integers are given.

Output

Print C in a line.

Constraints

Elements in S is sorted in ascending order

n ≤ 100000

q ≤ 50000

0 ≤ an element in S ≤ 109

0 ≤ an element in T ≤ 109

Sample Input 1

5
1 2 3 4 5
3
3 4 1

Sample Output 1

3

Sample Input 2

3
1 2 3
1
5

Sample Output 2

0

Sample Input 3

5
1 1 2 2 3
2
1 2

Sample Output 3

2

Notes

Solution in Rust (judge verdict: Accepted)

use std::collections::VecDeque;
use std::env;
//use std::fmt::Debug;
use std::io;
use std::io::prelude::*;
//use std::time::{Duration, Instant};

//===================================================
// MACROs need to be defined above the use place ... ?
macro_rules! dprintln {
    ($($x:expr),*) => {{
        if $crate::is_debug_mode() {
            let f = || {println!( $($x),*)};
            f()
        }
    }}
}

/// Backward For-Loop Helper
///  from -> to (exclude `to`)
///
/// # Examples
/// see tests::macro_backward()
///
#[macro_export]
macro_rules! backward_ho {
    ($from:expr, $to:expr) => {{
        (($to + 1)..($from + 1)).rev()
    }};
}
//===================================================

fn main() {
    // Initialize
    set_debug_mode(false);
    for arg in env::args() {
        if arg == "--debug" {
            set_debug_mode(true);
        }
    }
    dprintln!("[DebugMode] {}", "On");

    // Execute
    dprintln!("=================");
    dprintln!("= READ INPUT     ");
    let stdin = io::stdin();
    let input = Input::new(stdin.lock());
    dprintln!("[Input] \n{:?}", input);
    dprintln!("=================");
    dprintln!("= INTERPRET INPUT");
    let q = input.into_quiz();
    dprintln!("[Quiz] \n{:?}", q);
    dprintln!("=================");
    dprintln!("= SOLVE QUIZE    ");
    let a = q.solve();
    dprintln!("[Answer] \n{:?}", a);
    dprintln!("=================");
    dprintln!("= PRINT ANSWER   ");
    a.print();
    dprintln!("=================");
}

#[derive(Debug)]
struct Quiz {
    n1: usize,    // 0 <= N <= 100,000
    v1: Vec<u32>, // 0 <= v[i] <= 100,000,000, length=N, v[i] <= v[j] (all i,j: i < j)
    n2: usize,    // 0 <= N2 <= 50,000
    v2: Vec<u32>, // 0 <= v2[i] <= 100,000,000 len=N2, v2[i] != v2[j] (all i,j: i != j)
}

#[derive(Debug)]
struct Answer {
    count: u32,
}

impl<R: BufRead> Input<R> {
    fn into_quiz(mut self) -> Quiz {
        let n1: usize = self.parse_next().unwrap();
        let v1: Vec<u32> = self.parse_next_vec(n1).unwrap();
        let n2: usize = self.parse_next().unwrap();
        let v2: Vec<u32> = self.parse_next_vec(n2).unwrap();
        Quiz { n1, v1, n2, v2 }
    }
}

use std::cmp::*;
impl Quiz {
    fn solve(self) -> Answer {
        let mut count = 0;
        for &e2 in self.v2.iter() {
            if let Some(_i) = Quiz::bin_search(&self.v1, &e2) {
                count += 1;
            } else {
            }
        }
        Answer{ count }
    }

    fn bin_search<T>(s: &Vec<T>, t: &T) -> Option<usize> 
    where T: Ord + std::fmt::Display
    {
        let mut left = 0i32;
        let mut right = s.len() as i32 - 1;
        dprintln!("bs: search for {}", t);

        while left <= right {
            let i: usize = ((right - left)/2 + left) as usize;
            dprintln!("bs: l:{}, r:{}, i:{}, s[i]={}", left, right, i, s[i]);
            match t.cmp(&s[i]) {
                Ordering::Equal => {
                    dprintln!("bs: found");
                    return Some(i);
                },
                Ordering::Less  => {
                    dprintln!("bs:  next left");
                    right = i as i32 - 1;
                },
                Ordering::Greater => {
                    dprintln!("bs:  next right");
                    left = i as i32 + 1;
                },
            }
            dprintln!("bs:  next l:{}, r:{}", left, right);
        }

        dprintln!("bs: not found");

        None
    }
}

impl Answer {
    fn print(self) {
        println!("{}", self.count);
    }
}

// =====================================================
// =
// =====================================================

//==================================================
// Stdin Reader
#[derive(Debug)]
pub enum Token {
    Word(String),
    LineBreak,
}

impl Token {
    pub fn is_word(&self) -> bool {
        match *self {
            Token::Word(ref _x) => true,
            _ => false,
        }
    }
}

#[derive(Debug)]
pub struct Input<R: BufRead> {
    input: R,
    tokens: VecDeque<Token>,
}

impl<R: BufRead> Input<R> {
    pub fn new(input: R) -> Self {
        let tokens = VecDeque::new();

        Input { input, tokens }
    }

    pub fn read_next_word(&mut self) -> Option<Token> {
        loop {
            match self.tokens.pop_front() {
                None => {
                    // Read Input
                    let mut line = String::new();
                    let n = self.input.read_line(&mut line).expect("Read Error.");
                    if n == 0 {
                        return None;
                    }
                    let mut line_tokens = tokenaize(line);
                    self.tokens.append(&mut line_tokens);
                }
                Some(Token::LineBreak) => (),
                x => return x,
            }
        }
    }

    pub fn parse_next<T>(&mut self) -> Option<T>
    where
        T: std::str::FromStr,
    {
        if let Some(Token::Word(str)) = self.read_next_word() {
            match str.parse() {
                Ok(x) => Some(x),
                _ => None,
            }
        } else {
            None
        }
    }

    pub fn parse_next_vec<T>(&mut self, size: usize) -> Option<Vec<T>>
    where
        T: std::str::FromStr,
    {
        let mut v = Vec::new();

        for _i in 0..size {
            if let Some(t) = self.parse_next() {
                v.push(t);
            } else {
                return None;
            }
        }

        Some(v)
    }

    pub fn parse_next_vec2<T>(&mut self, size2: usize, size1: usize) -> Option<Vec<Vec<T>>>
    where
        T: std::str::FromStr,
    {
        let mut v = Vec::new();

        for _i in 0..size2 {
            if let Some(t) = self.parse_next_vec(size1) {
                v.push(t);
            } else {
                return None;
            }
        }

        Some(v)
    }

    pub fn parse_all_remaining_into_vec<T>(&mut self) -> Vec<T>
    where
        T: std::str::FromStr,
    {
        let mut v = Vec::new();

        loop {
            match self.parse_next() {
                Some(x) => v.push(x),
                None => return v,
            }
        }
    }
}

fn tokenaize(str: String) -> VecDeque<Token> {
    let mut v = VecDeque::new();

    for w in str.split_whitespace() {
        v.push_back(Token::Word(w.to_string()));
    }
    v.push_back(Token::LineBreak);
    v
}

//==================================================
// Utility Functions
pub fn concat_vec_to_string<T>(v: &Vec<T>) -> String
where
    T: std::fmt::Display,
{
    let mut string = String::with_capacity(v.len() * 2);

    if let Some((h, t)) = v.split_first() {
        string = h.to_string();

        for obj in t {
            string.push(' ');
            string.push_str(&obj.to_string());
        }
    }

    return string;
}

//==================================================
// Debug Switch
static mut S_DEBUG_MODE: bool = false;
fn is_debug_mode() -> bool {
    unsafe { S_DEBUG_MODE }
}

fn set_debug_mode(mode: bool) {
    unsafe {
        S_DEBUG_MODE = mode;
    }
}

//==================================================
// Usage
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn macro_backward() {
        let mut v = Vec::new();
        for i in backward_ho!(5, 1) {
            v.push(i);
        }
        assert_eq!(vec![5, 4, 3, 2], v);
    }
}