#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int t;
  scanf("%d", &t);
  for (int i = 0; i < t; i++) {
    long long n, m;
    scanf("%lld %lld", &n, &m);
    int last_num = m % 10;
    long long div = n / m;
    vector<int> v;
    int cycle = 0;
    int sum = last_num;
    int cnt = 0;
    while (sum != 0) {
      cycle += sum;
      sum += last_num;
      sum %= 10;
      cnt++;
      v.push_back(cycle);
    }
    v.push_back(cycle + 0);
    cnt++;
    long long ddiv = div / cnt;
    long long remain = div % cnt;
    long long result = ddiv * v[cnt - 1];
    if (remain != 0) result += v[remain - 1];
    printf("%lld\n", result);
  }
  return 0;
}
