#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset= tree<T , null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update>;
int n , Q=18 , q;
struct block{
int cnt;
vector<int> mas;
vector<int> smas;
};
vector<int> mas;
vector<vector<int> > t;
vector<block> blocks;
vector<vector<int> > m;
vector<vector<int> > pref_m;
vector<vector<int> > pref_stolb;
void build(int v , int l , int r){
if(l==r){
t[v].push_back(mas[l]);
}
else{
int mid=(l+r)/2;
build(v*2 , l , mid);
build(v*2+1 , mid+1 , r);
int l1=0;
int l2=0;
while(l1!=t[v*2].size()||l2!=t[v*2+1].size()){
if(l1==t[v*2].size()){
t[v].push_back(t[v*2+1][l2]);
l2++;
}
else{
if(l2==t[v*2+1].size()){
t[v].push_back(t[v*2][l1]);
l1++;
}
else{
if(t[v*2][l1]<=t[v*2+1][l2]){
t[v].push_back(t[v*2][l1]);
l1++;
}
else{
t[v].push_back(t[v*2+1][l2]);
l2++;
}
}
}
}
}
}
int dih_t(int v , int x){
int siz=t[v].size();
int l=0 , r=siz-1;
int ans=0;
//cout << l << "-" << r << " " << siz << endl;
while(l<=r){
int mid=(l+r)/2;
if(t[v][mid]>x){
ans=siz-mid;
r=mid-1;
}
else{
l=mid+1;
}
}
// cout << ans << ";" << endl;
return ans;
}
int find_ans(int v , int l , int r , int tl , int tr , int x){
/*cout << v << endl;
system("pause");*/
if(tl==l&&tr==r){
return dih_t(v , x);
}
else{
int mid=(l+r)/2;
if(tr<=mid){
return find_ans(v*2 , l , mid , tl , tr , x);
}
if(tl>mid){
return find_ans(v*2+1 , mid+1 , r , tl , tr , x);
}
return find_ans(v*2 , l , mid , tl , mid , x)+find_ans(v*2+1 , mid+1 , r , mid+1 , tr , x);
}
}
int find_blocks(int a , int b){
int ans=0;
if(a==b){
return blocks[a].cnt;
}
if(a<b){
swap(a , b);
}
else{
int l1=0 , l2=0;
while(l1!=blocks[a].smas.size()&&l2!=blocks[b].smas.size()){
if(blocks[a].smas[l1]>blocks[b].smas[l2]){
ans+=blocks[a].smas.size()-l1;
l2++;
}
else{
l1++;
}
}
}
return ans;
}
int find_cnt(int a){
if(blocks[a].mas.size()==0){
return 0;
}
oset<int> s;
int ans=0;
for(int i=0;i<blocks[a].mas.size();i++){
ans+=s.size()-s.order_of_key(blocks[a].mas[i]);
s.insert(blocks[a].mas[i]);
}
return ans;
}
int pref_ans=0;
void solve(){
int ans=0;
int l , r;
cin >> l >> r;
l=((pref_ans+l-1)%n)+1;
r=((pref_ans+r-1)%n)+1;
l--;
r--;
if(l>r){
swap(l , r);
}
// cout << l << " " << r << endl;
int q_min=999999 , q_max=-1;
for(int i=l;i<=r;){
if(i%Q==0&&i+Q-1<=r){
ans+=blocks[i/Q].cnt;
q_min=min(q_min , i/Q);
q_max=i/Q;
i+=Q;
}
else{
if(i!=l){
ans+=find_ans(1 , 0 , n-1 , l , i-1 , mas[i]);
// cout << ans << endl;
}
//cout << "bebraa" << endl;
i++;
}
}
if(q_min!=999999){
if(q_min==0){
ans+=pref_m[q_max][q_max];
}
else{
ans+=pref_m[q_max][q_max]-pref_m[q_max][q_min-1]-pref_m[q_min][q_max-1];
}
}
cout << ans << endl;
pref_ans=ans;
}
int main(){
cin >> n;
mas.resize(n+1);
t.resize(4*(n+1)+1);
pref_m.resize(n/Q+1 , vector<int>(n/Q+1));
blocks.resize(n/Q+1);
m.resize(n/Q+1 , vector<int>(n/Q+1));
pref_stolb.resize(n/Q+1 , vector<int>(n/Q+1));
for(int i=0;i<n;i++){
cin >> mas[i];
}
build(1 , 0 , n-1);
/* for(int i=1;i<t.size();i++){
if(t[i].size()!=0){
cout << i << ": ";
for(int j=0;j<t[i].size();j++){
cout << t[i][j] << " ";
}
cout << endl;
}
}*/
for(int i=0;i<n;i++){
blocks[i/Q].mas.push_back(mas[i]);
}
for(int i=0;i<=n/Q;i++){
if(blocks[i].mas.size()!=0){
blocks[i].smas=blocks[i].mas;
sort(blocks[i].smas.begin() , blocks[i].smas.end());
}
}
for(int i=0;i<=n/Q;i++){
if(blocks[i].mas.size()!=0){
blocks[i].cnt=find_cnt(i);
}
}
for(int i=0;i<=n/Q;i++){
for(int j=0;j<=n/Q;j++){
m[i][j]=find_blocks(i , j);
if(i==0){
pref_stolb[i][j]=m[i][j];
}
else{
pref_stolb[i][j]=pref_stolb[i-1][j]+m[i][j];
}
if(j==0){
pref_m[i][j]=pref_stolb[i][j];
}
else{
pref_m[i][j]=pref_stolb[i][j]+pref_m[i][j-1];
}
}
}
cin >> q;
while(q--){
solve();
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2luY2x1ZGU8ZXh0L3BiX2RzL2Fzc29jX2NvbnRhaW5lci5ocHA+CiNpbmNsdWRlPGV4dC9wYl9kcy90cmVlX3BvbGljeS5ocHA+CiNwcmFnbWEgR0NDIG9wdGltaXplKCJPZmFzdCx1bnJvbGwtbG9vcHMiKQp1c2luZyBuYW1lc3BhY2Ugc3RkOwp1c2luZyBuYW1lc3BhY2UgX19nbnVfcGJkczsKdGVtcGxhdGU8Y2xhc3MgVD4gdXNpbmcgb3NldD0gdHJlZTxUICwgbnVsbF90eXBlICwgbGVzczxUPiAsIHJiX3RyZWVfdGFnICwgdHJlZV9vcmRlcl9zdGF0aXN0aWNzX25vZGVfdXBkYXRlPjsKaW50IG4gLCBRPTE4ICwgcTsKc3RydWN0IGJsb2NrewogICAgaW50IGNudDsKICAgIHZlY3RvcjxpbnQ+IG1hczsKICAgIHZlY3RvcjxpbnQ+IHNtYXM7Cn07CnZlY3RvcjxpbnQ+IG1hczsKdmVjdG9yPHZlY3RvcjxpbnQ+ID4gdDsKdmVjdG9yPGJsb2NrPiBibG9ja3M7CnZlY3Rvcjx2ZWN0b3I8aW50PiA+IG07CnZlY3Rvcjx2ZWN0b3I8aW50PiA+IHByZWZfbTsKdmVjdG9yPHZlY3RvcjxpbnQ+ID4gcHJlZl9zdG9sYjsKdm9pZCBidWlsZChpbnQgdiAsIGludCBsICwgaW50IHIpewogICAgaWYobD09cil7CiAgICAgICAgdFt2XS5wdXNoX2JhY2sobWFzW2xdKTsKICAgIH0KICAgIGVsc2V7CiAgICBpbnQgbWlkPShsK3IpLzI7CiAgICBidWlsZCh2KjIgLCBsICwgbWlkKTsKICAgIGJ1aWxkKHYqMisxICwgbWlkKzEgLCByKTsKICAgIGludCBsMT0wOwogICAgaW50IGwyPTA7CiAgICB3aGlsZShsMSE9dFt2KjJdLnNpemUoKXx8bDIhPXRbdioyKzFdLnNpemUoKSl7CiAgICAgICAgaWYobDE9PXRbdioyXS5zaXplKCkpewogICAgICAgIHRbdl0ucHVzaF9iYWNrKHRbdioyKzFdW2wyXSk7CiAgICAgICAgbDIrKzsKICAgICAgICB9CiAgICAgICAgZWxzZXsKICAgICAgICAgICAgaWYobDI9PXRbdioyKzFdLnNpemUoKSl7CiAgICAgICAgICAgICAgICB0W3ZdLnB1c2hfYmFjayh0W3YqMl1bbDFdKTsKICAgICAgICAgICAgICAgIGwxKys7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZXsKICAgICAgICAgICAgICAgIGlmKHRbdioyXVtsMV08PXRbdioyKzFdW2wyXSl7CiAgICAgICAgICAgICAgICAgICAgdFt2XS5wdXNoX2JhY2sodFt2KjJdW2wxXSk7CiAgICAgICAgICAgICAgICAgICAgbDErKzsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGVsc2V7CiAgICAgICAgICAgICAgICAgICAgdFt2XS5wdXNoX2JhY2sodFt2KjIrMV1bbDJdKTsKICAgICAgICAgICAgICAgICAgICBsMisrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgfQp9CmludCBkaWhfdChpbnQgdiAsIGludCB4KXsKICAgIGludCBzaXo9dFt2XS5zaXplKCk7CiAgICBpbnQgbD0wICwgcj1zaXotMTsKICAgIGludCBhbnM9MDsKICAgIC8vY291dCA8PCBsIDw8ICItIiA8PCByIDw8ICIgIiA8PCBzaXogPDwgZW5kbDsKICAgIHdoaWxlKGw8PXIpewogICAgICAgICAgICBpbnQgbWlkPShsK3IpLzI7CiAgICAgICAgICAgIGlmKHRbdl1bbWlkXT54KXsKICAgICAgICAgICAgICAgIGFucz1zaXotbWlkOwogICAgICAgICAgICAgICAgcj1taWQtMTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlewogICAgICAgICAgICAgICAgbD1taWQrMTsKICAgICAgICAgICAgfQogICAgfQogICAvLyBjb3V0IDw8IGFucyA8PCAiOyIgPDwgZW5kbDsKICAgIHJldHVybiBhbnM7Cn0KaW50IGZpbmRfYW5zKGludCB2ICwgaW50IGwgLCBpbnQgciAsIGludCB0bCAsIGludCB0ciAsIGludCB4KXsKICAgLypjb3V0IDw8IHYgPDwgZW5kbDsKICBzeXN0ZW0oInBhdXNlIik7Ki8KICAgIGlmKHRsPT1sJiZ0cj09cil7CiAgICAgICAgcmV0dXJuIGRpaF90KHYgLCB4KTsKICAgIH0KICAgIGVsc2V7CiAgICAgICAgaW50IG1pZD0obCtyKS8yOwogICAgICAgIGlmKHRyPD1taWQpewogICAgICAgICAgICByZXR1cm4gZmluZF9hbnModioyICwgbCAsIG1pZCAsIHRsICwgdHIgLCB4KTsKICAgICAgICB9CiAgICAgICAgaWYodGw+bWlkKXsKICAgICAgICAgICAgcmV0dXJuIGZpbmRfYW5zKHYqMisxICwgbWlkKzEgLCByICwgdGwgLCB0ciAsIHgpOwogICAgICAgIH0KICAgICAgICByZXR1cm4gZmluZF9hbnModioyICwgbCAsIG1pZCAsIHRsICwgbWlkICwgeCkrZmluZF9hbnModioyKzEgLCBtaWQrMSAsIHIgLCBtaWQrMSAsIHRyICwgeCk7CiAgICB9Cn0KaW50IGZpbmRfYmxvY2tzKGludCBhICwgaW50IGIpewogICAgaW50IGFucz0wOwogICAgaWYoYT09Yil7CiAgICAgICAgcmV0dXJuIGJsb2Nrc1thXS5jbnQ7CiAgICB9CiAgICBpZihhPGIpewogICAgICAgIHN3YXAoYSAsIGIpOwogICAgfQogICAgZWxzZXsKICAgICAgICBpbnQgbDE9MCAsIGwyPTA7CiAgICAgICAgd2hpbGUobDEhPWJsb2Nrc1thXS5zbWFzLnNpemUoKSYmbDIhPWJsb2Nrc1tiXS5zbWFzLnNpemUoKSl7CiAgICAgICAgICAgIGlmKGJsb2Nrc1thXS5zbWFzW2wxXT5ibG9ja3NbYl0uc21hc1tsMl0pewogICAgICAgICAgICAgICAgYW5zKz1ibG9ja3NbYV0uc21hcy5zaXplKCktbDE7CiAgICAgICAgICAgICAgICBsMisrOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2V7CiAgICAgICAgICAgICAgICBsMSsrOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIGFuczsKfQppbnQgZmluZF9jbnQoaW50IGEpewogICAgaWYoYmxvY2tzW2FdLm1hcy5zaXplKCk9PTApewogICAgICAgIHJldHVybiAwOwogICAgfQogICAgb3NldDxpbnQ+IHM7CiAgICBpbnQgYW5zPTA7CiAgICBmb3IoaW50IGk9MDtpPGJsb2Nrc1thXS5tYXMuc2l6ZSgpO2krKyl7CiAgICAgICAgYW5zKz1zLnNpemUoKS1zLm9yZGVyX29mX2tleShibG9ja3NbYV0ubWFzW2ldKTsKICAgICAgICBzLmluc2VydChibG9ja3NbYV0ubWFzW2ldKTsKICAgIH0KICAgIHJldHVybiBhbnM7Cn0KaW50IHByZWZfYW5zPTA7CnZvaWQgc29sdmUoKXsKICAgIGludCBhbnM9MDsKICAgIGludCBsICwgcjsKICAgIGNpbiA+PiBsID4+IHI7CiAgICBsPSgocHJlZl9hbnMrbC0xKSVuKSsxOwogICAgcj0oKHByZWZfYW5zK3ItMSklbikrMTsKICAgIGwtLTsKICAgIHItLTsKICAgIGlmKGw+cil7CiAgICAgICAgc3dhcChsICwgcik7CiAgICB9CiAgIC8vIGNvdXQgPDwgbCA8PCAiICIgPDwgciA8PCBlbmRsOwogICAgaW50IHFfbWluPTk5OTk5OSAsIHFfbWF4PS0xOwogICAgZm9yKGludCBpPWw7aTw9cjspewogICAgICAgIGlmKGklUT09MCYmaStRLTE8PXIpewogICAgICAgIGFucys9YmxvY2tzW2kvUV0uY250OwogICAgICAgIHFfbWluPW1pbihxX21pbiAsIGkvUSk7CiAgICAgICAgcV9tYXg9aS9ROwogICAgICAgIGkrPVE7CiAgICAgICAgfQogICAgICAgIGVsc2V7CiAgICAgICAgICAgIGlmKGkhPWwpewogICAgICAgICAgICBhbnMrPWZpbmRfYW5zKDEgLCAwICwgbi0xICwgbCAsIGktMSAsIG1hc1tpXSk7CiAgICAgLy8gICAgICAgY291dCA8PCBhbnMgPDwgZW5kbDsKICAgICAgICAgICAgfQogICAgICAgICAgICAvL2NvdXQgPDwgImJlYnJhYSIgPDwgZW5kbDsKICAgICAgICAgICAgaSsrOwogICAgICAgIH0KICAgIH0KICAgIGlmKHFfbWluIT05OTk5OTkpewogICAgICAgICAgICBpZihxX21pbj09MCl7CiAgICAgICAgICAgICAgICBhbnMrPXByZWZfbVtxX21heF1bcV9tYXhdOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2V7CiAgICAgICAgYW5zKz1wcmVmX21bcV9tYXhdW3FfbWF4XS1wcmVmX21bcV9tYXhdW3FfbWluLTFdLXByZWZfbVtxX21pbl1bcV9tYXgtMV07CiAgICAgICAgICAgIH0KICAgIH0KICAgIGNvdXQgPDwgYW5zIDw8IGVuZGw7CiAgICBwcmVmX2Fucz1hbnM7Cn0KaW50IG1haW4oKXsKICAgIGNpbiA+PiBuOwogICAgbWFzLnJlc2l6ZShuKzEpOwogICAgdC5yZXNpemUoNCoobisxKSsxKTsKICAgIHByZWZfbS5yZXNpemUobi9RKzEgLCB2ZWN0b3I8aW50PihuL1ErMSkpOwogICAgYmxvY2tzLnJlc2l6ZShuL1ErMSk7CiAgICBtLnJlc2l6ZShuL1ErMSAsIHZlY3RvcjxpbnQ+KG4vUSsxKSk7CiAgICBwcmVmX3N0b2xiLnJlc2l6ZShuL1ErMSAsIHZlY3RvcjxpbnQ+KG4vUSsxKSk7CiAgICBmb3IoaW50IGk9MDtpPG47aSsrKXsKICAgICAgICBjaW4gPj4gbWFzW2ldOwogICAgfQogICAgYnVpbGQoMSAsIDAgLCBuLTEpOwogIC8qICBmb3IoaW50IGk9MTtpPHQuc2l6ZSgpO2krKyl7CiAgICAgICAgaWYodFtpXS5zaXplKCkhPTApewogICAgICAgICAgICAgICAgY291dCA8PCBpIDw8ICI6ICI7CiAgICAgICAgICAgIGZvcihpbnQgaj0wO2o8dFtpXS5zaXplKCk7aisrKXsKICAgICAgICAgICAgICAgIGNvdXQgPDwgdFtpXVtqXSA8PCAiICI7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgY291dCA8PCBlbmRsOwogICAgICAgIH0KICAgIH0qLwogICAgZm9yKGludCBpPTA7aTxuO2krKyl7CiAgICAgICAgYmxvY2tzW2kvUV0ubWFzLnB1c2hfYmFjayhtYXNbaV0pOwogICAgfQogICAgZm9yKGludCBpPTA7aTw9bi9RO2krKyl7CiAgICAgICAgaWYoYmxvY2tzW2ldLm1hcy5zaXplKCkhPTApewogICAgICAgIGJsb2Nrc1tpXS5zbWFzPWJsb2Nrc1tpXS5tYXM7CiAgICAgICAgc29ydChibG9ja3NbaV0uc21hcy5iZWdpbigpICwgYmxvY2tzW2ldLnNtYXMuZW5kKCkpOwogICAgICAgIH0KICAgIH0KICAgIGZvcihpbnQgaT0wO2k8PW4vUTtpKyspewogICAgICAgICAgICBpZihibG9ja3NbaV0ubWFzLnNpemUoKSE9MCl7CiAgICAgICAgYmxvY2tzW2ldLmNudD1maW5kX2NudChpKTsKICAgICAgICAgICAgfQogICAgfQogICAgZm9yKGludCBpPTA7aTw9bi9RO2krKyl7CiAgICAgICAgZm9yKGludCBqPTA7ajw9bi9RO2orKyl7CiAgICAgICAgICAgIG1baV1bal09ZmluZF9ibG9ja3MoaSAsIGopOwogICAgICAgICAgICBpZihpPT0wKXsKICAgICAgICAgICAgICAgIHByZWZfc3RvbGJbaV1bal09bVtpXVtqXTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlewogICAgICAgICAgICAgICAgcHJlZl9zdG9sYltpXVtqXT1wcmVmX3N0b2xiW2ktMV1bal0rbVtpXVtqXTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZihqPT0wKXsKICAgICAgICAgICAgICAgIHByZWZfbVtpXVtqXT1wcmVmX3N0b2xiW2ldW2pdOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2V7CiAgICAgICAgICAgICAgICBwcmVmX21baV1bal09cHJlZl9zdG9sYltpXVtqXStwcmVmX21baV1bai0xXTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIGNpbiA+PiBxOwogICAgd2hpbGUocS0tKXsKICAgICAgICBzb2x2ZSgpOwogICAgfQogICAgcmV0dXJuIDA7Cn0K