#include <bits/stdc++.h>
using namespace std;
int phi[1000060], v[1000060];
long long int sum[1000060], fun[1000060], afun[1000060], sfun[1000060];
long long int pro(long long int a, long long int b) {
  return (a * b) % 1000000007;
}
int main() {
  int i, a, x, n, m, t;
  long long int y, b, ans;
  fill(v + 2, v + 1000060, 1);
  for (i = 1; i < 1000060; i++) phi[i] = i;
  for (i = 2; i < 1000060; i++) {
    if (v[i]) {
      phi[i]--;
      for (a = i + i; a < 1000060; a += i)
        v[a] = 0, phi[a] = phi[a] - phi[a] / i;
    }
  }
  for (i = 1; i < 1000060; i++) {
    for (a = i, x = 1; a < 1000060; a += i, x++) sum[a] += (i * phi[x]);
    sum[i] -= i;
    sum[i] %= 1000000007;
  }
  for (i = 1; i < 1000060; i++) {
    b = i, b *= (i + 1), b /= 2, b *= (2 * i + 1), b /= 3;
    b = b % 1000000007, b += i, b += sum[i], b *= 2;
    y = i, y *= i, y *= 3, y %= 1000000007;
    fun[i] = (b - y + 1000000007) % 1000000007;
    afun[i] = pro(i, fun[i]);
    sfun[i] = pro(i, afun[i]);
  }
  for (i = 2; i < 1000060; i++) {
    fun[i] = (fun[i] + fun[i - 1]) % 1000000007;
    afun[i] = (afun[i] + afun[i - 1]) % 1000000007;
    sfun[i] = (sfun[i] + sfun[i - 1]) % 1000000007;
  }
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n), scanf("%d", &m);
    x = min(n, m);
    n++, m++;
    ans = 0;
    ans += pro(pro(n, m), fun[x]);
    ans -= pro(n + m, afun[x]);
    ans += sfun[x], ans += 1000000007;
    ans %= 1000000007, x = ans;
    printf("%d\n", x);
  }
}
