#include<bits/stdc++.h>
using namespace std;

#define all(v) v.begin(), v.end()

#ifdef LOCAL
#define debug(...) [](auto...a){((cout << a << ' '), ...) << endl; }(#__VA_ARGS__, ": ", __VA_ARGS__)
#define debugv(v) do { cout << #v << ": "; for (auto x : v) cout << x << ' ';cout << endl; } while(0)
#else
#define debug(...)
#define debugv(v) 
#endif

void solve();

int32_t main() {
  cin.tie(0); ios_base::sync_with_stdio(0);
  int t = 1;
  cin >> t;
  while (t--) solve(); 
}

const int K = 250; 
mt19937 rng(0);

void solve() {
  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  for (int &x : a) cin >> x;
  for (int t = 0; t < q; ++t) {
    int l, r;
    cin >> l >> r;
    --l;
    vector<int> ind;
    for (int i = 0; i < K; ++i) {
      ind.push_back(l + rng() % (r - l));
    }
    sort(all(ind));
    ind.erase(unique(all(ind)), ind.end());
    int k = ind.size();
    vector<int> lis(k, n + 1);
    int ans = 0;
    for (int i : ind) {
      int index = lower_bound(all(lis), a[i]) - lis.begin();
      lis[index] = a[i];
      ans = max(ans, index + 1);
    }
    cout << (ans * 1LL * (r - l)) / k << '\n';
  }
}
