#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 50;
inline int min(int a, int b) { return a < b ? a : b; }
inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int a[1 << 20 | 1], Log[1 << 20 | 1];
int G[1 << 20 | 1][20];
inline int GCD(int l, int r)
{
    int k = Log[r - l + 1];
    return gcd(G[l][k], G[r - (1 << k) + 1][k]);
}
int main()
{
    Log[0] = -1;
    for (int i = 1; i <= N; i++) Log[i] = Log[i >> 1] + 1;
    map<int, long long> M;
    int T, n, q;
    scanf("%d", &T);
    for (int t = 1; t <= T; t++)
    {
        M.clear();
        printf("Case #%d:\n", t);
        scanf("%d", &n);
        for (int i = 0; i < n; i++) scanf("%d", a + i);
        for (int i = 0; i < n; i++) G[i][0] = a[i];
        for (int j = 1; j <= Log[n]; j++)
            for (int i = 0; i + (1 << j) - 1 < n; i++)
                G[i][j] = gcd(G[i][j - 1], G[i + (1 << (j - 1))][j - 1]);
        for (int i = 0; i < n; i++)
        {
            int g = G[i][0], j = i;
            while (j < n)
            {
                int l = j, r = n, m;
                while (r - l > 1)
                {
                    if (GCD(i, m = (l + r) >> 1) == g)
                        l = m;
                    else
                        r = m;
                }
                if (GCD(i, m = l) != g) m = r;
                M[g] += m - j + 1;
                j = l + 1;
                g = GCD(i, j);
            }
        }
        scanf("%d", &q);
        for (int i = 0, l, r; i < q; i++)
        {
            scanf("%d%d", &l, &r);
            l--, r--;
            printf("%d %lld\n", GCD(l, r), M[GCD(l, r)]);
        }
    }
    return 0;
}
