#include <bits/stdc++.h>
using namespace std;
const int N = 1000000, M = 1000000007, D3 = (M + 1) / 3;
int T, n, m;
int pd[N + 5], pr[N], pn, phi[N + 5];
int f[N + 5], s0[N + 5], s1[N + 5], s2[N + 5];
void init() {
  phi[1] = 1;
  for (int i = 2; i <= N; i++) {
    if (!pd[i]) phi[i] = i - 1, pr[++pn] = i;
    for (int j = 1; j <= pn && i * pr[j] <= N; j++) {
      pd[i * pr[j]] = pr[j];
      if (i % pr[j] == 0) {
        phi[i * pr[j]] = phi[i] * pr[j];
        break;
      } else
        phi[i * pr[j]] = phi[i] * phi[pr[j]];
    }
  }
  for (int d = 1; d <= N; d++)
    for (int k = d; k <= N; k += d) f[k] += d * phi[k / d];
  for (int L = 1; L <= N; L++) {
    f[L] =
        (((long long)L * L - L - L) % M * L % M -
         (long long)(L + 1) * L % M * L % M +
         (long long)L * (L + 1) % M * (L + L + 1) % M * D3 % M + 2LL * f[L]) %
        M;
    if (f[L] < 0) f[L] += M;
  }
  for (int i = 1; i <= N; i++) {
    s0[i] = (s0[i - 1] + (long long)f[i]) % M;
    s1[i] = (s1[i - 1] + (long long)i * f[i]) % M;
    s2[i] = (s2[i - 1] + (long long)i * i % M * f[i]) % M;
  }
}
int main() {
  init();
  for (scanf("%d", &T); T--;) {
    scanf("%d%d", &n, &m);
    if (n > m) swap(n, m);
    long long ans = (long long)(n + 1) * (m + 1) % M * s0[n] % M;
    ans -= (long long)(n + m + 2) * s1[n] % M;
    ans += s2[n];
    ans = (ans % M + M) % M;
    printf("%d\n", ans);
  }
  return 0;
}
