#include <bits/stdc++.h>

#define int int64_t

signed main()
{
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int n, q;
  std::cin >> n >> q;
  std::vector<char> a(n + 1);
  for(int i = 1; i <= n; i++) {
    std::cin >> a[i];
  }
  std::vector<int> sufi(n + 2);
  std::vector<int> cnt6i(n + 2);
  int sufiks = 0;
  std::vector<int> suf_val(n + 2);
  for(int i = n; i >= 1; i--) {
    sufi[i] = sufi[i + 1];
    cnt6i[i] = cnt6i[i + 1];
    if(a[i] == '6' || a[i] == '?') {
      cnt6i[i] += 1;
      sufi[i] += sufiks;
    }
    else {
      sufiks += 1;
    }
    suf_val[i] = sufiks;
  }
  std::vector<int> count6(n + 2);
  std::vector<int> sumcount6(n + 2);
  std::vector<int> value(n + 2);
  sufiks = 0;
  for(int i = n; i >= 1; i--) {
    count6[i] = count6[i + 1];
    sumcount6[i] = sumcount6[i + 1];
    if(a[i] == '7' || a[i] == '?') {
      sufiks += 1;
    }
    else {
      count6[i] += 1;
      sumcount6[i] += sufiks;
    }
    value[i] = sufiks;
  }
  std::vector<int> suf(n + 2);
  for(int i = n; i >= 1; i--) {
    suf[i] = suf[i + 1];
    if(a[i] == '7' || a[i] == '?') {
      suf[i] += 1;
    }
  }
  std::function<int(int, int, int)> ask = [&] (int l, int r, int i) {
    return sumcount6[i] - sumcount6[r + 1] - count6[i] * value[r + 1] + count6[r + 1] * value[r + 1] + sufi[l] - sufi[i] - cnt6i[l] * suf_val[i] + cnt6i[i] * suf_val[i] + suf[i] * cnt6i[l] - suf[i] * cnt6i[i] - suf[r + 1] * cnt6i[l] + suf[r + 1] * cnt6i[i];
  };
  while(q--) {
    int l, r;
    std::cin >> l >> r;
    int ans = std::max(ask(l, r, l), ask(l, r, r));
    std::vector<int> pos;
    for(int i = l; i <= r; i++) {
      if(a[i] == '?') {
        pos.push_back(i);
      }
    }
    if(pos.size() == 0) {
      std::cout << ans << "\n";
      continue;
    }
    int x = pos.size() / 2 - 1;
    if(x < 0) {
      x = 0;
    }
    int y = pos.size() / 2 + 1;
    if(y >= pos.size()) {
      y = pos.size() - 1;
    }
    for(int i = x; i <= y; i++) {
      ans = std::max(ans, ask(l, r, pos[i]));
    }
    std::cout << ans << "\n";
  }

}
/**


int n, q;
  std::cin >> n >> q;
  std::vector<char> a(n + 1);
  for(int i = 1; i <= n; i++) {
    std::cin >> a[i];
  }
  std::vector<int> suf6(n + 2);
  int sufiks = 0;
  std::vector<int> val_suf(n + 2);
  for(int i = n; i >= 1; i--) {
    if(a[i] == '7' || a[i] == '?') {
      sufiks += 1;
      suf6[i] = suf6[i + 1];
    }
    else {
      suf6[i] = suf6[i + 1] + sufiks;
    }
    val_suf[i] = sufiks;
  }
  std::vector<int> cnt6(n + 2);
  for(int i = n; i >= 1; i--) {
    if(a[i] == '6') {
      cnt6[i] = cnt6[i + 1] + 1;
    }
    else {
      cnt6[i] = cnt6[i + 1];
    }
  }
  while(q--) {
    int l, r;
    std::cin >> l >> r;
    int ans = 0;
    for(int x = l; x <= r; x++) {
      // suf6[l] - cnt6[l] * (suf7?[r + 1])
      int desno = suf6[x] - suf6[r + 1] - (cnt6[x] - cnt6[r + 1]) * val_suf[r + 1];
      int levo = sufi[l] - sufi[x]
      ans = std::max(ans, levo + desno);
    }
    std::cout << ans << "\n";
  }




sumcount6[i] - sumcount6[r + 1] - (count6[i] - count6[r + 1]) * value[r + 1] + sufi[l] - sufi[i] - (cnt6i[l] - cnt6i[i]) * suf_val[i] + (cnt6i[l] - cnt6i[i]) * suf;


sumcount6[i] - sumcount6[r + 1] - count6[i] * value[r + 1] + count6[r + 1] * value[r + 1] + sufi[l] - sufi[i] - cnt6i[l] * suf_val[i] + cnt6i[i] * suf_val[i] + cnt6i[l] * suf - cnt6i[i] * suf;

c = - sumcount6[r + 1] + count6[r + 1] * value[r + 1] + sufi[l] + cnt6i[l] * - cnt6i[i] * suf;

**/