#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1005;
int a1[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int a2[5] = {2, 4, 6, 8, 0};
int a3[10] = {3, 6, 9, 2, 5, 8, 1, 4, 7, 0};
int a4[5] = {4, 8, 2, 6, 0};
int a5[2] = {5, 0};
int a6[5] = {6, 2, 8, 4, 0};
int a7[10] = {7, 4, 1, 8, 5, 2, 9, 6, 3, 0};
int a8[5] = {8, 6, 4, 2, 0};
int a9[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int sum1 = 45, sum2 = 20, sum3 = 45, sum4 = 20, sum5 = 5, sum6 = 20,
      sum7 = 45, sum8 = 20, sum9 = 45;
  int t;
  cin >> t;
  while (t--) {
    long long n, m, b, ans = 0;
    cin >> n >> m;
    b = n / m;
    if (b == 0 || m % 10 == 0)
      cout << 0 << endl;
    else {
      if (m % 10 == 1) {
        ans = b / 10 * sum1;
        for (int i = 0; i < b % 10; i++) ans += a1[i];
        cout << ans << endl;
      } else if (m % 10 == 2) {
        ans = b / 5 * sum2;
        for (int i = 0; i < b % 5; i++) ans += a2[i];
        cout << ans << endl;
      } else if (m % 10 == 3) {
        ans = b / 10 * sum3;
        for (int i = 0; i < b % 10; i++) ans += a3[i];
        cout << ans << endl;
      } else if (m % 10 == 4) {
        ans = b / 5 * sum4;
        for (int i = 0; i < b % 5; i++) ans += a4[i];
        cout << ans << endl;
      } else if (m % 10 == 5) {
        ans = b / 2 * sum5;
        for (int i = 0; i < b % 2; i++) ans += a5[i];
        cout << ans << endl;
      } else if (m % 10 == 6) {
        ans = b / 5 * sum6;
        for (int i = 0; i < b % 5; i++) ans += a6[i];
        cout << ans << endl;
      } else if (m % 10 == 7) {
        ans = b / 10 * sum7;
        for (int i = 0; i < b % 10; i++) ans += a7[i];
        cout << ans << endl;
      } else if (m % 10 == 8) {
        ans = b / 5 * sum8;
        for (int i = 0; i < b % 5; i++) ans += a8[i];
        cout << ans << endl;
      }
      if (m % 10 == 9) {
        ans = b / 10 * sum9;
        for (int i = 0; i < b % 10; i++) ans += a9[i];
        cout << ans << endl;
      }
    }
  }
  return 0;
}
