#pragma GCC optimize("Ofast")
#include <bits/stdc++.h> 
using namespace std;

typedef long long ll;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0) 
#define rep(pp,nn) for (int i = pp; i < nn; ++i)
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
#define endl "\n"

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

string int_to_string(int64_t x) {
    stringstream ss;
	ss << x;
	string ni = ss.str();
	return ni;
}
int64_t string_to_int(string x) {
	int64_t n;
	stringstream s(x);
	s >> n;
	return n;
}

int64_t power_mod(ll a, ll b, ll mo) {
	ll ans = 1;
	while (b) {
		if(b & 1) {
			ans = (ans % mo * a % mo) % mo;
		}
		a = (a % mo * a % mo) % mo;
		b /= 2;
	}
	return ans;
}
int64_t POW(ll a, ll b) {
	ll ans = 1;
	while (b) {
		if(b & 1) {
			ans = ans * a;
		}
		a = a * a;
		b /= 2;
	}
	return ans;
}

vector<int64_t> INVERSE(int64_t nn, int64_t mo) {
	vector<int64_t> inv(nn + 1);
	vector<int64_t> mul(nn + 1);
	inv[0] = 1;
    inv[1] = 1;
	for (ll i = 2; i <= nn; ++i) {
		inv[i] = (mo - (mo / i) * inv[mo % i] % mo) % mo; 
	}
	mul[0] = 1;
	for (int i = 1; i <= nn; ++i) {
		mul[i] = (mul[i - 1] * inv[i]) % mo;
	}
	return mul;
}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}

#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
	
vector<int> v(5e5 + 1);
vector<int> last_index(5e5 + 1, -1);
vector<int> fenwich(5e5 + 1);
int n, q;

struct FENWICH {
	void update(int index, int val) {
		for (; index <= n; index += index & -index) {
			fenwich[index] += val;
		}
	}
	int query(int index) {
		int cnt = 0;
		for (; index > 0; index -= index & -index) {
			cnt += fenwich[index];
		}
		return cnt;
	}
};

int main() {
	FENWICH fen;
	cin >> n >> q;
	for (int i = 1; i <= n; ++i) {
		cin >> v[i];
	}
	vector<vector<int>> query(q, vector<int> (3));
	for (int i = 0; i < q; ++i) {
		cin >> query[i][1] >> query[i][0];
		query[i][2] = i;
	}
	sort(query.begin(), query.end());
	vector<int> ans(q);
	int q_index = 0;
	for (int i = 1; i <= n; ++i) {
		if (last_index[v[i]] != -1) {
			fen.update(last_index[v[i]], -1);
		}
		last_index[v[i]] = i;
		fen.update(i, 1);
		while (q_index < q && query[q_index][0] == i) {
			ans[query[q_index][2]] = fen.query(query[q_index][0]) - fen.query(query[q_index][1] - 1);
			q_index++;
		}
	}
	for (int i = 0; i < q; ++i) {
		cout << ans[i] << endl;
	}
}







Measured memory_bytes: 38936