/**
cin:
  2 5
  5 10 15 20 30
  4 30
  3 34
cout:
  0
  4
**/
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

typedef struct{
    int fighting;
    int hunger;
    int index;
    int blance;
}pandas;

bool cmp(pandas a, pandas b){
    if (a.fighting > b.fighting){
        return true;
    }
    return false;
}

bool cmpindex(pandas a, pandas b){
    if (a.index < b.index){
        return true;
    }
    return false;
}

int main(){

    int n, m;

    cin >> n >> m;
    vector<int> sugar(m);

    for (int i = 0; i<m; i++){
        cin >> sugar[i];
    }
    sort(sugar.begin(), sugar.end());
    vector<bool> sugarflag(m,true);
    
    vector<pandas> pand(n);
    for (int i = 0; i<n; i++){
        cin >> pand[i].fighting >> pand[i].hunger;
        pand[i].index = i;
        pand[i].blance = 0;
    }
    sort(pand.begin(), pand.end(), cmp);

    int sum;
    for (int i = 0; i <n; i++){
          sum = 0;
          int j = m-1;
          while (j >= 0){
              if (sugarflag[j]){
                  if ((sum + sugar[j]) == pand[i].hunger){
                      sum += sugar[j];
                      sugarflag[j] = false;
                      break;
                  }
                  if (sugar[j]>pand[i].hunger){
                      j--;
                      continue;
                  }
                  if ((sum + sugar[j])>pand[i].hunger){
                      j--;
                      continue;
                  }
                  sum += sugar[j];
                  sugarflag[j] = false;
                  j--;
              }
              j--;
          }
          pand[i].blance = pand[i].hunger - sum;
    }
    sort(pand.begin(), pand.end(), cmpindex);
    for (int i = 0; i < n; i++){
        cout << pand[i].blance << endl;
    }
    return 0;
}