#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const int H = 6;
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
long long quickpow(long long a, long long n) {
  long long ans = 1;
  while (n) {
    if (n & 1) ans = (ans * a) % MOD;
    a = (a * a) % MOD;
    n >>= 1;
  }
  return ans;
}
long long inverse(long long a) { return quickpow(a, MOD - 2); }
void print(vector<long long> a) {
  for (int i = 0; i < a.size(); i++) {
    cout << a[i] << " ";
  }
  cout << endl;
}
vector<long long> operator*(vector<long long> a, long long b) {
  for (int i = 0; i < a.size(); i++) {
    a[i] *= b;
    a[i] %= MOD;
  }
  return a;
}
vector<long long> operator/(vector<long long> a, long long b) {
  return a * inverse(b);
}
vector<long long> operator+(vector<long long> a, vector<long long> b) {
  vector<long long> c;
  int n = max(a.size(), b.size());
  c.resize(n);
  for (int i = 0; i < a.size(); i++) {
    c[i] += a[i];
    c[i] %= MOD;
  }
  for (int i = 0; i < b.size(); i++) {
    c[i] += b[i];
    c[i] %= MOD;
  }
  return c;
}
vector<long long> operator*(vector<long long> a, vector<long long> b) {
  vector<long long> c;
  c.resize(a.size() + b.size() - 1);
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < b.size(); j++) {
      c[i + j] += a[i] * b[j] % MOD;
      c[i + j] %= MOD;
    }
  }
  return c;
}
const int SIZEN = 1000010;
long long calc_presum_with(vector<long long> a, long long ps[]) {
  long long ans = 0;
  for (int i = 0; i < a.size(); i++) {
    ans += (a[i] * ps[i]) % MOD;
    ans %= MOD;
  }
  return ans;
}
vector<long long> give_Fsum_L(void) {
  static long long A[10];
  A[0] = 1;
  vector<long long> ans(A, A + 1);
  A[0] = 0, A[1] = 1;
  ans = ans * vector<long long>(A, A + 2);
  A[0] = 1, A[1] = 1;
  ans = ans * vector<long long>(A, A + 2);
  A[0] = 1, A[1] = 2;
  ans = ans * vector<long long>(A, A + 2);
  ans = ans / 3;
  A[0] = 0, A[1] = 0, A[2] = -3;
  ans = ans + vector<long long>(A, A + 3);
  return ans;
}
long long G[SIZEN] = {0};
long long phi[SIZEN];
long long P[SIZEN][H], PG[SIZEN][H];
void work(long long N, long long M) {
  static long long A[10];
  if (N > M) swap(N, M);
  A[0] = M + 1, A[1] = -1;
  vector<long long> D(A, A + 2);
  A[0] = N + 1, A[1] = -1;
  D = D * vector<long long>(A, A + 2);
  vector<long long> F = give_Fsum_L();
  vector<long long> F1 = D * F;
  long long ans = 0;
  ans += calc_presum_with(F1, P[N]);
  ans %= MOD;
  A[0] = 2;
  vector<long long> F2 = D * vector<long long>(A, A + 1);
  ans += calc_presum_with(F2, PG[N]);
  ans %= MOD;
  ans = (ans + MOD) % MOD;
  printf("%I64d\n", ans);
}
void prepare(void) {
  for (int i = 1; i < SIZEN; i++) {
    phi[i] += i;
    for (int j = i + i; j < SIZEN; j += i) {
      phi[j] -= phi[i];
    }
  }
  for (int g = 1; g < SIZEN; g++) {
    G[g] += g;
    G[g] %= MOD;
    for (int x = 2 * g, d = 2; x < SIZEN; x += g, d++) {
      G[x] += (g * phi[d] % MOD);
      G[x] %= MOD;
    }
  }
  for (int i = 0; i < H; i++) {
    P[0][i] = PG[0][i] = 0;
  }
  for (long long x = 1; x < SIZEN; x++) {
    long long p = 1;
    for (int i = 0; i < H; i++) {
      P[x][i] = (P[x - 1][i] + p) % MOD;
      PG[x][i] = (PG[x - 1][i] + (p * G[x]) % MOD) % MOD;
      p = (p * x) % MOD;
    }
  }
}
int main() {
  prepare();
  long long T, N, M;
  scanf("%I64d", &T);
  while (T--) {
    scanf("%I64d%I64d", &N, &M);
    work(N, M);
  }
  return 0;
}
