#include <bits/stdc++.h>
char *fs, *ft, buf[1 << 20];
inline long long read() {
  long long x = 0, f = 1;
  char ch =
      (fs == ft && (ft = (fs = buf) + fread(buf, 1, 1 << 20, stdin), fs == ft))
          ? 0
          : *fs++;
  ;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = (fs == ft &&
          (ft = (fs = buf) + fread(buf, 1, 1 << 20, stdin), fs == ft))
             ? 0
             : *fs++;
    ;
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = (fs == ft &&
          (ft = (fs = buf) + fread(buf, 1, 1 << 20, stdin), fs == ft))
             ? 0
             : *fs++;
    ;
  }
  return x * f;
}
using namespace std;
const long long N = 1e5 + 5;
const long long inf = 0x7fffffff;
const long long mod = 998244353;
const double eps = 1e-6;
long long a[N];
signed main() {
  long long t;
  cin >> t;
  while (t--) {
    long long d, m;
    cin >> d >> m;
    long long now = 1;
    long long cnt = 0;
    long long tot = 0;
    while (tot <= d) {
      a[++cnt] = now;
      tot += now;
      now *= 2;
    }
    now /= 2;
    tot -= now;
    a[cnt] = d - tot;
    long long res = 1;
    for (long long i = 1; i <= cnt; i++) {
      res = res * (a[i] + 1) % m;
    }
    res = (res + m - 1) % m;
    cout << res << '\n';
  }
}
