#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define LL long long
#define pii pair<int,int>
#define MP make_pair
#define ls i << 1
#define rs ls | 1
#define md (ll + rr >> 1)
#define lson ll, md, ls
#define rson md + 1, rr, rs
#define Pi acos(-1.0)
#define mod 1000000007
#define eps 1e-10
#define inf 0x3f3f3f3f
#define N 100010
#define M 800020
struct node{
int l, r, d;
node(int l = 0, int r = 0, int d = 0) : l(l), r(r), d(d) {}
bool operator < (const node &b) const {
return r < b.r || r == b.r && l < b.l;
}
};
int n, m, cnt, a[N];
int san[N*30], val[N], nxt[N];
LL c[N*30];
vector<node> vt[N];
int gcd(int x, int y){
return y == 0 ? x : gcd(y, x % y);
}
int haxi(int val){
return lower_bound(san + 1, san + 1 + cnt, val) - san;
}
int main(){
int cas, kk = 0;
scanf("%d", &cas);
while(cas--){
scanf("%d", &n);
for(int i = 1; i <= n; ++i){
scanf("%d", &a[i]);
vt[i].clear();
}
cnt = 0;
nxt[1] = 1, val[1] = a[1];
san[++cnt] = a[1];
vt[1].push_back(node(1, 1, val[1]));
for(int i = 2; i <= n; ++i){
nxt[i] = i, val[i] = a[i];
int j = i;
while(j > 0){
val[j] = gcd(val[j], a[i]);
while(nxt[j] > 1 && gcd(val[nxt[j]-1], val[j]) == val[j])
nxt[j] = nxt[nxt[j]-1];
san[++cnt] = val[j];
vt[i].push_back(node(nxt[j], j, val[j]));
j = nxt[j] - 1;
}
}
sort(san + 1, san + 1 + cnt);
cnt = unique(san + 1, san + 1 + cnt) - san - 1;
for(int i = 1; i <= cnt; ++i) c[i] = 0;
for(int i = 1; i <= n; ++i){
sort(vt[i].begin(), vt[i].end());
for(int j = 0; j < vt[i].size(); ++j){
int v = haxi(vt[i][j].d);
c[v] += vt[i][j].r - vt[i][j].l + 1;
}
}
printf("Case #%d:\n", ++kk);
scanf("%d", &m);
while(m--){
int l, r;
scanf("%d%d", &l, &r);
int k = lower_bound(vt[r].begin(), vt[r].end(), node(-1, l, -1)) - vt[r].begin();
printf("%d ", vt[r][k].d);
int v = haxi(vt[r][k].d);
printf("%lld\n", c[v]);
}
}
return 0;
}