#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<usize>();
    let c = read_vec::<usize>()
        .iter()
        .map(|&x| x - 1)
        .collect::<Vec<_>>();
    let (n, q) = (v[0], v[1]);
    let mut queries = vec![];
    for i in 0..q {
        let v = read_vec::<usize>();
        let (l, r) = (v[0] - 1, v[1] - 1);
        queries.push((l, r, i));
    }
    queries.sort();
    queries.reverse();

    let mut poses = vec![vec![]; n];
    let mut spans = vec![];
    for i in 0..n {
        if !poses[c[i]].is_empty() {
            let last_pos = poses[c[i]][poses[c[i]].len() - 1];
            spans.push((last_pos, i));
        }
        poses[c[i]].push(i);
    }
    spans.sort();
    spans.reverse();
    // debug!(spans);
    let mut spans = spans.into_iter().collect::<VecDeque<_>>();

    let mut answers = vec![0; q];
    let mut seg = SegTree::new(n, 0, |x, y| x + y);
    for (l, r, i) in queries {
        while !spans.is_empty() && spans[0].0 >= l {
            seg.update(spans[0].1, 1);
            spans.pop_front();
        }
        let ded = seg.query(l, r + 1);
        answers[i] = r - l + 1 - ded;
    }
    for ans in answers {
        println!("{}", ans);
    }
}

#[derive(Clone)]
struct SegTree<T, F>
where
    F: Fn(T, T) -> T,
    T: std::clone::Clone + std::marker::Copy,
{
    n: usize,
    dat: Vec<T>,
    init: T,
    functor: F,
}

impl<T, F> SegTree<T, F>
where
    F: Fn(T, T) -> T,
    T: std::clone::Clone + std::marker::Copy,
{
    fn new(n: usize, init: T, f: F) -> SegTree<T, F> {
        let mut m = 1;
        // For simplicity, we use 2 ** n sized SegTree.
        while m < n {
            m *= 2;
        }
        SegTree {
            n: m,
            dat: vec![init; 2 * m - 1],
            init: init,
            functor: f,
        }
    }

    // dat[k] = a;
    fn update(&mut self, k: usize, a: T) {
        let mut k = k;
        k += self.n - 1;
        self.dat[k] = a;
        while k > 0 {
            k = (k - 1) / 2;
            self.dat[k] = (self.functor)(self.dat[k * 2 + 1], self.dat[k * 2 + 2]);
        }
    }

    // [a, b)
    fn query(&self, a: usize, b: usize) -> T {
        self.query_inner(a, b, 0, 0, self.n)
    }

    fn query_inner(&self, a: usize, b: usize, k: usize, l: usize, r: usize) -> T {
        if r <= a || b <= l {
            return self.init;
        }
        if a <= l && r <= b {
            return self.dat[k];
        }

        let vl = self.query_inner(a, b, k * 2 + 1, l, (l + r) / 2);
        let vr = self.query_inner(a, b, k * 2 + 2, (l + r) / 2, r);
        (self.functor)(vl, vr)
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}


Measured memory_bytes: 56888