/* USE AT YOUR OWN RISK */
/* Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn */
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <ios>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <iostream>
#include <streambuf>
#include <new>
#include <locale>
#include <memory>
#include <numeric>
#include <utility>
#include <iterator>
#include <typeinfo>
#include <algorithm>
#include <exception>
#include <stdexcept>
#include <functional>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <vector>
#include <complex>
#if ((defined (__GNUC__) && __cplusplus >= 201103L) || (defined (_MSC_VER) && __cplusplus >= 199711))
# include <array>
# include <regex>
# include <tuple>
# include <random>
# include <typeindex>
# include <type_traits>
# include <forward_list>
# include <unordered_map>
# include <unordered_set>
# include <initializer_list>
# if (!defined (_MSC_VER))
# include <mutex>
# include <ratio>
# include <atomic>
# include <chrono>
# include <future>
# include <thread>
# include <condition_variable>
# endif
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////
const char * INPUTFILE = NULL;
const char * OUTPUTFILE = NULL;
const char * ERROR_LOG__FILE = NULL;
const FILE * INPUT = (INPUTFILE ? freopen (INPUTFILE, "r", stdin) : stdin);
const FILE * OUTPUT = (OUTPUTFILE ? freopen (OUTPUTFILE, "w", stdout) : stdout);
const FILE * ERROR_LOG = (ERROR_LOG__FILE ? freopen (ERROR_LOG__FILE, "w", stderr) : stderr);
///////////////////////////////////////////////////////////////////////////////////////////////////
inline double TIME () {
return (static_cast < double > (clock ())) / (static_cast < double > (CLOCKS_PER_SEC));
}
#if (defined (__GNUC__) && __cplusplus >= 201103L)
using namespace std :: chrono;
#endif
using namespace std;
const int MAX_N = (int) 1e5;
const int MAX_Q = (int) 5e4;
const int SQRT_N = 350;
int t, n, c, q;
int a [MAX_N];
struct query {
int l, r, id;
bool operator < (const query & a) const {
if (l / SQRT_N != a.l / SQRT_N) {
return l / SQRT_N < a.l / SQRT_N;
} else {
return r < a.r;
}
}
} qr [MAX_Q];
pair < int, int > ans [MAX_Q];
int l, r;
int cnt [MAX_N], ptr;
int cntcnt [MAX_N + MAX_N + 1];
inline void add (const int & id) {
-- cntcnt [cnt [a [id]] + MAX_N];
++ cnt [a [id]];
++ cntcnt [cnt [a [id]] + MAX_N];
if (ptr < cnt [a [id]]) {
++ ptr;
}
}
inline void del (const int & id) {
-- cntcnt [cnt [a [id]] + MAX_N];
-- cnt [a [id]];
++ cntcnt [cnt [a [id]] + MAX_N];
if (cntcnt [ptr + MAX_N] == 0) {
-- ptr;
}
}
inline void SolveOrDie () /* noexcept */ {
scanf ("%d", &t);
for (int test = 1; test <= t; ++ test) {
scanf ("%d%d%d", &n, &c, &q);
for (int i = 0; i < n; ++ i) {
scanf ("%d", &a [i]);
-- a [i];
}
for (int i = 0; i < q; ++ i) {
scanf ("%d%d", &qr [i].l, &qr [i].r);
-- qr [i].l; -- qr [i].r;
qr [i].id = i;
}
sort (qr, qr + q);
memset (cnt, 0, sizeof cnt);
memset (cntcnt, 0, sizeof cntcnt);
cntcnt [0] = n;
ptr = 0;
l = r = 0;
add (l);
for (int i = 0; i < q; ++ i) {
while (l > qr [i].l) {
-- l;
add (l);
}
while (l < qr [i].l) {
del (l);
++ l;
}
while (r > qr [i].r) {
del (r);
-- r;
}
while (r < qr [i].r) {
++ r;
add (r);
}
ans [i].second = ptr;
ans [i].first = qr [i].id;
}
sort (ans, ans + q);
printf ("Case %d:\n", test);
for (int i = 0; i < q; ++ i) {
printf ("%d\n", ans [i].second);
}
}
}
int main (const int argc, const char ** argv) /* noexcept */ {
assert (INPUT);
try {
SolveOrDie ();
} catch (const exception & e) {
cerr << e.what () << endl;
exit (-1);
} catch (...) {
cerr << "Exception\n";
exit (-1);
}
return 0;
}