1 #include<bits/stdc++.h> 2 using namespace std; 3 #define rep(i,a,b) for(int i=a;i<=b;++i) 4 #define ms(arr,a) memset(arr,a,sizeof arr) 5 const int maxn=1e5+5; 6 int A[maxn]; 7 int nxt[maxn];//nxt[i]表示第i位之后第一个比A[i]小的A[j]的j 8 int n; 9 int main() 10 { 11 int T;scanf("%d",&T); 12 while(T--) 13 { 14 scanf("%d",&n); 15 rep(i,1,n)nxt[i]=n+1; 16 stack<int> s; 17 while(!s.empty()) s.pop(); 18 rep(i,1,n) 19 { 20 scanf("%d",A+i); 21 while(!s.empty()&&A[s.top()]>A[i]) 22 { 23 nxt[s.top()]=i; 24 s.pop(); 25 } 26 s.push(i); 27 } 28 int m;scanf("%d",&m); 29 while(m--) 30 { 31 int l,r;scanf("%d%d",&l,&r); 32 int i=l+1; 33 int ans=A[l]; 34 while(i<=r) 35 { 36 if(A[i]==0)break; 37 if(ans==0)break; 38 ans%=A[i]; 39 i=nxt[i]; 40 } 41 printf("%d ",ans); 42 } 43 } 44 }