include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector vi;
typedef pair<int, int> pi;
define endl ‘\n’
define INF INT_MAX
void diskta(int S, vector& res, unordered_map<int, vector>& adj) {
fill(res.begin(), res.end(), INF);
res[S] = 0;
priority_queue<pi, vector, greater> pq;
pq.push({0, S});
while (!pq.empty()) {
int currnode = pq.top().second;
int dist = pq.top().first;
pq.pop();
if (dist > res[currnode]) {
continue;
}
for (const auto& [n, d] : adj[currnode]) {
if (d + dist < res[n]) {
res[n] = d + dist;
pq.push({res[n], n});
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll t;
cin >> t;
while (t--) {
ll n, m;
cin >> n >> m;
unordered_map<int, vector<pi>> mp;
vi arr(n + 1); // Change size to n + 1 to accommodate 1-based indexing
for (int i = 1; i <= n; i++) { // Read 1-based indexing
cin >> arr[i];
}
for (int i = 1; i <= n; ++i) { // Use 1-based index for node i
for (int j = 1; j <= arr[i]; ++j) { // Adjusted loop to start from 1
if (i + j <= n) { // Ensure 1-based index stays within bounds
mp[i].push_back({i + j, 1});
}
}
}
vector<vector<int>> allRes(n + 1, vector<int>(n + 1, INF)); // Change size to n + 1
for (int i = 1; i <= n; i++) { // Use 1-based index for source
diskta(i, allRes[i], mp);
}
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
if (allRes[x][y] == INF) { // 1-based index for queries
cout << -1 << endl;
} else {
cout << allRes[x][y] << endl;
}
}
}
return 0;
}