#include <bits/stdc++.h>
using namespace std;
const int mo = 1000000007;
const long long inv3 = 333333336;
int P[1000005], fi[1000005], F[1000005], n, m, T, t;
int s[4], W[1000005][4];
int main() {
  fi[1] = 1;
  for (int i = 2; i <= 1000000; ++i) {
    if (!P[i]) P[++t] = i, fi[i] = i - 1;
    for (int j = 1; j <= t; ++j) {
      int x = P[j] * i;
      if (x > 1000000) break;
      P[x] = 1;
      if (i % P[j] == 0) {
        fi[x] = fi[i] * P[j];
        break;
      }
      fi[x] = fi[i] * (P[j] - 1);
    }
  }
  for (int i = 1; i <= 1000000; ++i) {
    s[0] = 1ll * i * i % mo;
    s[1] = s[2] = mo - i;
    s[3] = 1;
    for (int j = i; j <= 1000000; j += i) (F[j] += i * fi[j / i]) %= mo;
    long long x = ((2 * inv3 * i % mo * i % mo * i % mo - 2ll * i * i % mo +
                    inv3 * i % mo + 2ll * F[i]) %
                       mo +
                   mo) %
                  mo;
    for (int j = 0; j < 4; ++j) W[i][j] = (W[i - 1][j] + s[j] * x) % mo;
  }
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &m);
    if (n > m) swap(n, m);
    ++n;
    ++m;
    printf("%lld\n",
           (W[n - 1][0] + 1ll * W[n - 1][1] * n % mo +
            1ll * W[n - 1][2] * m % mo + 1ll * W[n - 1][3] * n % mo * m % mo) %
               mo);
  }
  return 0;
}
