#include <bits/stdc++.h>
using namespace std;
long long sum1[1000005], sum2[1000005], sum3[1000005], sum4[1000005], a, b,
    fi[1000005];
int T, i, A[1000005], v[1000005], j, o;
const int MOD = 1000000007;
int main() {
  fi[1] = 1;
  for (i = 2; i <= 1000000; i++) {
    if (!v[i]) {
      A[++o] = i;
      fi[i] = i - 1;
    }
    for (j = 1; j <= o && A[j] * i <= 1000000; j++) {
      v[A[j] * i] = 1;
      if (i % A[j] == 0)
        fi[A[j] * i] = fi[i] * A[j];
      else
        fi[A[j] * i] = fi[i] * (A[j] - 1);
      if (i % A[j] == 0) break;
    }
  }
  for (i = 1; i <= 1000000; i++)
    for (j = i; j <= 1000000; j += i)
      sum1[j] = (sum1[j] + (long long)fi[j / i] * i) % MOD;
  for (i = 1; i <= 1000000; i++)
    sum2[i] = ((long long)i * (i + 1) * (2 * i + 1) / 3 - (long long)i * i * 3 +
               2 * sum1[i]) %
              MOD;
  for (i = 1; i <= 1000000; i++) {
    sum3[i] = (long long)(i - 1) * sum2[i] % MOD;
    sum4[i] = (long long)(i - 1) * (i - 1) % MOD * sum2[i] % MOD;
  }
  for (i = 2; i <= 1000000; i++) {
    sum2[i] = (sum2[i] + sum2[i - 1]) % MOD;
    sum3[i] = (sum3[i] + sum3[i - 1]) % MOD;
    sum4[i] = (sum4[i] + sum4[i - 1]) % MOD;
  }
  scanf("%d", &T);
  while (T--) {
    cin >> a >> b;
    cout << (a * b % MOD * sum2[min(a, b)] - (a + b) * sum3[min(a, b)] % MOD +
             sum4[min(a, b)]) %
                MOD
         << endl;
  }
  return 0;
}
