#include <bits/stdc++.h>
using namespace std;
using namespace std;
long long mod = 1000000007;
int maxn = 1024 * 1024, two = 2;
vector<long long> phi, F, A[4], ans, G[4];
int main(int argc, char const *argv[]) {
  phi = vector<long long>(maxn, 0), F = vector<long long>(maxn, 0),
  ans = vector<long long>(maxn, 0);
  for (int i = 0; i < 4; i++) {
    A[i] = vector<long long>(maxn, 0);
    G[i] = vector<long long>(maxn, 0);
  }
  for (long long i = 1; i < maxn; ++i) {
    phi[i] = ((((phi[i] + i) % mod) + mod) % mod);
    F[i] = ((F[i] + phi[i]) % mod);
    A[1][i] = ((A[1][i - 1] + i) % mod);
    A[2][i] = ((A[2][i - 1] + (i - 1) * (i - 1)) % mod);
    A[3][i] = ((((i * i) % mod) * i) % mod);
    for (long long j = 2; i * j < maxn; ++j) {
      phi[i * j] -= phi[i];
      F[i * j] += ((phi[i] * j) % mod);
    }
    ans[i] =
        ((two * A[2][i] + A[3][i] - two * i * (A[1][i]) + two * F[i]) % mod);
    ans[i] = ((ans[i] + mod) % mod);
    G[0][i] = ((G[0][i - 1] + ans[i]) % mod);
    G[1][i] = ((G[1][i - 1] + ans[i] * i) % mod);
    G[2][i] = ((G[2][i - 1] + ans[i] * ((i * i) % mod)) % mod);
  }
  int t;
  long long a, b;
  scanf("%d", &t);
  for (int cs = 0; cs < t; ++cs) {
    scanf("%I64d %I64d", &a, &b);
    int n = min(a, b);
    ++a, ++b;
    long long res = ((a * b) % mod) * G[0][n] - (a + b) * G[1][n] + G[2][n];
    printf("%I64d\n", ((((res) % mod) + mod) % mod));
  }
  return 0;
}
