#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
long long int powmod(long long int a, long long int e) {
  long long int sum = 1;
  long long int cur = a;
  while (e > 0) {
    if (e % 2) {
      sum = sum * cur % mod;
    }
    cur = cur * cur % mod;
    e /= 2;
  }
  return sum;
}
void add(long long int &x, long long int y) { x = (x + y) % mod; }
int gcd(int x, int y) {
  while (y != 0) {
    int r = x % y;
    x = y;
    y = r;
  }
  return x;
}
const int N = 2100000;
long long int sum[N];
long long int sqsum[N];
long long int qs[N], qs2[N], q0[N], q1[N];
long long int r2[N], r1[N];
int pfac[N];
long long int gcdsum[N];
long long int gp0[N], gp1[N], gp2[N];
void init(void) {
  for (int i = (int)(1); i < (int)(N); i++) {
    sum[i] = sum[i - 1];
    add(sum[i], i);
  }
  for (int i = (int)(1); i < (int)(N); i++) {
    sqsum[i] = sqsum[i - 1];
    add(sqsum[i], (long long int)i * (long long int)i);
  }
  for (int i = (int)(1); i < (int)(N); i++) {
    q0[i] = q0[i - 1];
    q1[i] = q1[i - 1];
    qs[i] = qs[i - 1];
    qs2[i] = qs2[i - 1];
    long long int tmp = (long long int)i * (long long int)(i - 2 + mod) % mod;
    add(q0[i], tmp);
    add(q1[i], tmp * (i - 1));
    add(qs[i], tmp * sum[i - 1]);
    add(qs2[i], tmp * sqsum[i - 1]);
  }
  for (int i = (int)(1); i < (int)(N); i++) {
    r2[i] = r2[i - 1];
    r1[i] = r1[i - 1];
    long long int tmp = (long long int)i * (long long int)(i - 2 + mod) % mod;
    add(r1[i], tmp * (i - 1));
    long long int u = tmp + 1;
    add(r2[i], tmp * u);
  }
  for (int i = (int)(2); i < (int)(N); i++) {
    if (pfac[i] != 0) continue;
    pfac[i] = i;
    for (int j = (int)(2); j < (int)((N - 1) / i + 1); j++) pfac[i * j] = i;
  }
  for (int i = (int)(1); i < (int)(N); i++) {
    int v = i;
    long long int prod = 1;
    while (v != 1) {
      int p = pfac[v];
      int e = 0;
      while (v % p == 0) {
        v /= p;
        e++;
      }
      long long int tmp = powmod(p, e - 1);
      tmp = tmp * (e * (p - 1) + p) % mod;
      prod = tmp * prod;
    }
    gcdsum[i] = prod;
    gp0[i] = (gp0[i - 1] + prod) % mod;
    gp1[i] = (gp1[i - 1] + prod * i) % mod;
    gp2[i] = (gp2[i - 1] + (prod * i % mod) * i) % mod;
  }
}
long long int solve(int n, int m) {
  long long int tot = 0;
  int lim = min(n, m);
  long long int tmp = qs2[lim];
  add(tot, mod - tmp);
  tmp = qs[lim];
  tmp = tmp * (m + n + 2) % mod;
  add(tot, tmp);
  tmp = q1[lim];
  tmp = tmp * (m + 1) % mod;
  tmp = tmp * (n + 1) % mod;
  add(tot, mod - tmp);
  long long int coef = sqsum[lim];
  add(coef, (mod - m - n - 2) * sum[lim]);
  add(coef,
      (long long int)((long long int)lim * (m + 1) % mod) * (n + 1) % mod);
  add(tot, coef * q0[lim]);
  tot = tot * 2 % mod;
  add(tot, mod - r2[lim]);
  add(tot, (m + n) * r1[lim]);
  add(tot, (q0[lim] * m % mod) * (mod - n));
  long long int gcd_add = 0;
  add(gcd_add, (long long int)(m + 1) * (n + 1) % mod * gp0[lim]);
  add(gcd_add, (m + n + 2) * (mod - gp1[lim]));
  add(gcd_add, gp2[lim]);
  add(tot, gcd_add * 2);
  return tot;
}
long long int solve_naive(int n, int m) {
  long long int tot = 0;
  for (int x = (int)(1); x < (int)(min(n, m) + 1); x++) {
    for (int y = (int)(0); y < (int)(min(n, m) + 1 - x); y++) {
      long long int tmp = 0;
      add(tmp, (long long int)x * (long long int)(x - 2));
      add(tmp, (long long int)y * (long long int)(y - 2));
      add(tmp, 2 * gcd(x, y));
      tmp = tmp * (m - x - y + 1) % mod;
      tmp = tmp * (n - x - y + 1) % mod;
      add(tot, tmp);
    }
  }
  return tot;
}
int main(void) {
  init();
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    long long int ans = solve(n, m);
    cout << ans << "\n";
  }
}
