#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int q;
  scanf("%d", &q);
  while (q--) {
    long long ans = 1, n, p;
    scanf("%lld %lld", &n, &p);
    for (long long x = 1; x <= n; x *= 2) {
      long long nx = min(n + 1, x * 2);
      long long t = (ans * (nx - x) % p + ans) % p;
      ans = t;
    }
    printf("%lld\n", (ans - 1 + p) % p);
  }
}
