#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
  char ch;
  bool flag = false;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-') flag = true;
  for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  x = flag ? -x : x;
}
inline void write(int x) {
  static const int maxlen = 100;
  static char s[maxlen];
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (!x) {
    putchar('0');
    return;
  }
  int len = 0;
  for (; x; x /= 10) s[len++] = x % 10 + '0';
  for (int i = len - 1; i >= 0; --i) putchar(s[i]);
}
const int MAXN = 110000;
struct kt {
  int a, b, c;
} E[MAXN];
bool cmp_c(kt A, kt B) { return A.c < B.c; }
vector<pair<int, int> > q[MAXN];
int n, m;
vector<int> now;
bool IN[MAXN];
void add(int x) {
  if (IN[x]) return;
  IN[x] = 1;
  now.push_back(x);
}
vector<int> son[MAXN];
unordered_map<int, int> M[MAXN];
struct query {
  int a, b, ans;
} QQ[MAXN];
vector<int> sub_G;
int tag[MAXN];
void dfs(int x, int T) {
  sub_G.push_back(x);
  tag[x] = T;
  for (int i = 0; i < son[x].size(); i++)
    if (tag[son[x][i]] == 0) dfs(son[x][i], T);
}
void doit_great() {
  for (int i = 0; i < sub_G.size(); i++) {
    int x = sub_G[i];
    for (int j = 0; j < q[x].size(); j++)
      if (tag[q[x][j].first] == tag[x]) {
        int ID = q[x][j].second;
        QQ[ID].ans++;
      }
  }
}
void doit_less() {
  for (int i = 0; i < sub_G.size(); i++)
    for (int j = i + 1; j < sub_G.size(); j++) {
      int x = sub_G[i], y = sub_G[j];
      M[x][y]++;
      M[y][x]++;
    }
}
void doit() {
  for (int i = 0; i < now.size(); i++)
    if (tag[now[i]] == 0) {
      sub_G.clear();
      int x = now[i];
      dfs(x, x);
      if (sub_G.size() > sqrt(n))
        doit_great();
      else
        doit_less();
    }
}
void clr() {
  for (int i = 0; i < now.size(); i++) {
    int x = now[i];
    son[x].clear();
    IN[x] = 0;
    tag[x] = 0;
  }
  now.clear();
}
int main() {
  read(n);
  read(m);
  for (int i = 1; i <= m; i++) {
    read(E[i].a);
    read(E[i].b);
    read(E[i].c);
  }
  int Q;
  read(Q);
  for (int i = 1; i <= Q; i++) {
    int x, y;
    read(x);
    read(y);
    q[x].push_back(make_pair(y, i));
    QQ[i].a = x;
    QQ[i].b = y;
    QQ[i].ans = 0;
  }
  sort(E + 1, E + m + 1, cmp_c);
  for (int i = 1; i <= m; i++) {
    int j = i;
    while ((j <= m) && (E[j].c == E[i].c)) {
      int x = E[j].a, y = E[j].b;
      son[x].push_back(y);
      son[y].push_back(x);
      add(x);
      add(y);
      j++;
    }
    doit();
    clr();
    i = j - 1;
  }
  for (int i = 1; i <= Q; i++) printf("%d\n", QQ[i].ans + M[QQ[i].a][QQ[i].b]);
  return 0;
}
