#include <bits/stdc++.h>
using namespace std;
const int mo = 1000000007;
const int N = 1000005;
int f[N], phi[N];
int g[N], h[N];
int S1(int x) { return 1ll * x * (x + 1) % mo * (mo + 1) / 2 % mo; }
int S2(int x) {
  return 1ll * x * (x + 1) % mo * (2 * x + 1) % mo * (mo + 1) / 2 % mo *
         (mo + 1) / 3 % mo;
}
void init() {
  for (int i = (int)(1); i <= (int)(N - 1); i++)
    f[i] = (1ll * i * i + 1ll * S1(i) * i + mo - S2(i)) % mo;
  for (int i = (int)(1); i <= (int)(N - 1); i++) phi[i] = i;
  for (int i = (int)(1); i <= (int)(N - 1); i++)
    for (int j = (int)(2); j <= (int)((N - 1) / i); j++) phi[i * j] -= phi[i];
  ;
  for (int i = (int)(1); i <= (int)(N - 1); i++)
    for (int j = (int)(1); j <= (int)((N - 1) / i); j++)
      f[i * j] = (f[i * j] + mo - phi[i] * j) % mo;
  for (int i = (int)(1); i <= (int)(N - 1); i++) f[i] = 2ll * f[i] % mo;
  for (int i = (int)(1); i <= (int)(N - 1); i++)
    f[i] = (1ll * i * i * i + mo - f[i]) % mo;
  for (int i = (int)(1); i <= (int)(N - 1); i++)
    g[i] = 1ll * f[i] * i % mo, h[i] = 1ll * f[i] * i % mo * i % mo;
  for (int i = (int)(1); i <= (int)(N - 1); i++) f[i] = (f[i] + f[i - 1]) % mo;
  for (int i = (int)(1); i <= (int)(N - 1); i++) g[i] = (g[i] + g[i - 1]) % mo;
  for (int i = (int)(1); i <= (int)(N - 1); i++) h[i] = (h[i] + h[i - 1]) % mo;
}
void solve() {
  int n, m, ans = 0;
  scanf("%d%d", &n, &m);
  if (n > m) swap(n, m);
  ans = 1ll * (n + 1) * (m + 1) % mo * f[n] % mo;
  ans = (ans + mo - 1ll * (n + m + 2) * g[n] % mo) % mo;
  ans = (ans + h[n]) % mo;
  printf("%d\n", ans);
}
int main() {
  init();
  int T;
  scanf("%d", &T);
  while (T--) solve();
}
