#include <bits/stdc++.h>
using namespace std;
const int lim = 1000050, mm = 1000000007;
bool flag[1100001];
long long g[1100001], f[1100001], f1[1100001], f2[1100001], ans, n, m;
int tests, i, j, k, phi[1100001], p[1100001], tot;
void init() {
  int i, j;
  memset(flag, 0, sizeof(flag));
  p[0] = 0;
  phi[1] = 1;
  for (i = 2; i <= lim; i++) {
    if (!flag[i]) {
      p[++p[0]] = i;
      phi[i] = i - 1;
    }
    for (j = 1; i * p[j] <= lim && j <= p[0]; j++) {
      flag[i * p[j]] = 1;
      if (i % p[j] == 0) {
        phi[i * p[j]] = phi[i] * p[j];
        break;
      }
      phi[i * p[j]] = phi[i] * (p[j] - 1);
    }
  }
  for (i = 1; i <= lim; i++) {
    g[i] =
        (long long)i * (long long)(2 * i + 1) * (long long)(i + 1) / 3LL % mm -
        3LL * (long long)i * (long long)i % mm;
    g[i] = (g[i] % mm + mm) % mm;
  }
  for (i = 1; i <= lim; i++)
    for (j = i; j <= lim; j += i)
      g[j] = (g[j] + 2LL * (long long)i * (long long)phi[j / i]) % mm;
  for (i = 1, f[0] = f1[0] = f2[0] = 0; i <= lim; i++) {
    f[i] = (f[i - 1] + g[i]) % mm;
    f1[i] = (f1[i - 1] + (long long)i * g[i]) % mm;
    f2[i] = (f2[i - 1] + (long long)i * (long long)i % mm * g[i]) % mm;
  }
}
int main() {
  scanf("%d", &tests);
  init();
  while (tests--) {
    scanf("%d%d", &j, &k);
    n = j;
    m = k;
    if (n > m) swap(n, m);
    ans = (n * m + n + m + 1) % mm * f[n] % mm;
    ans = (ans - (n + m + 2) * f1[n] % mm + mm) % mm;
    ans = (ans + f2[n]) % mm;
    k = ans;
    printf("%d\n", k);
  }
  return 0;
}
