#include <bits/stdc++.h>
using namespace std;
void getre() {
  int x = 0;
  printf("%d\n", 1 / x);
}
void gettle() {
  int res = 1;
  while (1) res <<= 1;
  printf("%d\n", res);
}
template <typename T, typename S>
inline bool upmin(T &a, const S &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T, typename S>
inline bool upmax(T &a, const S &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename N, typename PN>
inline N flo(N a, PN b) {
  return a >= 0 ? a / b : -((-a - 1) / b) - 1;
}
template <typename N, typename PN>
inline N cei(N a, PN b) {
  return a > 0 ? (a - 1) / b + 1 : -(-a / b);
}
template <typename N>
N gcd(N a, N b) {
  return b ? gcd(b, a % b) : a;
}
template <typename N>
inline int sgn(N a) {
  return a > 0 ? 1 : (a < 0 ? -1 : 0);
}
inline void gn(long long &x) {
  int sg = 1;
  char c;
  while (((c = getchar()) < '0' || c > '9') && c != '-')
    ;
  c == '-' ? (sg = -1, x = 0) : (x = c - '0');
  while ((c = getchar()) >= '0' && c <= '9') x = x * 10 + c - '0';
  x *= sg;
}
inline void gn(int &x) {
  long long t;
  gn(t);
  x = t;
}
inline void gn(unsigned long long &x) {
  long long t;
  gn(t);
  x = t;
}
inline void gn(double &x) {
  double t;
  scanf("%lf", &t);
  x = t;
}
inline void gn(long double &x) {
  double t;
  scanf("%lf", &t);
  x = t;
}
inline void gs(char *s) { scanf("%s", s); }
inline void gc(char &c) {
  while ((c = getchar()) > 126 || c < 33)
    ;
}
inline void pc(char c) { putchar(c); }
inline long long sqr(long long a) { return a * a; }
inline double sqrf(double a) { return a * a; }
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846264338327950288L;
const double eps = 1e-6;
const int mo = 1e9 + 7;
int qp(int a, long long b) {
  int n = 1;
  do {
    if (b & 1) n = 1ll * n * a % mo;
    a = 1ll * a * a % mo;
  } while (b >>= 1);
  return n;
}
const int SIEVE_MAXN = 1000000 + 5;
int prime[SIEVE_MAXN > 100000 ? SIEVE_MAXN / 10 : 10000];
bool bo[SIEVE_MAXN];
int mu[SIEVE_MAXN];
int phi[SIEVE_MAXN];
int nex[SIEVE_MAXN], deg[SIEVE_MAXN];
int sieve(int n) {
  int tot = 0;
  mu[1] = 1;
  phi[1] = 1;
  for (int i = 2; i <= n; i++) {
    if (!bo[i]) {
      prime[++tot] = i;
      mu[i] = -1;
      phi[i] = i - 1;
      nex[i] = 1;
      deg[i] = 1;
    }
    for (int j = 1; j <= tot && prime[j] * i <= n; j++) {
      bo[i * prime[j]] = 1;
      if (i % prime[j] == 0) {
        mu[i * prime[j]] = 0;
        phi[i * prime[j]] = phi[i] * prime[j];
        nex[i * prime[j]] = nex[i];
        deg[i * prime[j]] = deg[i] + 1;
        break;
      } else {
        mu[i * prime[j]] = -mu[i];
        phi[i * prime[j]] = phi[i] * (prime[j] - 1);
        nex[i * prime[j]] = i;
        deg[i * prime[j]] = 1;
      }
    }
  }
  return tot;
}
int arr[1111];
int tot;
int fac[55];
int ifac[55];
int main() {
  sieve(1000000);
  fac[0] = 1;
  for (int i = (1), _ed = (50); i < _ed; i++)
    fac[i] = 1ll * fac[i - 1] * i % mo;
  for (int i = (0), _ed = (50); i < _ed; i++) ifac[i] = qp(fac[i], mo - 2);
  int q;
  gn(q);
  while (q--) {
    int r, n;
    gn(r);
    gn(n);
    tot = 0;
    while (n > 1) {
      arr[++tot] = deg[n];
      n = nex[n];
    }
    int an = 1;
    for (int i = (1), _ed = (tot + 1); i < _ed; i++) {
      int su = 1;
      int k = arr[i];
      for (int j = (1), _ed = (k + 1); j < _ed; j++)
        (((su) = 1ll * (su) * (r + j) % mo) < 0 ? (su) += mo : (su));
      (((su) = 1ll * (su) * (2) % mo) < 0 ? (su) += mo : (su));
      (((su) = 1ll * (su) * (ifac[k]) % mo) < 0 ? (su) += mo : (su));
      int su1 = 1;
      if (r == 0)
        su1 = 0;
      else {
        for (int j = (1), _ed = (k + 1); j < _ed; j++)
          (((su1) = 1ll * (su1) * (r - 1 + j) % mo) < 0 ? (su1) += mo : (su1));
        (((su1) = 1ll * (su1) * (ifac[k]) % mo) < 0 ? (su1) += mo : (su1));
      }
      (((su) = ((su) + (-su1)) % mo) < 0 ? (su) += mo : (su));
      (((an) = 1ll * (an) * (su) % mo) < 0 ? (an) += mo : (an));
    }
    (((an) = ((an) + (0)) % mo) < 0 ? (an) += mo : (an));
    printf("%d\n", an);
  }
  return 0;
}
