#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
void add(long long& a, const long long& b) { a = (a + b) % mod; }
long long mul(const long long& a, const long long& b) { return (a * b) % mod; }
const int maxnm = 1e6 + 79;
vector<int> lp(maxnm, 1), pr, phi(maxnm, 1);
vector<long long> g(maxnm, 0), f(maxnm, 0), fl(maxnm, 0), fl2(maxnm, 0);
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  for (int i = 2; i < maxnm; i++) {
    if (lp[i] == 1) lp[i] = i, pr.push_back(i);
    phi[i] = (lp[i] == lp[i / lp[i]] ? lp[i] : lp[i] - 1) * phi[i / lp[i]];
    for (int j = 0; j < pr.size() && pr[j] * i < maxnm && pr[j] <= lp[i]; j++)
      lp[pr[j] * i] = pr[j];
  }
  for (int gcd = 1; gcd < maxnm; gcd++)
    for (int num = gcd; num < maxnm; num += gcd) g[num] += phi[num / gcd] * gcd;
  for (long long l = 1; l < maxnm; l++) {
    f[l] =
        (l * l * l - 2 * l * l + (l * (l + 1) * (1 - l)) / 3 + 2 * g[l]) % mod;
    fl[l] = mul(f[l], l);
    fl2[l] = mul(fl[l], l);
    add(f[l], f[l - 1]);
    add(fl[l], fl[l - 1]);
    add(fl2[l], fl2[l - 1]);
  }
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    int l = min(n, m);
    n++, m++;
    long long ans = mul(mul(n, m), f[l]) - mul(n + m, fl[l]) + fl2[l];
    ans = (ans % mod + mod) % mod;
    cout << ans << "\n";
  }
  return 0;
}
