#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
using namespace std;
const long long MOD = 998244353;
mt19937_64 rnd(time(0));
long long n, m, k, d, h, g, T, Q, kol[1000001];
vector<long long> v;
map<long long, int> w;
bool tt[1000001];
long long binpow(long long h, long long r, long long md = MOD) {
  long long l = 1;
  while (r) {
    if (r & 1) l *= h, l %= md;
    h *= h;
    h %= md;
    r /= 2;
  }
  return l;
}
long long gcd(long long x, long long y) {
  if (x < y) swap(x, y);
  while (x && y) {
    x %= y;
    swap(x, y);
  }
  return x + y;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long D;
  cin >> D;
  long long p = sqrt(D);
  for (long long i = 1; i <= p; i++)
    if (D % i == 0) {
      v.push_back(i);
      if (i != D / i) v.push_back(D / i);
    }
  sort(v.begin(), v.end());
  long long n = v.size() - 1;
  vector<long long> smp;
  w[1] = 0;
  for (int i = 1; i <= n; i++) {
    w[v[i]] = i;
    if (!tt[i]) {
      smp.push_back(v[i]);
      for (int j = i + 1; j <= n; j++)
        if (v[j] % v[i] == 0) {
          tt[j] = 1;
        }
    }
  }
  kol[0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < smp.size(); j++)
      if (v[i] % smp[j] == 0) {
        kol[i] = (kol[i] + kol[w[v[i] / smp[j]]]) % MOD;
      }
  }
  long long q;
  cin >> q;
  while (q--) {
    long long x, y;
    cin >> x >> y;
    long long z = gcd(x, y);
    x /= z;
    y /= z;
    cout << (kol[w[x]] * kol[w[y]]) % MOD << "\n";
  }
  return 0;
}
