#include <bits/stdc++.h>
using namespace std;
int main() {
  int tc;
  cin >> tc;
  while (tc--) {
    long long n, m;
    cin >> n >> m;
    long long cnt = n / m, tmp = 0, sum = 0;
    set<long long> st;
    vector<int> v;
    for (int i = 0; i < n; i++) {
      tmp += m;
      tmp %= 10;
      if (st.find(tmp) != st.end()) break;
      v.push_back(tmp);
      st.insert(tmp);
      sum += tmp;
    }
    long long ans = (cnt / (1LL * st.size())) * sum;
    cnt = (cnt % (1LL * st.size()));
    for (int i = 0; i < cnt; i++) ans += v[i];
    cout << ans << endl;
  }
}
