#include <bits/stdc++.h>
using namespace std;
int N, M;
long long tblX[1001001];
int minp[1001001];
long long gs[1001001];
long long bsum[1001001];
long long bsums0[1001001];
long long bsums1[1001001];
long long bsums2[1001001];
long long linsum(long long K) {
  long long P = K, Q = K + 1;
  if (P % 2 == 0)
    P /= 2;
  else
    Q /= 2;
  return (P * Q) % 1000000007;
}
long long sqrsum(long long K) {
  long long ret = K;
  ret = ret * (K + 1) % 1000000007;
  ret = ret * ((2 * K + 1) % 1000000007) % 1000000007;
  return ret * ((1000000007 + 1) / 6) % 1000000007;
}
void erat() {
  for (int i = 0; i <= 1000000; i++) minp[i] = -1;
  for (int i = 2; i <= 1000000; i++)
    if (minp[i] == -1) {
      for (int j = i; j <= 1000000; j += i)
        if (minp[j] == -1) minp[j] = i;
    }
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long count(long long a, long long b) {
  if (min(N, M) <= a + b - 1) return 0;
  long long pat = (long long)(N - (a + b - 1)) * (M - (a + b - 1)) % 1000000007;
  long long ec = a * a + b * b - 2 * (a + b) + 2 * gcd(a, b);
  ec %= 1000000007;
  return pat * ec % 1000000007;
}
void calc_gs() {
  gs[1] = 1;
  for (int i = 2; i <= 1000000; i++) {
    int p = i, q = i;
    long long aux = 0;
    int ord = 0;
    while (q % minp[i] == 0) {
      ++ord;
      q /= minp[i];
    }
    long long pow = 1;
    for (int j = 0; j < ord - 1; j++) pow = (pow * minp[i]);
    aux += pow * minp[i];
    aux += pow * (minp[i] - 1) * ord;
    aux %= 1000000007;
    aux = aux * gs[q] % 1000000007;
    gs[i] = aux;
  }
}
void calc_bsum() {
  bsum[0] = 0;
  for (int i = 1; i <= 1000000; i++) {
    long long tmp = sqrsum(i) + sqrsum(i - 1) + 2 * 1000000007 -
                    (2 * i * (long long)i % 1000000007) + 2 * gs[i];
    bsum[i] = tmp % 1000000007;
  }
}
void totalize() {
  bsums0[0] = bsums1[0] = bsums2[0] = 0;
  for (int i = 1; i <= 1000000; i++) {
    bsums0[i] = (bsums0[i - 1] + bsum[i]) % 1000000007;
    bsums1[i] = (bsums1[i - 1] + bsum[i] * i % 1000000007) % 1000000007;
    bsums2[i] = (bsums2[i - 1] + bsum[i] * i % 1000000007 * i % 1000000007) %
                1000000007;
  }
}
int main() {
  erat();
  calc_gs();
  calc_bsum();
  totalize();
  int T;
  scanf("%d", &T);
  for (int i = 0; i < T; i++) {
    scanf("%d%d", &N, &M);
    if (N > M) swap(N, M);
    long long ret = (long long)(N + 1) * (M + 1) % 1000000007 * bsums0[N];
    ret += 1000000007 - ((long long)(N + M + 2) * bsums1[N] % 1000000007);
    ret += bsums2[N];
    ret %= 1000000007;
    printf("%d\n", (int)ret);
  }
  return 0;
}
